Virtualenv is used to create multiple standalone Python runtime environments on a single machine, Virtualenvwrapper provides some handy package on the command line.
Reasons to use Virtualenv:
- A third-party package dependency between quarantined items, such as a project dependent Django1.2.5,b project relies on django1.3.
- To facilitate the deployment of applications, the development environment of the virtual environment packaged into a production environment, do not need to be on the server to toss another turn.
Instructions for use:
Installation: sudo easy_install virtualenv
Create a new operating environment: Virtualenv <env-name>
Enter the appropriate standalone environment: source <env-path>/bin/activate
Recently inadvertently saw a book "Python testing Cookbook", just as our project recently to improve the quality of testing, "turn" to see. Some handy tools, such as virtualenv, are introduced in the book.
The virtualenv makes it easy to create an isolated python environment, known as a sandbox. The benefits of sandboxing include:
- Resolve version dependencies between libraries, such as different applications on the same system that depend on different versions of the same library.
- Resolve permissions restrictions, such as you do not have root privileges.
- Try new tools without worrying about polluting the system environment.
Of course, virtualenv still needs to be installed into the system environment, through Easy_install. Then you can create the sandbox:
$virtualenv sandbox name (e.g. Mysandbox)
This command creates a directory Mysandbox, and all the contents of the sandbox are in that directory. There is a custom Python interpreter in the Mysandbox/bin directory, which takes precedence over libraries in the Mysandbox/lib/pythonx.x/site-packages directory. The libraries installed through the interpreter are placed in the above directory.
For ease of use, Virtualenv also provides two scripts in the Mysandbox/bin directory:
$source mysandbox/bin/activate: Enables the specified sandbox. It just changes the path of the python in the environment variable path. You will notice that the console prefix becomes (mysandbox) $, prompting you to have entered the sandbox environment.
$source mysandbox/bin/deactivate: Closes the specified sandbox.
The Sandbox interpreter will first look for the sandbox's site-packages directory, and if not, it will look for the system's Site-packages directory. We can add the parameter--no-site-packages when creating the sandbox to prevent it from finding the directory of the system.
Virtualenv's author also selflessly recommended another powerful sandbox tool, Zc.buildout.
"I strongly recommend anyone doing application development or deployment use one of the these tools."
The authors are Mosky virtualenv and pythonbrew are tools that can create a virtual (separate) Python environment, but are different from virtual (independent) goals.
Virtualenv can separate the library to require different cases, so that they do not affect each other. After the creation and activation of the virtual environment, the installation will pip
be placed in a virtual environment, the case can have a unique environment.
In simple terms, virtualenv can help you:
- Install a new kit without any restrictions
- Different versions of the same suite can be used in various cases
- The suite version does not affect other cases when it is upgraded
Pythonbrew can install multiple Python at home and quickly switch versions, or you can test your Python program in batches under the specified Python version, and also integrate virtualenv.
This article will detail these two tools to make you more attentive in the environment where multiple people develop and develop multiple versions.
Pre-Prepare
Python's package is usually spread to PyPI, and many tools can be installed from the PyPI . This tool will be used easy_install
(provided by Setuptools ) to install Virtualenv and pythonbrew.
Linux.
If Setuptools is not known or is not installed easy_install
, the Debian/ubuntu can be installed with the following instructions:
$ sudo apt-get install python-setuptools
In Fedora/centos/redhat/opensuse, you can use:
$ su -# yum install python-setuptools
. Windows
In Windows, you can find the *.exe
format of the archive from the Setuptools page. After the installation, you can C:\PythonX.Y\Scripts\
find it in (X.y is a Python version) easy_install.exe
. Remember to put this path in the Windows environment change path.
Then you can easily install any Python package in PyPI .
Virtualenv-virtual Python Environment Builder01. Installation
Pythonbrew has integrated the virtualenv, if you do not want to install a kit, you can also do not install virtualenv.
If an installation is required, enter the following commands in the command-line mode:
# easy_install virtualenv
02. Use method I. Create a virtual environment
Please enter the following commands in the command-line mode:
$ virtualenv [指定虛擬環境的名稱]
For example, the following command creates a virtual environment called "ENV":
$ virtualenv ENV
When setting up a virtual environment, the site packages in the system environment, if you want to completely not rely on the system of packages, you can add the parameters to establish a --no-site-packages
virtual environment:
$ virtualenv --no-site-packages [指定虛擬環境的名稱]
Ii. activating the virtual environment
Please first switch to the virtual environment you created. In the preceding precedent, the establishment is called "ENV", which:
$ cd ENV
Then, activate the virtual environment:
$ source bin/activate
In Windows environment, use:
> \path\to\env\Scripts\activate.bat
Then you can notice that at the very front of the shell prompt, there is a pseudo-environment name hint:
(ENV) ...$
Iii. exiting the virtual environment
Please enter the following commands in the command-line mode:
$ deactivate
You can return to the system's original Python environment.
Iv. Installing a new Python suite in a virtual environment
Virtualenv is attached with pip
this Python kit installation tool, and when the virtual environment is initiated, the package that is installed will appear in the Virtual Environment folder, using the following:
(ENV)...$ pip install [套件名稱]
If the system also has an installation pip
, please be aware that the virtual environment has been initiated, or the suite will be installed in the system, not in the virtual environment.
If you want to avoid pip
being used without entering a virtual environment, you can ~/.bashrc
add:
export PIP_REQUIRE_VIRTUALENV=true
Requirements pip
must be done in a virtual environment.
You can also use the following settings to allow the system to pip
auto-activate the virtual environment.
export PIP_RESPECT_VIRTUALENV=true
Avoid accidentally installing the kit into a system environment.
V. Specifying the use of virtual environments from the program
There is no way to activate a virtual environment from the Shell, like using mod_python or mod_wsgi, which can be added in Python's program:
activate_this = ‘/path/to/env/bin/activate_this.py‘execfile(activate_this, dict(__file__=activate_this))
To use the packages installed in a virtual environment.
03. Extension Kit: Virtualenvwrapper
Virtualenvwrapper is a virtualenv extension that makes virtual environments easier to manage.
In detail, Virtualenvwrapper provides the following features:
- Integrate all of the virtual environments into one catalogue.
- Manage (Add, remove, copy) all your virtual environments.
- You can use a single command to switch between virtual environments.
- The name of the Tab-full virtual environment.
- Each operation is provided with a hooks that allows users to customize.
- Extension plugin system that can be easily shared.
I. Install
Please enter the following commands in the command-line mode:
# easy_install virtualenvwrapper
II. How to use
To $WORKON_HOME
make a virtual environment:
$ mkvirtualenv [-i package] [-r requirements_file] [virtualenv options] ENVNAME
List all the virtual environments:
$ lsvirtualenv [-b] [-l] [-h]
-b
is a short mode, -l
is a detailed mode (preset), -h
is printed help information.
To remove a virtual environment:
$ rmvirtualenv ENVNAME
To reproduce the virtual environment:
$ cpvirtualenv ENVNAME TARGETENVNAME
To activate the virtual environment:
$ workon [environment_name]
If you enter only workon
, all the virtual environments will be listed.
It is used in a virtual environment deactivate
.
You can use the following settings to tell pip
virtualenv the path.
export PIP_VIRTUALENV_BASE=$WORKON_HOME
Virtualenvwrapper's function is more than that, more versatility can be virtualenvwrapper official documents .
Pythonbrew01. Installation
Pythonbrew is a relatively new case, although relatively new, but very complete. It also has the virtualenv to integrate the above introduction. The virtualenv can be operated in a similar virtualenvwrapper manner.
As with virtualenv, just enter the following instructions:
# easy_install $ pythonbrew
Pythonbrew official has recommended the installation method, but this teaching for consistency, not an extra introduction, you can refer to pythonbrew/readme.rst.
Unfortunately for Windows users, Pythonbrew does not have a project to support Windows (#6: Windows Help-Issues-utahta/pythonbrew-github). So Windows has no way to use Pythonbrew.
easy_install
after the installation, you also need to execute the shell:
$ pythonbrew_install
To configure the initial configuration files and folders into your home catalogue. Then the configuration to be modified ~/.bashrc
:
$ echo "source ~/.pythonbrew/etc/bashrc" >> ~/.bashrc
So even if the installation is complete.
Pythonbrew use to curl
crawl the information, if your system does not have, please remember to install. This line of instructions can be used on Ubuntu:
$ sudo apt-get install curl
02. Pre-Prepare the translator
Because Pythonbrew took off the tarball and programmed it, we wanted to prepare the kit for the system to compile the Python.
And because many Linux distributions have been packaged in Python, we can steal lazy and use packaged packages to resolve the dependencies required for editing. On the Ubuntu/debian, you can pass:
$ sudo apt-get build-dep python2.7
To install all the required packages for the translation of Python 2.7. Although it is already fully installed, it lacks gdbm
this module and, if needed, can pass through:
$ sudo apt-get build-dep python-gdbm
gdbm
to install the required kit for the editing.
Note:,,, and bsddb185
linuxaudiodev
so on are installed in the ossaudiodev
sunaudiodev
above manner, will still be missing module. where ossaudiodev
(Open sound System) is available in Python with Ubuntu, listed for your reference.
Fedora/centos/redhat/opensuse can use yum-builddep
this instruction.
03. How to Use
Pythonbrew's operation is all about installing, removing, listing and using the new Python version, which is described in the order that is required for initial use.
I. Listing the versions that are available for installation
First we'll use list --know
a list of Python versions that we can install:
$ pythonbrew list --know
II. New version of the installation
Then use it install VERSION
to download and edit Python to this machine, in addition to the Python version, you can also pick up Python's tarball path or Web address, but also can be adjusted to edit the Python option. Here are some examples:
$ pythonbrew install 2.7.2$ pythonbrew install --verbose 2.7.2$ pythonbrew install --force 2.7.2$ pythonbrew install --no-test 2.7.2$ pythonbrew install --configure="CC=gcc_4.1" 2.7.2$ pythonbrew install --no-setuptools 2.7.2$ pythonbrew install http://www.python.org/ftp/python/2.7/Python-2.7.2.tgz$ pythonbrew install /path/to/Python-2.7.2.tgz$ pythonbrew install /path/to/Python-2.7.2$ pythonbrew install 2.7.2 3.2
III. Cleaning up the archives of the installation
The downloaded Python tarball will be placed ~/.pythonbrew/dists/
next, and the editing is going to be ~/.pythonbrew/build/
done. If you want to clean up these two entries, you can use:
$ pythonbrew cleanup
IV. listing all installed versions
After installation, you can use list
the command to list all installed Python versions:
$ pythonbrew list
There is a star in the back, which is now in use in the Python version.
V. Switching to an installed version
A Python version that can be used to switch
switch presets:
$ pythonbrew switch VERSION
If you want to switch only from the current shell, you can use use
:
$ pythonbrew use VERSION
To switch back to the preset environment, use off
:
$ pythonbrew off
will return to the system environment of Python.
Vi. batches tested in different versions
Most importantly, you can test your program with all the versions of Python installed in the system, or the Python version you specify!
$ pythonbrew py test.py # 使用所有有安裝的版本$ pythonbrew py -v test.py # 詳細輸出$ pythonbrew py -p 2.7.2 -p 3.2 test.py # 指定特定的版本
VII. Removal of installed versions
To remove an already installed Python, you can use uninstall
:
$ pythonbrew uninstall 2.7.2$ pythonbrew uninstall 2.7.2 3.2
Viii. Integration with Virtualenv
Note that the virtualenv provided in Pythonbrew is based on Python installed in Pythonbrew (set ~/.pythonbrew/venvs/Python-VERSION/
below). Without the use of pythonbrew, it is impossible to use the function attached to Pythonbrew venv
.
Pythonbrew provides functionality similar to Virtualenvwrapper, but does not have a complete plugin system like Virtualenvwrapper. All virtualenv directives in Pythonbrew are venv
used as the first secondary command.
$ pythonbrew venv create [指定虛擬環境的名稱]$ pythonbrew venv list$ pythonbrew venv use [指定虛擬環境的名稱]$ pythonbrew venv delete [指定虛擬環境的名稱]
It is used in a virtual environment deactivate
.
IX. buildout
If you have a tool that uses buildout , you can perform it through Pythonbrew:
$ pythonbrew buildout$ pythonbrew buildout -p 2.6.6 # 指定版本
X. Self-renewal
Finally, Pythonbrew has built-in instructions to update itself:
$ pythonbrew update$ pythonbrew update --master$ pythonbrew update --develop
Virtualenv and Pythonbrew are here, and if you want to get more information, you can take a lot of their official web. I wish you all a pleasant stay.
References
- www.virtualenv.org
- Utahta/pythonbrew-github
- Setuptools-pypi
- Pip-pypi
- Virtualenvwrapper documentaion
- Building Python and more on missing Modules-stack Overflow
Python sandbox environment--virtualenv