管理虚拟环境
项目描述
Dephell VEnvs
管理Python虚拟环境。
安装
从PyPI安装
python3 -m pip install --user dephell_venvs
从管理器获取venv
from pathlib import Path
from dephell_venvs import VEnv, VEnvs
# pass here path with substitutions:
venvs = VEnvs(path=Path() / '{project}-{digest}' / '{env}')
VEnvs
获取参数path
,它是虚拟环境的路径,带有替换
{project}
- 路径中项目的最后一部分。{digest}
- 项目完整路径的短哈希值,以避免冲突。{env}
- 子环境名称,因为大多数项目需要多个环境。例如,tests
、docs
、tests-py35
。
从VEnvs
获取VEnv
对象的方式
venvs.get(project_path, env)
。传递项目路径和子环境名称,DepHell将在路径模板中替换它们,并返回该路径的VEnv
实例。venvs.get_by_name(name)
。仅传递名称,这将作为{project}
替换,其他替换({digest}
、{env}
)将直接删除。venvs.current
-- 如果有虚拟环境处于活动状态,则返回当前活动venv。
示例
venv = venvs.get(project_path=Path('dephell_venvs'), env='pytest')
# VEnv(path=PosixPath('dephell_venvs/pytest'), project='dephell_venvs', env='pytest')
管理venv
VEnv
可以从VEnvs
获取或手动创建
venv = VEnv(path=Path('venv'))
检查是否存在
venv.exists()
# False
创建和销毁
venv.create()
venv.destroy()
一些有用的信息
venv.bin_path
# PosixPath('dephell_venvs-njyT/pytest/bin')
venv.lib_path
# PosixPath('dephell_venvs-njyT/pytest/lib/python3.7/site-packages')
venv.python_path
# PosixPath('dephell_venvs-njyT/pytest/bin/python3.7')
venv.prompt
# 'dephell_venvs/pytest'
venv.python
# Python(path=PosixPath('dephell_venvs-njyT/pytest/bin/python3.7'), version='3.7.0', implementation='python', abstract=False)
关于 Python
对象的详细信息,请参阅 dephell_pythons。