virtual_env
- class pcs.virtual_env.NoOpVirtualEnv(venv_path: Optional[pathlib.Path] = None)
This class implements the VirtualEnv interface, but does not actually modify the Python environment in which the program is executing. Use this class anywhere you need a VirtualEnv object but where you also do not want to modify the execution environment (i.e. you just want to run code in the existing Python environment).
- activate() None
Activates the virtual environment currently being managed. When activated, an import statement (e.g. run by a Component) will execute within the virtual environment.
- create_virtual_env() None
Creates the directory and objects that back the virtual environment.
- deactivate() None
Deactivates the virtual environment (i.e. re-activates the default environment under which AgentOS was executed).
- classmethod from_requirements_paths(req_paths: Sequence) pcs.virtual_env.VirtualEnv
Takes a sequence of full paths to pip-compatible requirements files, creates a new virtual environment, installs all those requirements into that environment, and then returns a VirtualEnv object corresponding to that virtual environment.
- install_requirements_file(req_path: pathlib.Path, pip_flags: Optional[dict] = None) None
Installs the requirements_file pointed at by req_path into the virtual environment.
pip_flags
is a dictionary of command-line flags to pass to pip during the installation, for example:` {'-F': 'https://example.com/foo/bar'} `
results in pip being run with the following command-line flags:
` pip install .. -F https://example.com/foo/bar `
- class pcs.virtual_env.VirtualEnv(venv_path: Optional[pathlib.Path] = None)
This class manages a Python virtual environment. It provides methods to setup, enable, and disable virtual environments as well as utility methods such as one to clear the whole virtual environment cache.
- __init__(venv_path: Optional[pathlib.Path] = None)
- activate() None
Activates the virtual environment currently being managed. When activated, an import statement (e.g. run by a Component) will execute within the virtual environment.
- static clear_env_cache(env_cache_path: Optional[pathlib.Path] = None, assume_yes: bool = False) None
Completely removes all the virtual environments that have been created for Components. Pass True to
assume_yes
to run non-interactively.
- create_virtual_env() None
Creates the directory and objects that back the virtual environment.
- deactivate() None
Deactivates the virtual environment (i.e. re-activates the default environment under which AgentOS was executed).
- classmethod from_requirements_paths(req_paths: Sequence) pcs.virtual_env.VirtualEnv
Takes a sequence of full paths to pip-compatible requirements files, creates a new virtual environment, installs all those requirements into that environment, and then returns a VirtualEnv object corresponding to that virtual environment.
- install_requirements_file(req_path: pathlib.Path, pip_flags: Optional[dict] = None) None
Installs the requirements_file pointed at by req_path into the virtual environment.
pip_flags
is a dictionary of command-line flags to pass to pip during the installation, for example:` {'-F': 'https://example.com/foo/bar'} `
results in pip being run with the following command-line flags:
` pip install .. -F https://example.com/foo/bar `
- set_env_cache_path(env_cache_path: pathlib.Path) None
Allows overriding of the path of the environment cache. The environment cache is where all the virtual environments for Components are created.
- pcs.virtual_env.auto_revert_venv()
Use this context manager when you need to revert the Python environment to whatever was in place before the managed block. Useful in tests when an exception might leave the environment in an unexpected state and cause spurious test failures.
Usage example:
with auto_revert_venv(): # Do something here that may fail, leaving the env in a bad state # Env guaranteed to be reset to its pre-managed-block state here