Virtualenv
Something of a tool, the tool can easy to create isolated Python environments–Virtualenv.
本文为学习Virtualenv官方文档的笔记
英文好的出门左拐官方文档美滋滋
Why use Virtualenv?
我们一般在本地会有很多项目,每个项目所依赖的包一般都是不同的,如果在整个系统的Python环境下安装各种项目需要的第三方模块会导致整个系统的Python环境十分臃肿,也很容易搞乱整个环境,造成不好修复的后果。如果我们能为每个项目都提供独立的Python运行环境与系统隔离不是美滋滋,Virtualenv就是这样美滋滋的工具
Virtualenv能创建理论上无数的不同的Python运行环境,在每个环境里面我们可以做着和在系统环境下一模一样的事情,且不论你怎么弄都不会影响到全局的Python环境
Installation
To install globally with pip(if you have pip 1.3 or greater installed globally)
1 | [sudo] pip install virtualenv |
Example
The following procedure indicates that the installation is successful,the installation version is 16.0.0
1 | smileorigins-MacBook-Pro:~ smileorigin$ pip3 install virtualenv |
Usage
Virtualenv has one basic command
1 | virtualenv ENV |
ENV is a directory to place the new virtual environment
- ENV/lib/ and ENV/include/
Containing supporting library files for a new virtualenv python
Packages installed in this environment will live under ENV/lib/pythonX.X/site-packages/ - ENV/bin/
Containing the executables – noticeably a new python
Thus running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python - pip and setuptools are installed
This associated pip can be run from ENV/bin/pip
Example
1 | smileorigins-MacBook-Pro:~ smileorigin$ mkdir test_env |
Activate script
In a newly created virtualenv there will also be a activate shell script
On Posix systems, this resides in ENV , so you can run :
1 | source bin/activate |
To undo these changes to your path (and prompt), just run:
1 | deactivate |
Example
1 | smileorigins-MacBook-Pro:test_env smileorigin$ source bin/activate |
Inherit global site-packages
If you want to inherit packages from /usr/lib/python2.7/site-packages (or whatever your global site-packages directory is), you can run:
1 | virtualenv --system-site-packages ENV |
This can be used if you have control over the global site-packages directory, and you want to depend on the packages there. If you want isloation from the global system, do not use this flag.
Example
My global python environment is 3.7.0, so we can see the result is inherit 3.7.0
1 | smileorigins-MacBook-Pro:~ smileorigin$ mkdir test_inherit |
- 本文标题:Virtualenv
- 本文作者:Thomas Woo
- 发布时间:2018-08-06 18:20
- 最后更新:2020-06-20 15:41
- 本文链接:http://smileorigin.site/python/virtualenv/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY 4.0 许可协议。转载请注明出处!