程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python virtual environment

編輯:Python

 ​ use pyenv and virtualenv Build a stand-alone multi version python Virtual development environment ​​

As the mainstream development language , use python More and more programs are being developed . What's convenient is that most linux The system is integrated by default python, Development can start anytime, anywhere . But sometimes it becomes a short board , For example, sometimes we need to develop and debug something that needs to be installed by default python The program in the path , Repeated modification and installation will make the system directory more and more disordered , This is not acceptable to some programmers who are obsessed with cleanliness . There are times when we need to be in different python Development on version , Generally, the system only comes with one or two types python edition , Switching is even more troublesome .

This article will introduce how to use pyenv and virtualenv Build multiple versions on a single machine python Virtual development environment .

First, let's find out pyenv and virtualenv What are they .

pyenv It can help you build multiple versions of python Environmental Science , And provide convenient switching methods .

virtualenv Provides a function , Is to create a directory as a virtual python Environmental Science , In this case , Users can create multiple virtual environments , In every environment python Versions can be different , It can be the same , And the environment is independent of each other .

If the explanation is not very clear , Let's give an example of .

First we can use pyenv Install multiple python edition , For example, it is installed 2.5, 2.6, 3.3 Three versions . The user can switch the current default python edition . 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 the environment of this version to be modified . This is the time , If we use virtual env To build a virtual environment , It can completely guarantee the cleanness of the system path . No matter what program you install in the virtual environment , Will not affect the installed version of the system environment .

Said so much , It's better to practice . The example of this article is on a clean Ubuntu On the machine

1. First we install pyenv

Because of my Ubuntu It's clean , So you need to install curl and git

sudo apt-get install curl git-core

  • 1.

Next install pyenv

curl -L https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

  • 1.

This order will put pyenv Installed to the current user's ~/.pyenv Under the table of contents .

meanwhile , We also need to save the following code to ~/.bashrc In file :

export PYENV_ROOT="${HOME}/.pyenv"


if [ -d "${PYENV_ROOT}" ]; then
export PATH="${PYENV_ROOT}/bin:${PATH}"
eval "$(pyenv init -)"
fi
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

The main purpose of this code is to indicate pyenv The location of , So that you can run it directly on the command line in the future pyenv command . Save in ~/.bashrc In the file is to take effect automatically after each user logs in .

So if you run the command for the first time and don't log off , This paragraph will not take effect , We also need to run the following command to make it effective

source ~/.bashrc

  • 1.

Then you can type... Directly on the command line pyenv 了 .

 

2. Install some necessary packages

sudo apt-get build-dep python2.7

  • 1.

  Install other versions later python You will need these bags when you need them

 

3. Install a version of python

First of all, we can see which versions of python Can install

pyenv install --list

  • 1.

give the result as follows :

2.4

2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
...
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

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

pyenv install 2.7.1

  • 1.
pyenv install 3.3.5

  • 1.

After installation , We can check the installation

pyenv versions

  • 1.

The output is as follows :

* system (set by /home/tony/.pyenv/version)

2.7.1
3.3.5
  • 1.
  • 2.
  • 3.

In short, we have installed three versions of python

system Represents the... Of the current system python edition

2.7.1 and 3.3.5 We use pyenv Installed

* Represents the current python edition , You can see , We are still using the default system Self contained python edition

 

4. Switch python Test the version

Now that you have installed two other versions of python, Let's do a test

pyenv global 2.7.1

  • 1.

This command is to change the current python Version switch to 2.7.1

Run again pyenv versions, give the result as follows :

system

* 2.7.1 (set by /home/tony/.pyenv/version)
3.3.5
  • 1.
  • 2.
  • 3.

You can see the current python The version has been switched to 2.7.1 了 , If you don't feel at ease , Direct operation ‘python’ Command to verify :

[email protected]:~$ 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.
>>>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

You can see clearly here , The current version is indeed 2.7.1

Empathy , We can continue to use python global Switch between versions , If you want to switch back to the system version , Please use :

pyenv global system

  • 1.

If you want to uninstall a version of python, It can be like this :

pyenv uninstall x.x.x

  • 1.

 

5. utilize virtualenv Create virtual python Environmental Science

Now we have installed multiple python edition , But that's not enough , Some programmers who are obsessed with cleanliness must have a clean system environment , At this time, we can use virtualenv To create a virtual python Environmental Science

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

Fortunately, , If you are installing in the way we did before pyenv Of , Then it has helped us plugin In the form of virtualenv, We just need to use it

First let's create one 2.7.1 Virtual environment for

pyenv virtualenv 2.7.1 env271

  • 1.

This command creates a command on this machine called env271 Of python A virtual environment , The real directory for this environment is located in :~/.pyenv/versions/

Be careful , In the command ‘2.7.1’ It must be installed in the previous steps python edition , Otherwise it will go wrong .

Then we can continue through ‘pyenv versions’ Command to view the current virtual environment , give the result as follows :

* system (set by /home/tony/.pyenv/version)

2.7.1
3.3.5
env271
  • 1.
  • 2.
  • 3.
  • 4.

Here we can see , In addition to the already installed python edition , We have one more env271 Of python A virtual environment

 

6. Switch and use the new python A virtual environment

The command to switch to the new virtual environment is

pyenv activate env271

  • 1.

Next, our python The environment has been switched to 2.7.1 It's a virtual environment , function ‘python’ Command authentication

(env271)[email protected]:~$ 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.
>>>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

You can see , python The version is already 2.7.1, And in a virtual environment (env271)

Basically, you can do whatever you want in this virtual environment :) Don't worry about the system path being messed up any more .

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

pyenv deactivate

  • 1.

What if you want to delete this virtual environment ? The answer is simple and crude , Just delete the directory where it is located :

rm -rf ~/.pyenv/versions/env271/

  • 1.

install virtualenv and virtualenvwrapper   $sudo apt-get install python-virtualenv $sudo easy_install virtualenvwrapper   The above tools cannot be found after being installed mkvirtualenv command , You need to perform the following environment variable settings   1. Create a directory to store the virtual environment       mkdir $HOME/.virtualenvs   2. stay ~/.bashrc China Canada Bank :       export WORKON_HOME=$HOME/.virtualenvs     source /usr/local/bin/virtualenvwrapper.sh     3. function :       source ~/.bashrc   3. establish python A virtual environment   mkvirtualenv [ Virtual environment name ]   4. Switch virtual environment workon [ Virtual environment name ]     5. Exit virtual environment      deactivate   6. Delete virtual environment      rmvirtualenv [ Virtual environment name ]   notes : The created environment is independent , Mutual interference , There is no need to sudo Permission to use pip To manage packages , If used in a virtual environment sudo The installed packages are in the primary environment   Use -p Parameter specifies the virtual environment python Version of   $ mkvirtualenv -p python python2_tornado


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved