Python Virtual Environment

Source: Internet
Author: User
Tags virtual environment virtualenv

Build a standalone Multi-version Python virtual development environment with PYENV and virtualenv

As a mainstream development language, more and more programs are being developed with Python. The convenience is that most Linux systems have Python integrated by default, and development can start anywhere. But sometimes this also becomes a short board, for example, sometimes we need to develop and debug some programs that need to be installed in the default Python path, repeated modification and installation will make the system directory more and more chaotic, which for some neat programmers are unacceptable. There are times when we need to develop on different versions of Python, and the system usually comes with only one or two Python versions, which can be a hassle to switch.

This article will show you how to build a multi-version Python virtual development environment using PYENV and virtualenv on a single machine.

First we figure out what pyenv and virtualenv are doing respectively.

Pyenv can help you build multiple versions of the Python environment on a single development machine and provide a convenient way to switch.

Virtualenv provides a function of setting up a directory as a virtual Python environment, so that users can create multiple virtual environments in which the Python version can be different or the same, and the environment is independent of each other.

If the explanation is not clear enough, let us give an example.

First we can install multiple Python versions with pyenv, such as installing 2.5, 2.6, 3.3 three versions. The user is free to switch to the current default Python version. But this time, each version of the environment is still unique, if we want to install some libraries in the environment, it will still cause this version of the environment to be modified. At this point, if we use virtual env to build a virtualized environment, we can fully guarantee the clean system path. No matter what program you install in the virtual environment, it will not affect the installed version of the system environment.

Having said so much, it is really a practical experience. The example of this article is done on a clean Ubuntu machine.

1. First we install the PYENV

Because my Ubuntu is clean, I need to install curl and git first

sudo apt-get Install Curl Git-core

Next Install Pyenv

Curl-l Https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | Bash

This command installs the pyenv to the current user's ~/.pyenv directory.

Also, we need to save the following code in the ~/.BASHRC file:

Export pyenv_root= "${home}/.pyenv" if [-D "${pyenv_root}"]; Then  export path= "${pyenv_root}/bin:${path}"  eval "$ (pyenv init-)" fi

The main purpose of this code is to indicate the location of the pyenv, so that you can run the pyenv command directly in the command line at a later time. Saved in the ~/.BASHRC file is automatically applied every time the user logs in.

So if you are running the command for the first time and you are not logged out, this paragraph will not take effect, and we will need to run the following command to make it effective.

SOURCE ~/.BASHRC

Then you can knock pyenv directly in the command line.

2. Install some necessary packages

sudo apt-get build-dep python2.7

You will need these packages when you install other versions of Python later

3. Install a version of Python

First we can look at what versions of Python can be installed

Pyenv Install--list

The results are as follows:

2.42.4.12.4.22.4.32.4.42.4.52.4.62.5 ...

Next, as an example we install two versions (2.7.1 and 3.3.5)

Pyenv Install 2.7.1
Pyenv Install 3.3.5

Once the installation is complete, we can check the installation situation

Pyenv versions

The output results are as follows:

* System (Set by/home/tony/.pyenv/version)  2.7.1  3.3.5

In a nutshell, we've installed three versions of Python on this machine.

System represents the Python version of the current systems

2.7.1 and 3.3.5, we installed it with pyenv.

* Indicates the current Python version, as you can see, we are still using the default system-brought Python version

4. Switch python version and do some testing.

Now that we've installed two other versions of Python, let's do a test.

Pyenv Global 2.7.1

This is the command to switch the current Python version to 2.7.1

Run pyenv versions again and the results are as follows:

  System * 2.7.1 (set by/home/tony/.pyenv/version)  3.3.5

You can see that the current Python version has switched to 2.7.1, and if you are not sure, run the ' python ' command directly to verify:

[Email protected]:~$ Pythonpython 2.7.1 (r271:86832, May  

As you can see clearly, the current version is really 2.7.1

In the same vein, we can continue to switch between versions with Python global, and if you want to switch back to the system version, use:

PYENV Global System

If you want to uninstall a version of Python, you can:

Pyenv Uninstall x.x.x

5. Create a virtual Python environment with virtualenv

Now that we have multiple versions of Python installed, but that's not enough, some neat programmers must be clean enough to use virtualenv to create a virtual Python environment

Virtualenv is an independent tool, the official website is here: https://pypi.python.org/pypi/virtualenv

Fortunately, if you are installing pyenv in front of us, then it has helped us to install the virtualenv in the form of plugin, we just need to use it.

First, we create a 2.7.1 virtual environment

Pyenv virtualenv 2.7.1 env271

This command creates a Python virtual environment named env271 on this machine, the real directory of this environment is located at: ~/.pyenv/versions/

Note that the ' 2.7.1 ' in the command must be a Python version that has already been installed in the previous step, otherwise an error will occur.

We can then continue to view the current virtual environment through the ' pyenv versions ' command, with the following results:

* System (Set by/home/tony/.pyenv/version)  2.7.1  3.3.5
env271

Here we can see that in addition to the version of Python that has been installed, we have an extra env271 python virtual environment

6. Switch and use the new Python virtual environment

The command to switch to the new virtual environment is

Pyenv Activate env271

Next, our Python environment has switched to 2.7.1 's virtual environment, running the ' Python ' command authentication

(env271) [Email protected]:~$ Pythonpython 2.7.1 (r271:86832, May  9, 01:07:17) [GCC 4.8.2] on Linux3type ' help ', ' Copyrigh T "," credits "or" license "for more information.>>>

As you can see, the Python version is already 2.7.1 and is in a virtual environment (env271)

Basically you can do whatever you like in this virtual environment:) No more worrying about the system path being messed up.

If you want to switch back to the system environment, run this command to

Pyenv Deactivate

What if you want to delete this virtual environment? The answer is simple and rude, just delete the directory where it resides:

RM-RF ~/.pyenv/versions/env271/
Install virtualenv and virtualenvwrapper $sudo apt-get install Python-virtualenv$sudo easy_install virtualenvwrapper When the above tool is installed, the mkvirtualenv command is not found and the following environment variable settings are required 1. Create a directory to hold the virtual environment mkdir $HOME/.virtualenvs 2. Add rows to ~/.BASHRC: Export workon_home = $HOME/.virtualenvs source/usr/local/bin/virtualenvwrapper.sh 3. Run: Source ~/.BASHRC 3. Create a Python virtual environment mkvirtualenv [Virtual Environment name] 4. Switch virtual environment Workon [virtual Environment name] 5. Exit the virtual Environment Deactivate 6. Delete a virtual environment rmvirtualenv [virtual Environment name] Note: The created environment is independent, non-intrusive, with no sudo permission to use PIP to package Management, if you use a sudo installed package in a virtual environment, use the-p parameter in the main environment to specify the version of Python in the virtual environment Mkvirtualenv-p python Python2_tornado

Python Virtual Environment

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.