Virtualenv Quick Start Guide
1. virtualenv Overview
virtualenv
Create an independent Python development environment tool to solve the dependency, version, and indirect permission issues. For example, if a project depends on Django1.3 and the current global development environment is Django1.7 and the version span is too large, the project cannot be running due to incompatibility. Using virtualenv can solve these problems.
virtualenv
Create an environment with its own installation directory. This environment does not share libraries with other virtual environments, allowing you to conveniently manage python versions and python libraries.
2. Install Virtualenv
Usepip
Install Virtualenv
$ Pip install virtualenv $ sudo pip install virtualenv # Or use sudo to temporarily escalate permissions due to permission issues
3. virtualenv uses 3.1 to create a python environment using virtualenv
$ Virtualenv test # create a directory named test, install bin/python and bin/pip, and create lib, include, bin directory Using base prefix'/usr' New python executable in/home/zhangchengfei/test/bin/python3 Also creating executable in/home/zhangchengfei/test/bin/python Installing setuptools, pip, wheel... done. $ cd test $ ll drwxrwxr-x 2 contains zhangchengfei 4096 May 24 19:37 bin/drwxrwxr-x 2 contains records 4096 May 24 19:37 include/drwxrwxr-x 3 zhangchengfei 4096 May 24 19:37 lib/-rw -rw-r -- 1 zhangchengfei 60 May 24 19:37 pip-selfcheck.json
lib
All installed python libraries will be placed under lib/pythonx. x/site-packages/in this directory.
bin
, Bin/python is the python interpreter used in the current environment
If you runvirtualenv --system-site-packages test
, Will inherit/usr/lib/python2.7/site-packages
The latest version of virtualenv takes the access to the global site-packages as the default action.
3.2 activate virtualenv
Run the following command in the test directory:
$ Source./bin/activate # activate the current virtualenv (test) $ # note that the terminal has changed
Usepip
View the current library, display dependencies, and generate the requirement File
(Test) $ pip listpip (8.1.2) setuptools (21.2.1) wheel (0.29.0) # currently only the three pip freeze # Show All dependent pip freeze> requirement.txt # generate the requirement.txt file pip install-r requirement.txt # generate requirement.txt to generate the same environment
3.3 disable virtualenv
3.4 specify the python version
Available-p PYTHON_EXE
Specifies the python version when creating a virtual environment.
# Create a python2.7 Virtual Environment $ virtualenv-p/usr/bin/python2.7 test Running virtualenv with interpreter/usr/bin/python2.7 New python executable in/home/tests/test/bin/python2.7 not overwriting existing python script/home/zhangchengfei/test/bin/python (you must use/home/zhangchengfei/test/bin/python2.7) installing setuptools, pip, wheel... done. # create a python3.4 Virtual Environment $ virtualenv-p/usr/local/bin/python3.4 test Running virtualenv with interpreter/usr/bin/python3.4 Using base prefix'/usr 'new python executable in/ home/zhangchengfei/test/bin/python3.4 Also creating executable in/home/zhangchengfei/test/bin/python Installing setuptools, pip, wheel... done.
4. Generate a packable Environment
In some special requirements, there may be no network. We hope to package a test directly, which can be decompressed and used directly at this time.virtualenv -relocatable
Command to change test to a test
# Change the created virtual environment to [test] $ virtualenv -- relocatable for migration. /Making script/home/zhangchengfei/test/bin/wheel relative Making script/home/zhangchengfei/test/bin/pip3 relative Making script/home/zhangchengfei/test/bin/pip relative making script/home/zhangchengfei/test/bin/easy_install-3.4 relative Making script/home/zhangchengfei/test/bin/pip3.4 relative Making script/home/zhangchengfei/test/bin/python-config relative Making script/home/zhangchengfei/test/bin/easy_install relative
[End]
Virtualenv create a virtual environment to install Flask
Python virtual environment: Virtualenv
Use Virtualenv in CentOS6.5 to build the Python3 Development Environment
This article permanently updates the link address: