mypythontools_cicd.venvs package¶
Module that will help with virtual environments so it’s possible to work with venv from python. You can create, delete or update dependencies.
- class mypythontools_cicd.venvs.Venv(venv_path: Union[Path, str], with_wsl: bool = False)[source]¶
Bases:
object
You can create new venv or sync it’s dependencies.
- venv_path¶
Path of venv. E.g. venv
- Type
Path
- with_wsl¶
If working with linux venv from linux.
- Type
bool, optional
Example
>>> from pathlib import Path >>> import subprocess >>> from mypythontools.system import is_wsl ... >>> path = "venv/doctest/3.10" if platform.system() == "Windows" and not is_wsl() else "venv/doctest/wsl-3.10" >>> venv = Venv(path)
Create venv. Skip if exists
>>> venv.create()
Install one library with
>>> venv.install_library("colorama==0.3.9") >>> "colorama==0.3.9" in venv.list_packages() True
Sync requirements that are in requirements file (also remove if not in requirements)
>>> venv.sync_requirements(verbosity=0, path=PROJECT_PATHS.root) # There ia a 0.4.4 in requirements.txt >>> "colorama==0.4.4" in venv.list_packages() True
You can use this venv from different venv with subprocess
>>> result = subprocess.run(f"{venv.activate_prefix_command} pip list", capture_output=True, shell=True).stdout >>> "colorama" in str(result) True
Remove venv with
>>> venv.remove() >>> Path(path).exists() False
- activate_prefix_command: str¶
This command will activate venv. It also contains shell like e.g. &&, so next command needs to be defined if using in subprocess.
- create(verbose: bool = False) None [source]¶
Create virtual environment. If it already exists, it will be skipped and nothing happens.
- Parameters
verbose (bool, optional) – If True, result of terminal command will be printed to console. Defaults to False.
- create_command: str¶
Command that can be used to create venv.
- executable: Path¶
Path to Python executable (e.g. Python.exe).
- executable_str: str¶
Path to Python executable in posix string form.
- install_library(name: str, verbose: bool = False, upgrade: bool = False, path: None | PathLike = None) None [source]¶
Install package to venv with pip install.
You can use extras with square brackets.
- Parameters
name (str) – Name of installed library.
verbose (bool, optional) – If True, result of terminal command will be printed to console. Defaults to False.
upgrade (bool, optional) – Update flag. If True, then latest is installed. If False, and already exists, it’s skipped. Defaults to False
path (None | Pathlike, optional) – If installing from local path, this is path from where you can use relative paths. Defaults to None.
- installed: bool¶
Whether venv is installed on defined path. Inferred just in init - static variable.
- list_packages() str [source]¶
Get list of installed libraries in the venv.
The reason why it’s meta coded via string parsing and not parsed directly is that it needs to be available from other venv as well.
- scripts_path: Path¶
Path to scripts like for example pip or black.
- subprocess_prefix¶
Run as module, so library can be directly call afterwards. Can be directly used in terminal. It can look like this for example:
.../Scripts/activate.bat && .../venv/Scripts/python.exe -m
- sync_requirements(requirements_files: None | Literal['infer'] | PathLike | Sequence[PathLike] = 'infer', requirements: None | list[str] = None, verbosity: Literal[0, 1, 2] = 1, path: PathLike = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/mypythontools-cicd/checkouts/latest/docs/source')) None [source]¶
Sync libraries based on requirements. Install missing, remove unnecessary.
- Parameters
requirements_files (Literal["infer"] | PathLike | Sequence[PathLike], optional) – Define what libraries will be installed. If “infer”, autodetected. Can also be a list of more files e.g [“requirements.txt”, “requirements_dev.txt”]. Defaults to “infer”.
requirements (list[str], optional) – List of requirements. Defaults to None.
verbosity (Literal[0, 1, 2], optional) – If 0, prints nothing, if 1, then one line description of what happened is printed. If 3, all the results from terminal are printed. Defaults to 1.
path (PathLike, optional) – If using just names or relative path, and not found, define the root. It’s also necessary when using another referenced files. If inferring files, it’s used to search. Defaults to PROJECT_PATHS.root.
- uninstall_library(name: str, verbose: bool = False) None [source]¶
Uninstall package to venv with pip install.
- Parameters
name (str) – Name of library to uninstall.
verbose (bool, optional) – If True, result of terminal command will be printed to console. Defaults to False.
- venv_path¶
Path to venv prefix, e.g. …/venv
- mypythontools_cicd.venvs.is_venv() bool [source]¶
True if run in venv, False if run in main interpreter directly.
- mypythontools_cicd.venvs.prepare_venvs(path: None | PathLike = 'venv', versions: Sequence[str] = ['3.7', '3.10', 'wsl-3.7', 'wsl-3.10'], verbosity: Literal[0, 1, 2] = 1)[source]¶
This will install virtual environments with defined versions.
Installation will be skipped if there is already existing folder with content. It is possible tu use wsl on windows. You have to install python launcher when using linux or wsl. More about python-launcher https://github.com/brettcannon/python-launcher
- Parameters
path (None | PathLike) – Where venvs will be stored. If None, cwd() will be used. Defaults to “venv”.
versions (Sequence[str], optional) – List of used versions. If you want to use wsl, add wsl- prefix like for example wsl-3.7. Defaults to [“3.7”, “3.10”, “wsl-3.7”, “wsl-3.10”].
verbosity (Literal[0, 1, 2], optional) – If 0, prints nothing, if 1, then one line description of what happened is printed. If 3, all the results from terminal are printed. Defaults to 1.