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

Source: Internet
Author: User
Tags virtual environment 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

1 sudo apt-get install curl git-core

Next Install Pyenv

1 Curl 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:

123456 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.

1 SOURCE ~/. BASHRC

Then you can knock pyenv directly in the command line.

2. Install some necessary packages

1 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

1 Pyenv Install --list

The results are as follows:

123456789 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5 . . .

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

1 Pyenv Install 2.7.1

1 Pyenv Install 3.3.5

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

1 Pyenv versions

The output results are as follows:

123 * 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.

1 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:

123   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:

12345 Tony@ubuntu:~$ python python 2.7.1 (r271:86832, may  9 2014 01:07:17) [GCC 4.8.2] on linux3 Type "Help", "Copyright", "credits" or "license" For more information. >>>

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:

1 Pyenv Global system

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

1 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

1 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:

123 * system (set by /home/tony /. Pyenv/version)   2.7.1   < Span class= "CRAYON-CN" >3.3.5<br>< Span class= "crayon-v" >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

1 Pyenv Activate env271

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

12345 (env271) tony< Span class= "Crayon-sy" >@ubuntu:~$ python python 2.7.1 (r271:86832, may  9 2014 01:07:17) [GCC 4.8.2] on linux3 Type "Help", "Copyright", "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

1 Pyenv Deactivate

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

1 RM -RF ~/. Pyenv/versions/env271/

It's done.

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

Related Article

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.