理解venv module in Python

本文详细介绍了Python中venv模块的功能与使用方法。venv模块用于创建轻量级虚拟环境,支持独立的site目录,并能选择性地隔离系统目录。文章还解释了虚拟环境的布局,并说明了如何通过venv模块进行安装与使用。
  • venv

    documents

    source code

    理解site,site-packages in Python

    PEP 405

    Creating a virtual environment

    PEP405 proposes adding a new venv module to the standard library which implements the creation of virtual environments.

    The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories.

    Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

  • Virtual environment layout

    A typical virtual environment layout on a POSIX system would be:

    pyvenv.cfg
    bin/python3
    bin/python
    bin/pysetup3
    include/
    lib/python3.3/site-packages/
    

    While on a Windows system:

    pyvenv.cfg
    Scripts/python.exe
    Scripts/python3.dll
    Scripts/pysetup3.exe
    Scripts/pysetup3-script.py
    		... other DLLs and pyds...
    Include/
    Lib/site-package
    

    Third-party packages installed into the virtual environment will have their Python modules placed in the site-packages directory, and their executables placed in bin/ or Scripts.

  • Splitting the meanings of sys.prefix

    sys.prefix is describes asA string giving the site-specific directory prefix where the platform independent Python files are installed.” Bu default, this is the string '/usr/local'.

    If a virtual environment is in effect, this value will be changed in site.py to point to the virtual environment. The value for the Python installation will still be avaiable, via base_prefix.

    The standard library and header files as found under sys.prefix, but without site-packages.

  • 安装及使用

    python标准库

    不同于作为application的virtualenv, venv更多是作为module library存在,当然也可以通过python -m venv来调用scripts.

    python -m venv pro
    source pro/bin/activate # 激活
    deactivate
    rm -rf pro
    
  • References

  1. [Lib/venv/ : Creation of virtual environments](Lib/venv/ : Creation of virtual environments )
Python 的 `venv` 模块是用于创建轻量级虚拟环境的标准工具,自 Python 3.3 起内置,无需额外安装。它允许用户为不同的项目创建隔离的环境,以避免全局安装包带来的依赖冲突。 ### 创建虚拟环境 使用 `venv` 模块创建虚拟环境非常简单。以下命令将在当前目录下创建一个名为 `test_env` 的新虚拟环境: ```bash $ python3 -m venv test_env ``` 执行完成后,`test_env` 目录将包含一个独立的 Python 解释器和标准库副本。 ### 激活虚拟环境 创建虚拟环境后,需要激活它以便在该环境中运行 Python 脚本。激活方式取决于操作系统: - **在 Unix 或 macOS 上**: ```bash $ source test_env/bin/activate ``` - **在 Windows 上**: ```cmd > test_env\Scripts\activate ``` 激活后,命令行提示符将显示虚拟环境名称(如 `(test_env)`),表示当前处于该环境中。 ### 安装包和管理依赖 一旦虚拟环境被激活,可以使用 `pip` 安装所需的包,这些包将仅安装在当前虚拟环境中,不会影响全局 Python 安装[^1]。例如: ```bash (test_env) $ pip install requests ``` 如果项目依赖多个包,可以使用 `requirements.txt` 文件来管理依赖。通过以下命令可以一次性安装所有依赖: ```bash (test_env) $ pip install -r requirements.txt ``` ### 停用虚拟环境 完成工作后,可以通过 `deactivate` 命令退出虚拟环境: ```bash (test_env) $ deactivate ``` 这将恢复使用全局 Python 环境。 ### 使用 `setup.py` 管理包 在开发 Python 包时,`setup.py` 是一个关键文件,用于定义包的元数据(如名称、版本、依赖等)[^3]。以下是一个简单的 `setup.py` 示例: ```python from setuptools import setup setup( name='mytestpack', version='0.1', install_requires=[ 'requests', ], ) ``` 通过 `setup.py`,可以执行多种包管理操作,例如构建源码分发包: ```bash $ python setup.py sdist ``` 或安装包到当前虚拟环境中: ```bash $ python setup.py install ``` 此外,`setup.py` 还可用于将包上传到 PyPI(Python Package Index),以便他人安装和使用[^4]。 ### 注意事项 - `venv` 适用于 Python 3.3 及更高版本。如果使用的是更早版本的 Python,则需要使用 `virtualenv` 来创建虚拟环境[^2]。 - 在虚拟环境中使用 `if __name__ == '__main__':` 结构可以确保某些代码仅在脚本直接运行时执行,而不是在模块被导入时执行[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值