python虚拟环境
我们在写的Python程序的时候,经常会用到非标准库的包和模块,比如要求等非常有用的第三方包和模块。有时候也会用到某个包和模块的特定版本,可能是这个特定的版本修复了某个bug,或者是依赖的接口只有该版本有。也可能系统安装了Python3.6,但应用程序要求3.7或Python2.7。
这些情况注定了对多个Python及其库的需求。如果我们写的程序依赖某个库的1.0的版本,而系统安装的是1.2的版本,应用程序就无法运行;如果应用程序要求Python2.7,则它在Python3上就可能会保存。
要解决这些不同需求的问题就是使用虚拟环境,它是一个目录树,其中安装特定的Python的版本及需要的库的版本。这样,不同的应用可以使用不同的虚拟环境来满足其运行条件。不同虚拟环境内部的库版本涉及不会影响其它虚拟环境。
Python的虚拟环境的创建和使用
在Python2时代,虚拟环境管理用的是virtualenv及其封装virtuaalenvwrapper这两个包(通过pipinstall可以安装)。它们也支持在Python3下使用。virtuaalenvwrapper是使用shell开发的,因此不支持Windows。支持Windows的叫做virtuaalenvwrapper-win。
从Python3.3开始,引入了新的库venv来进行虚拟环境的管理,详见PEP-405的描述。它的很多操作都和virtualenv类似。pyvenv是Python3.3和3.4中创建虚拟环境的推荐工具,但是在Python3.6中已经弃用,之后的版本还是用venv。
使用venv创建³³虚拟环境还是很容易的,将venv模块作为脚本运行并确定虚拟环境存放的路径即可:
python3-mvenv/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
(脚本这个的英文为bashshell关系编写的,你如果使用的的英文csh或fish壳,使用就要activate.csh或activate.fis脚本来激活)。
激活虚拟环境后,你的壳提示就会显示你正在使用的虚拟环境的名称,你就可以使用该虚拟环境下的Python的及相关的库了:
$sourcemyenv/bin/activate
(myenv)veelion@gtx:~/p2/tutorial/md_Python/codes$whichpython
/home/veelion/p2/tutorial/md_Python/codes/myenv/bin/python
(myenv)veelion@gtx:~/p2/tutorial/md_Python/codes$python
Python3.6.8(default,Dec242018,19:24:27)
[GCC5.4.020160609]onlinux
Type"help","copyright","credits"or"license"formoreinformation.
>>>importsys
>>>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']
>>>
以上内容为大家介绍了python虚拟环境,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。http://www.mobiletrain.org/

相关推荐HOT
更多>>
python中ndarray与list转换的方法
python中ndarray与list转换的方法在python数据类型中,list元素可以任意类型组合,而ndarray元素类型必须相同,但是ndarray可以更方便的对多维...详情>>
2023-11-10 08:51:14
python中OrdereDict如何使用?
python中OrdereDict如何使用?我们在对数据进行处理时,如果能够使它们保存顺序的排序,那么用起来会非常的便利。不过字典本身就是没有顺序的,...详情>>
2023-11-10 07:05:00
python中如何使用scipy.fftpack模块计算快速傅里叶变换?
python中如何使用scipy.fftpack模块计算快速傅里叶变换?在编程中,快速傅里叶变换是工程中非常有价值的一类算法,它可以将时域和频域的信号相互...详情>>
2023-11-10 05:50:23
zip()函数如何在python中遍历多个列表?
zip()函数如何在python中遍历多个列表?下面我们就python中zip的说明、语法、使用注意点进行讲解,然后带来遍历多个列表的实例。1、说明zip()函...详情>>
2023-11-10 05:38:33热门推荐
python concat函数有何用法?
沸python里函数装饰器怎么使用?
热如何实现python中的continue语句?
热python中的冒泡排序是什么?
新python中str函数是什么
如何使用python实现项目进度显示?
python中ndarray与list转换的方法
python复制文件的方法整理
Python中numpy数组如何添加元素
python有几种可视化图形库?
python中xlwings是什么?
python上下文管理器的基本介绍
Python自动化测试基础必备知识点一
python中OrdereDict如何使用?
技术干货






