Python's virtual environment and multiple versions of the weapon ─virtualenv and Pythonbrew__python

Source: Internet
Author: User
Tags curl prepare windows support virtual environment virtualenv

This article is reproduced to: http://www.openfoundry.org/tw/tech-column/8516- Pythons-virtual-environment-and-multi-version-programming-tools-virtualenv-and-pythonbrew

Both Virtualenv and pythonbrew are tools that can create virtual (stand-alone) Python environments, but are different from virtual (stand-alone) goals.

Virtualenv can separate the library requirements for different cases so that they don't affect each other. After creating and activating the virtual environment, the PIP-mounted suite is placed in a virtual environment, and a dedicated case can have a unique environment.

In short, virtualenv can help you: Install a new suite without the right circumstances. Different cases can be upgraded with different versions of the same suite version, and it doesn't affect other cases.

Pythonbrew can install multiple python at home, and quickly switch versions, or you can test your Python program in a specified Python version, and you'll also integrate virtualenv.

This article will detail these two tools to make you more attentive in the context of many people developing and multiple versions of the environment.

Prepare beforehand

Python's package are usually uploaded to PyPI, and many tools are available to install package from PyPI. The following tool (provided by Setuptools) is used in Easy_install to install Virtualenv and pythonbrew. Linux

If you do not know easy_install or have not yet installed Setuptools, in 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 *.exe format files from Setuptools's homepage. After installation, you can find Easy_install.exe under C:\PythonX.Y\Scripts\ (x.y is the Python version). Remember to put this path into the path of the WINDOWS environment variable.

Then you can easily install any Python Package in PyPI. virtualenv-virtual Python Environment Builder 01. Installation

Pythonbrew has integrated the virtualenv, and if you do not want to install a kit, you can not install virtualenv.

If installation is required, enter the following instructions in the command line mode:

# Easy_install Virtualenv

02. How to use I. Creating a virtual Environment

Enter the following instructions in the command line mode:

$ virtualenv [name of the specified virtual environment]

For example, the following instructions create a virtual environment called "ENV":

$ virtualenv ENV

When creating a virtual environment, you will rely on the site packages in the system environment, and if you want to completely packages the system, add the parameters--no-site-packages to create the virtual environment:

$ virtualenv--no-site-packages [name of the specified virtual environment]
Ii. Activating the virtual environment

Please switch to the previous record to create the virtual environment. In the preceding precedent, the name "ENV" is established:

$ CD ENV

Then, to activate the virtual environment:

$ source Bin/activate

In Windows environment, instead:

> \path\to\env\scripts\activate.bat

You can then note that there is a virtual environment name hint at the front of the shell prompt:

(ENV) ... $
Iii. exit from the virtual environment

Enter the following instructions in the command line mode:

$ deactivate

We can go back to the original Python environment of the system. Iv. Installing a new Python suite in the virtual environment

Virtualenv the Python kit installation with PIP when the virtual environment is being inspired, the package installed by it will appear in the Virtual Environment folder, using the following information in the context of the installation:

(ENV) ... $ pip install [suite name]

If the system also has an installation PIP, please pay special attention to whether the virtual environment has been enabled, or the package will be installed in the system, not in the virtual environment.

If you want to avoid the PIP being used when it is not in the virtual environment, add the following ~/.BASHRC:

Export Pip_require_virtualenv=true

Require the PIP to be executed in the virtual environment.

You can also use the following settings to allow the system's PIP to automatically activate the virtual environment.

Export Pip_respect_virtualenv=true

Avoid accidental installation of the kit into the system environment. v. Specify the function library to use the virtual environment from the program

It's not possible to start a virtual environment from the Shell, like using Mod_python or MOD_WSGI, which can be added to the Python program:

Activate_this = '/path/to/env/bin/activate_this.py '
execfile (activate_this, Dict (__file__=activate_this))

To use an packages in the virtual environment. 03. Extension Kit: Virtualenvwrapper

Virtualenvwrapper is a virtualenv extension that makes the management of virtual environments easier.

In detail, Virtualenvwrapper offers the following features: Integrating all the virtual environments into one. Manage (Add, remove, duplicate) all virtual environments. You can use a command to swap the virtual environment. Tab to fill the name of the entire virtual environment. Each operation provides a user-customized hooks. Can write the extension plugin system that is easy to share. I. installation

Enter the following instructions in the command line mode:

# Easy_install Virtualenvwrapper
Ii. methods of Use

In $WORKON _home to 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 a help information.

To remove the virtual environment:

$ rmvirtualenv Envname

Duplicate Virtual Environment:

$ cpvirtualenv Envname Targetenvname

To activate the virtual environment:

$ Workon [Environment_name]

If you only enter Workon, you will list all the virtual environments.

Using deactivate is the way to leave the virtual environment.

You can use the following settings to tell Pip Virtualenv's path.

Export pip_virtualenv_base= $WORKON _home

Virtualenvwrapper's function is, of course, more versatile and can refer to Virtualenvwrapper's official documents. Pythonbrew 01. Installation

Pythonbrew is a newer and more specialized case, though relatively new, and very complete. It also has the virtualenv of integrating the above introductions. You can manipulate virtualenv in a similar virtualenvwrapper way.

Installed in the same way as virtualenv, as long as the input of the following instructions can be:

# Easy_install $ pythonbrew

Pythonbrew is officially a recommended installation, but this teaching is for consistency, and it's not an extra introduction, it can be a reference to Pythonbrew/readme.rst.

Unfortunately for Windows users, Pythonbrew temporarily does not have a plan to support Windows (#6: Windows Support?-Issues-utahta/pythonbrew-github). So Windows doesn't have time to use Pythonbrew.

After Easy_install's installation, you also need to perform a shell:

$ pythonbrew_install

To configure your initial settings and folders into your home catalogue. Then you want to modify the configuration of the ~/.BASHRC:

$ echo "Source ~/.PYTHONBREW/ETC/BASHRC" >> ~/.BASHRC

This way, even if it's installed completely.

Pythonbrew use curl to crawl data and if your system is not there, remember to install it. This line of instructions can be used on Ubuntu:

$ sudo apt-get Install curl
02. Prepare before translating

Because the pythonbrew takes off the Tarball and the method of translating and installing it, we have to first prepare the kit for Python for the system.

And because so many Linux releases are packaged in Python, we can steal lazy and use the packaged suite to resolve the dependencies required for translation. On the Ubuntu/debian, you can through:

$ sudo apt-get build-dep python2.7

To install all the kits needed to translate Python 2.7. Although it has been able to be fully installed, it still lacks the gdbm module, which, if needed, can be passed through:

$ sudo apt-get build-dep python-gdbm

To install the kit required by GDBM.

Registration: bsddb185, Linuxaudiodev, Ossaudiodev, Sunaudiodev, etc. are still missing module after being installed in the above manner. The Ossaudiodev (Open Sound System) is available in Python in Ubuntu, and is listed for your reference.

The

Fedora/centos/redhat/opensuse can use the  yum-builddep  directive. 03. Use Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.