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
2
3
4
5
6
7
8
9
smileorigins-MacBook-Pro:~ smileorigin$ pip3 install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB)
100% |████████████████████████████████| 1.9MB 88kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.0.0
smileorigins-MacBook-Pro:~ smileorigin$ virtualenv --version
16.0.0
smileorigins-MacBook-Pro:~ smileorigin$

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
2
3
4
5
6
smileorigins-MacBook-Pro:~ smileorigin$ mkdir test_env
smileorigins-MacBook-Pro:~ smileorigin$ virtualenv test_env
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/smileorigin/test_env/bin/python3.7
Also creating executable in /Users/smileorigin/test_env/bin/python
Installing setuptools, pip, wheel...done.

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
2
3
4
5
6
7
smileorigins-MacBook-Pro:test_env smileorigin$ source bin/activate
(test_env) smileorigins-MacBook-Pro:test_env smileorigin$ python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(test_env) smileorigins-MacBook-Pro:test_env smileorigin$ deactivate

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
2
3
4
5
6
7
8
9
10
11
12
13
14
smileorigins-MacBook-Pro:~ smileorigin$ mkdir test_inherit
smileorigins-MacBook-Pro:~ smileorigin$ virtualenv --system-site-packages test_inherit
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/smileorigin/test_inherit/bin/python3.7
Also creating executable in /Users/smileorigin/test_inherit/bin/python
Installing setuptools, pip, wheel...done.
smileorigins-MacBook-Pro:~ smileorigin$ cd test_inherit
smileorigins-MacBook-Pro:test_inherit smileorigin$ source bin/activate
(test_inherit) smileorigins-MacBook-Pro:test_inherit smileorigin$ python --version
Python 3.7.0
(test_inherit) smileorigins-MacBook-Pro:test_inherit smileorigin$ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×