Fork me on GitHub

Python之虚拟环境

Python之虚拟环境

简介

virtualenv可以搭建虚拟且独立的python运行环境, 使得单个项目的运行环境与其它项目独立起来.

项目实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@linux-node8 ~]# pip install virtualenv
Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 600kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
[root@linux-node8 ~]# virtualenv test1 # 创建虚拟环境
New python executable in /root/test1/bin/python2
Also creating executable in /root/test1/bin/python
Installing setuptools, pip, wheel...done.
[root@linux-node8 ~]# source test1/bin/activate # 进入虚拟环境目录
(test1) [root@linux-node8 ~]# ls -l
total 8
-rw-------. 1 root root 1086 Feb 26 2017 anaconda-ks.cfg
-rw-r--r-- 1 root root 424 Mar 2 04:43 requirements.txt
drwxr-xr-x 5 root root 77 Mar 2 04:57 test1
(test1) [root@linux-node8 ~]# deactivate # 退出虚拟环境
[root@linux-node8 ~]#


[root@linux-node8 ~]# virtualenv --system-site-packages test2 # 默认情况下, 虚拟环境中不包括系统的site-packages, 若要使用请添加参数
New python executable in /root/test2/bin/python2
Also creating executable in /root/test2/bin/python
Installing setuptools, pip, wheel...done.
[root@linux-node8 ~]# source test2/bin/activate
(test2) [root@linux-node8 ~]# pip freeze >1.txt
(test2) [root@linux-node8 ~]# cat 1.txt
backports.ssl-match-hostname==3.4.0.2
chardet==2.2.1
configobj==4.7.2
.............
urlgrabber==3.10
virtualenv==15.1.0
yum-metadata-parser==1.1.4
(test2) [root@linux-node8 ~]# deactivate

参考资料

1、官网资料:https://virtualenv.pypa.io/en/stable/

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/70f31043.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!