您现在的位置是:首页 > 学无止境 > 其他网站首页其他 Python虚拟环境

Python虚拟环境

  • 莫愁
  • 其他
  • 2019-07-28
简介我们在写Python程序的时候,经常会用到非标准库的包和模块,比如requests等非常有用的第三方包和模块。有时候也会用到某个包和模块的特定版本,可能是这个特定的版本修复了某个bug,或者是依赖的接口只有该版本有。也可能系统安装了Python 3.6,但应用程序要求 3.7 或 Python 2.7。
字数 2464

我们在写Python程序的时候,经常会用到非标准库的包和模块,比如requests等非常有用的第三方包和模块。有时候也会用到某个包和模块的特定版本,可能是这个特定的版本修复了某个bug,或者是依赖的接口只有该版本有。也可能系统安装了Python 3.6,但应用程序要求 3.7 或 Python 2.7。

Python虚拟环境

这些情况注定了对多个Python及其库的需求。如果我们写的程序依赖某个库的1.0的版本,而系统安装的是1.2的版本,应用程序就无法运行;如果应用程序要求Python 2.7,则它在Python3上就可能会保存。

要解决这些不同需求的问题就是使用虚拟环境,它是一个目录树,其中安装特定的Python版本及需要的库的版本。这样,不同的应用可以使用不同的虚拟环境来满足其运行条件。不同虚拟环境内部的库版本涉及不会影响其它虚拟环境。

 

Python虚拟环境的创建和使用

在Python 2 时代,虚拟环境管理用的是virtualenv及其封装virtuaalenvwrapper这两个包(通过 pip install可以安装)。它们也支持在Python3下使用。virtuaalenvwrapper是使用shell开发的,因此不支持Windows。支持Windows的叫做virtuaalenvwrapper-win

从 Python 3.3 开始,引入了新的库venv来进行虚拟环境的管理,详见 PEP-405 的描述。它的很多操作都和virtualenv类似。pyvenv是Python 3.3 和 3.4 中创建虚拟环境的推荐工具,但是在 Python 3.6 中已经弃用,之后的版本还是用 venv

使用venv创建虚拟环境还是很容易的,将venv模块作为脚本运行并确定虚拟环境存放的路径即可:

python3 -m venv /path/to/myenv

这条命令将创建/path/to/myenv目录,并在其中创建包含Python解释器、标准库和各种支持文件的目录:

myenv
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── easy_install
│   ├── easy_install-3.6
│   ├── pip
│   ├── pip3
│   ├── pip3.6
│   ├── python -> python3.6
│   ├── python3 -> python3.6
│   └── python3.6 -> /usr/bin/python3.6
├── include
├── lib
│   └── python3.6
│       └── site-packages
├── lib64 -> lib
└── pyvenv.cfg

创建虚拟环境后,就可以激活并使用它。在Linux和macOS上,运行:

source /path/to/myenv/bin/activate

(这个脚本是为bash shell编写的,如果你使用的是cshfish shell,就要使用activate.cshactivate.fis脚本来激活。)

激活虚拟环境后,你的shell提示就会显示你正在使用的虚拟环境的名称,你就可以使用该虚拟环境下的Python及相关的库了:

$ source myenv/bin/activate
(myenv) veelion@gtx:~/p2/tutorial/md_Python/codes$ which python
/home/veelion/p2/tutorial/md_Python/codes/myenv/bin/python
(myenv) veelion@gtx:~/p2/tutorial/md_Python/codes$ python
Python 3.6.8 (default, Dec 24 2018, 19:24:27) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/veelion/p2/tutorial/md_Python/codes/myenv/lib/python3.6/site-packages']
>>> 

运行venv的命令如果加-h选项,就可以看到详细的使用方法:

$ python3 -m venv -h
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:  ENV_DIR               A directory to create the environment in.
optional arguments:
  -h, --help            show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  --copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)

Once an environment has been created, you may wish to activate it, e.g. bysourcing an activate script in its bin directory.

 

Ubuntu 16.04 上 Python2 使用 virtualenvwrapper 创建 Python 3 的虚拟环境

Ubuntu 16.04 系统默认的Python 还是2,要在这个系统上使用Python 3.6 或 Python 3.7,最好的方法就是创建它们的虚拟环境。目前可能还有很多人在使用这个版本的Ubuntu,所以有必要讲一下这一小节的内容。这里,我们推崇使用virtualenvwrapper,拒绝使用Anacodna

 

(1)Ubuntu 16.04 上安装 Python 3.6,3.7 和 3.8

可以通过ppa源安装,也可以从源码编译安装。为了方便(安装和以后更新),我们选择ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.6 python3.6-dev

如果安装3.7或3.8,替换掉上面命令中的3.6即可。

连接PPA比较慢的话,可以用USTC的反向代理,https://lug.ustc.edu.cn/wiki/mirrors/help/revproxy
只要一句话,全替换成USTC加速的PPA:

sudo find /etc/apt/sources.list.d/ -type f -name "*.list" -exec sed
-i.bak -r  's#deb(-src)?/s*http(s)?://ppa.launchpad.net#deb/1
http/2://launchpad.proxy.ustclug.org#ig' {} /;

 

(2)安装配置virtualenvwrapper

首先,安装virtualenvwrapper

sudo pip install virtualenvwrapper

这会将virtualenvwrapper安装到Ubuntu系统的 Python2 搜索的路径,同时也会安装它依赖的virtualenv库。

接着,编辑~/.bashrc,写入一下配置:

# virtualenvwrapper settings
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
source /usr/local/bin/virtualenvwrapper_lazy.sh

配置了虚拟环境的根目录是$HOME/.virtualenvs。为了使以上配置生效,运行命令:

$ source ~/.bashrc

这时候,命令行里面就有了命令:mkvirutalenv

 

(3)创建虚拟环境

运行命令:

mkvirtualenv py3.6 -p=/usr/bin/python3.6

它的意思是,创建一个名为py3.6的虚拟环境,它的解释器是/usr/bin/python3.6,即我们创建了一个Python 3.6的虚拟环境叫做py3.6。

workon命令来激活py3.6这个虚拟环境:

$ workon py3.6
(py3.6) veelion@gtx:~$

这是shell提示的开始多了(py3.6)

退出虚拟环境,返回系统默认环境的命令是:

deactivate

 

练习:

创建自己的虚拟环境,如果你用的是Python3,请用官方的venv来创建虚拟环境。

 

Python虚拟环境总结

(1)虚拟环境是干什么用的?

(2)Python3 官方自带的 venv 的使用

(3)在Python2下用virtualenvwrapper创建Python3.6的虚拟环境。


转载: 感谢您对莫愁个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源莫愁个人博客 https://www.mochoublog.com/study/295.html”。

文章评论

    • 评论
    人参与,条评论

技术在线

服务时间

周一至周日 12:00-22:00

关闭下雪
关闭背景特效