python基础教程:虚拟环境

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

这些情况注定了对多个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壳,使用就要activate.cshactivate.fis脚本来激活)。

激活虚拟环境后,你的壳提示就会显示你正在使用的虚拟环境的名称,你就可以使用该虚拟环境下的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. by
sourcing 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:~$

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

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

deactivate

Python的虚拟环境总结

(1)虚拟环境是干什么用的?
(2)Python3官方自带的venv的使用
(3)在Python2下用virtualenvwrapper创建Python3.6的虚拟环境。

大家在学python的时候肯定会遇到很多难题,以及对于新技术的追求,这里推荐一下我们的Python学习扣qun:784758214,这里是python学习者聚集地!!同时,自己是一名高级python开发工程师,从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每日分享一些学习的方法和需要注意的小细节

猜你喜欢

转载自blog.csdn.net/fanhuasijin1996/article/details/90109580