美文网首页
Python 基础之 Virtualenv详解

Python 基础之 Virtualenv详解

作者: 逢春枯木 | 来源:发表于2019-08-01 00:00 被阅读0次

virtualenv

功能说明

virtualenv is a tool to create isolated Python environments.

virtualenv可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。

安装

easy_install安装

[root@Tiven-CentOS]~# easy_install virtualenv

pip安装

[root@Tiven-CentOS]~# pip install virtualenv

pip安装开发版

[root@Tiven-CentOS]~# pip install https://github.com/pypa/virtualenv/tarball/develop

源码安装

[root@Tiven-CentOS]~# curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-x.x.tar.gz
[root@Tiven-CentOS]~# tar -xvzf virtualenv-x.x.tar.gz
[root@Tiven-CentOS]~# cd virtualenv-x.x
[root@Tiven-CentOS]~# python setup.py install

命令选项

Usage: virtualenv [OPTIONS] DEST_DIR

Options:
  --version             show program's version number and exit 
    显示版本号并退出
  -h, --help            show this help message and exit 
    显示本帮助信息并退出
  -v, --verbose         Increase verbosity. 
    增量信息
  -q, --quiet           Decrease verbosity. 
    消除冗长
  -p PYTHON_EXE, --python=PYTHON_EXE 
                        The Python interpreter to use, e.g.,
                        --python=python2.5 will use the python2.5 interpreter
                        to create the new environment.  The default is the
                        interpreter that virtualenv was installed with
                        (/usr/share/python3/bin/python3.5)
    设定虚拟环境中Python的运行版本
  --clear               Clear out the non-root install and start from scratch. 
    
  --no-site-packages    DEPRECATED. Retained only for backward compatibility. 
                        Not having access to global site-packages is now the
                        default behavior.
    **过时的** 虚拟环境会依赖系统环境中的site packages,就是说系统中已经安装好的第三方package也会安装在虚拟环境中,
    如果不想依赖这些package,那么可以加上参数 
  --system-site-packages
                        Give the virtual environment access to the global
                        site-packages.

    如果在命令行中运行virtualenv --system-site-packages ENV, 会继承/usr/lib/python2.7/site-packages下的所有库, 最新版本virtualenv把访问全局site-packages作为默认行为    default behavior.
  --always-copy         Always copy files rather than symlinking.
  --unzip-setuptools    Unzip Setuptools when installing it.
  --relocatable         Make an EXISTING virtualenv environment relocatable.
                        This fixes up scripts and makes all .pth files
                        relative.
    某些特殊需求下,可能没有网络, 我们期望直接打包一个ENV, 可以解压后直接使用, 这时候可以使用virtualenv -relocatable指令将ENV修改为可更改位置的ENV
  --no-setuptools       Do not install setuptools in the new virtualenv. 不安装setuptools
  --no-pip              Do not install pip in the new virtualenv. 不安装pip
  --no-wheel            Do not install wheel in the new virtualenv.不安装wheel
  --extra-search-dir=DIR
                        Directory to look for setuptools/pip distributions in.
                        This option can be used multiple times.
  --download            Download preinstalled packages from PyPI.
  --no-download, --never-download
                        Do not download preinstalled packages from PyPI.
  --prompt=PROMPT       Provides an alternative prompt prefix for this
                        environment.
  --setuptools          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.
  --distribute          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.
    使virtualenv使用新的基于发行版的包管理系统而不是 setuptools 获得的包。 你现在需要知道的就是 --distribute 选项会自动在新的虚拟环境中安装 pip ,这样就不需要手动安装了。

创建环境

使用virtualenv命令

[root@Tiven-CentOS]~# virtualenv env  
Using base prefix '/usr/share/python3'
New python executable in /root/env/bin/python3.5
Also creating executable in /root/env/bin/python
Installing setuptools, pip, wheel...done.

新建一个名为‘env’的虚拟环境,并在当前目录下新建同名文件夹

virtualenv拷贝了Python可执行文件的副本,并创建一些有用的脚本和安装了项目需要的软件包,你可以在项目的整个生命周期中安装/升级/删除这些包。 它也修改了一些搜索路径,例如PYTHONPATH,以确保:

当安装包时,它们被安装在当前活动的virtualenv里,而不是系统范围内的Python路径。

当import代码时,virtualenv将优先采取本环境中安装的包,而不是系统Python目录中安装的包。

还有一点比较重要,在默认情况下,所有安装在系统范围内的包对于virtualenv是可见的。 这意味着如果你将simplejson安装在您的系统Python目录中,它会自动提供给所有的virtualenvs使用。 这种行为可以被更改,在创建virtualenv时增加 --no-site-packages 选项的virtualenv就不会读取系统包,如下:

[root@Tiven-CentOS]~# virtualenv env --no-site-packages

脚本调用

[root@Tiven-CentOS]~# curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-x.x.tar.gz
[root@Tiven-CentOS]~# tar -xvzf virtualenv-x.x.tar.gz
[root@Tiven-CentOS]~# cd virtualenv-x.x
[root@Tiven-CentOS]~# python virtualenv.py env

新建一个名为'env'的虚拟环境,并在当前目录下新建同名文件夹

激活环境

使用source命令执行虚拟环境目录中bin/activate文件,将激活虚拟环境,命令行前出现(环境名)表示已在虚拟环境中

[root@Tiven-CentOS]~# source env/bin/activate #激活并使用虚拟环境
(env) [root@Tiven-CentOS]~# deactive

退出环境

执行命令deactivate退出虚拟环境

[root@Tiven-CentOS]~# source env/bin/activate
(env) [root@Tiven-CentOS]~# deactivate  退出虚拟环境
[root@Tiven-CentOS]~#

相关文章

网友评论

      本文标题:Python 基础之 Virtualenv详解

      本文链接:https://www.haomeiwen.com/subject/kehfdctx.html