Elegant python Environment Management and python Environment Management
 
Elegant python Environment Management  
Origin  
Scenario 1: manage different python versions 
 
For the management of multiple python versions on the same computer, in order to highlight the common problems, the following are some questions raised on segmentfault. 
 
 
 
From: http://segmentfault.com/q/1010000000162043 
 
Scenario 2: manage different versions of the same library in the same python version 
 
For example, in python2.7, application A is based on Django1.6, and application B is based on Django1.7. How can this problem be managed. 
 
To solve the above problems, we will introduce virtualenv and virtualenvwrapper. 
Virtualenv  
From the official virtualenv website https://virtualenv.pypa.io/en/latest/virtualenv.html#: 
 
"The basic problem being addressed is one of dependencies and versions, and indirectly permissions. imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. how can you use both these applications? If you install everything into/usr/lib/python2.7/site-packages (or whatever your platform's standard location is ), it's easy to end up in a situation where you unintentionally upgrade an application that shouldn't be upgraded. 
 
Or more generally, what if you want to install an applicationAnd leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application. 
 
Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host ." 
 
Virtualenv is used to create multiple independent python runtime environments on a machine: 
 
L isolate third-party package dependencies between projects 
 
L install the new Python software package without permission 
 
In addition, there is an additional benefit: when deploying an application, you can package the virtual environment of the development environment into the production environment. 
Install  
Method 1: Use pip 
 
 
  
   
   | [Sudo] pip install virtualenv  | 
 
  
 
Method 2: source code 
 
 
  
   
   | Download and decompress  Cd virtualenv-X.X  [Sudo] python setup. py install  | 
 
  
 
Method N... 
Use Create a virtual environment  
 
  
   
   | Virtualenv [Virtual Environment name]/virtualevn.exe [Virtual Environment name]  | 
 
  
 
My environment is windows python3.4, so as to create a virtual environment: 
 
E: \> D: \ Python34 \ Scripts \ virtualenv.exe env_py3.4 
 
Using base prefix 'd: \ python34' 
 
New python executable in env_py3.4 \ Scripts \ python.exe 
 
Installing setuptools, pip... done. 
 
After the virtual environment env_py3.4 is created, the corresponding directory contains the following folders: 
 
E: \ env_py3.4 directory 
 
<DIR>. 
 
<DIR> .. 
 
2014/05/16 :34 <DIR> Include 
 
<DIR> Lib 
 
<DIR> Scripts 
 
Here, Lib/site-packages contains the libraries installed from the python3.4 system. By default, the virtual environment depends on the site packages in the system environment, that is, the third-party packages installed in the system will also be installed in the virtual environment. If you do not want to rely on these packages, you can add the -- no-site-packages parameter to create a virtual environment. 
 
 
  
   
   | Virtualenv -- no-site-packages [Virtual Environment name]  | 
 
  
 
  
Enable Virtual Environment  
  
 
  
 
 
  
   
   | Source bin/activate or Scripts \ activate. bat  | 
 
  
 
After the virtual environment is enabled, the command line prompt is prefixed with the virtual environment name, as shown in figure(Env_py3.4)E: \ env_py3.4>. You can also print out OS. sys. path in python for verification. For example, after the env_py3.4 virtual environment is enabled, the system path is as follows: 
 
E: \ env_py3.4>Scripts \ activate 
 
(Env_py3.4)E: \ env_py3.4> python 
 
Python 3.4.0 (v3.4.0: 04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (In 
 
Tel)] on win32 
 
Type "help", "copyright", "credits" or "license" for more information. 
 
>>> Import OS 
 
>>>Print (OS. sys. path) 
 
['', 'E: \ env_py3.4 \ Scripts \ python34.zip ', 'e: \ env_py3.4 \ DLLs', 'e: \ env_py3 
 
. 4 \ lib ', 'e: \ env_py3.4 \ Scripts', 'd: \ Python34 \ Lib ', 'd: \ Python34 \ DLLs ',' 
 
E: \ env_py3.4 ', 'e: \ env_py3.4 \ lib \ site-packages'] 
 
From OS. sys. path, we can see that all operations related to python in this environment are limited to virtual environments. 
Exit the Virtual Environment  
  
 
 
  
   
   | Bin/deactivate or Scripts \ deactivate  | 
 
  
 
If you exit the env_py3.4 virtual environment, the prefix of the command line prompt is changed accordingly. 
 
(Env_py3.4) E: \ env_py3.4> Scripts \ deactivate 
 
E: \ env_py3.4> 
 
  
 
Install the new python software package in the virtual environment. It is the same as normal python, and pip, easy_install, and other tools are included in the virtual environment for convenient use. 
Virtualenvwrapper  
Virtualenvwrapper is a Virtualenv extension that makes it easier to manage virtual environments. Virtualenvwrapper provides the following functions: 
 
L integrate all virtual environments into one directory. 
 
L manage (add, remove, and copy) all virtual environments. 
 
L you can use a command to switch the virtual environment. 
 
L Tab to complete the name of the virtual environment. 
 
L each operation provides a user-defined hooks. 
 
L you can write an extension plugin system that is easy to share. 
Install  
Install virtualenvwrapper Through pip or easy_install 
 
 
  
   
   | Pip install virtualenvwrapper  | 
 
  
 
By default, virtualenvwrapper is installed under/usr/local/bin. In fact, you need to run the virtualenvwrapper. sh file. 
 
  
 
Note: virtualenvwrapper is a set of shell functions defined in Bourne shell compatible syntax. So in the window platform, is not used, but there is a virtualenvwrapper-win version (https://pypi.python.org/pypi/virtualenvwrapper-win ). 
 
Virtualenvwrapper-win will be installed under the python root directory, such as D: \ Python34. 
 
# using pip
 
 
pip install virtualenvwrapper-win
 
 
 
   
 
 
# using easy_install
 
 
easy_install virtualenvwrapper-win
 
 
 
   
 
 
# from source
 
 
git clone git://github.com/davidmarble/virtualenvwrapper-win.git
 
 
cd virtualenvwrapper-win
 
 
python setup.py install
 
 
  
Use  
Virtualenvwrapper-win and virtualenvwrapper are used in the same way: 
 
L list Virtual Environments: workon or lsvirtualenv 
 
L create a virtual environment: mkvirtualenv [Virtual Environment name] 
 
L start/switch the virtual environment: workon [Virtual Environment name] 
 
L delete a virtual environment: rmvirtualenv [Virtual Environment name] 
 
L leave the virtual environment: deactivate. 
 
For example, I have created three virtual environments. 
 
....... 
 
(Vn_py3.4_app2) D: \ Python34>Mkvirtualenv. bat vn_py3.4_app3 
 
Using base prefix 'd: \ python34' 
 
New python executable in vn_py3.4_app3 \ Scripts \ python.exe 
 
Installing setuptools, pip... done. 
 
  
 
(Vn_py3.4_app3) D: \ Python34>Workon. bat 
 
  
 
Pass a name to activate one of the following virtualenvs: 
 
========================================================== ============================ 
 
Vn_py3.4_app1 
 
Vn_py3.4_app2 
 
Vn_py3.4_app3 
 
The default Virtual Environment path is % USERPROFILE % \ Envs, that is, C: \ Users \ User Name \ Envs. 
 
Switch the virtual environment: 
 
(Vn_py3.4_app3) D: \ Python34>Workon. bat vn_py3.4_app1 
 
(Vn_py3.4_app1)D: \ Python34>Workon. bat vn_py3.4_app2 
 
(Vn_py3.4_app2)D: \ Python34> 
 
  
 
Note: If the windowRun the following command D: \ Python34 \ Scripts> mkvirtualenv. bat F: \ evn_py3.4_app1: 
 
Python.exe: can't open file 'd: \ Python34 \ Scripts \ virtualenv-script.py ': [Errno 2 
 
] No such file or directory, Modify mkvirtualenv. batPython.exe "% PYHOME % \ Scripts \ virtualenv-script.py" % ARGS % =Export python.exe "% PYHOME % \ Scripts \ virtualenv.exe" % ARGS %You can. 
Summary  
Based on virtualenv + virtualenvwrapper, Environment isolation can be well completed to ensure that the environment for each application is clean. In addition, for a clean environment, you can use: 
 
Pip freeze> requirements.txtKeep the package dependency information in the requirements.txt File 
 
Pip install-r requirements.txtAll packages are automatically downloaded and installed from the Internet. 
 
This facilitates application deployment and distribution. next article about pip software package management. 
 
  
 
Related connections: 
 
Https://virtualenv.pypa.io/en/latest/index.html 
 
Http://virtualenvwrapper.readthedocs.org/en/latest/index.html 
 
Https://pypi.python.org/pypi/virtualenvwrapper-win