python virtualenv 介绍

virtualenv是一个创建python独立运行环境的工具。类似的工具还有pipenv, venv。

独立运行环境带来的好处是多个项目开发,依赖module隔离,可以依赖同一module的不同版本,清理环境更加彻底。

virtualenv 安装

pip install virtualenv

创建虚拟python环境

[root@sam tmp]# virtualenv python-project
New python executable in /tmp/python-project/bin/python2
Also creating executable in /tmp/python-project/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...done.
[root@sam tmp]# cd python-project/
[root@sam python-project]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Dec 22 16:13 bin
drwxr-xr-x 2 root root 4096 Dec 22 16:13 include
drwxr-xr-x 3 root root 4096 Dec 22 16:13 lib
lrwxrwxrwx 1 root root    3 Dec 22 16:13 lib64 -> lib
-rw-r--r-- 1 root root   60 Dec 22 16:13 pip-selfcheck.json
[root@sam python-project]# source bin/activate
(python-project) [root@sam python-project]# pip install requests
(python-project) [root@sam python-project]# cd lib/python2.7/site-packages/
(python-project) [root@sam site-packages]# ls -l
total 76
drwxr-xr-x  2 root root 4096 Dec 22 16:14 certifi
drwxr-xr-x  2 root root 4096 Dec 22 16:14 certifi-2017.11.5.dist-info
drwxr-xr-x  3 root root 4096 Dec 22 16:14 chardet
drwxr-xr-x  2 root root 4096 Dec 22 16:14 chardet-3.0.4.dist-info
-rw-r--r--  1 root root  126 Dec 22 16:13 easy_install.py
-rw-r--r--  1 root root  315 Dec 22 16:13 easy_install.pyc
drwxr-xr-x  2 root root 4096 Dec 22 16:14 idna
drwxr-xr-x  2 root root 4096 Dec 22 16:14 idna-2.6.dist-info
drwxr-xr-x 10 root root 4096 Dec 22 16:13 pip
drwxr-xr-x  2 root root 4096 Dec 22 16:13 pip-9.0.1.dist-info
drwxr-xr-x  4 root root 4096 Dec 22 16:13 pkg_resources
drwxr-xr-x  2 root root 4096 Dec 22 16:14 requests
drwxr-xr-x  2 root root 4096 Dec 22 16:14 requests-2.18.4.dist-info
drwxr-xr-x  4 root root 4096 Dec 22 16:13 setuptools
drwxr-xr-x  2 root root 4096 Dec 22 16:13 setuptools-38.2.4.dist-info
drwxr-xr-x  5 root root 4096 Dec 22 16:14 urllib3
drwxr-xr-x  2 root root 4096 Dec 22 16:14 urllib3-1.22.dist-info
drwxr-xr-x  4 root root 4096 Dec 22 16:13 wheel
drwxr-xr-x  2 root root 4096 Dec 22 16:13 wheel-0.30.0.dist-info
(python-project) [root@sam site-packages]# deactivate 
[root@sam site-packages]#
  • virtualenv python-project: python-project是虚拟环境根目录
  • source bin/activate: 进入虚拟python环境,之后相关的python操作都是针对此虚拟运行环境
  • pip install requests: 虚拟环境安装requests module示例
  • deactivate: 退出虚拟运行环境

删除虚拟环境

[root@sam tmp]# rm -rf python-project/

你可能感兴趣的:(python virtualenv 介绍)