Sometimes it is useful to keep multiple versions of python on the same machine. Suppose for the development of two projects, we need the second and third branch of python. Or do you support a project that uses the old version of python.
Usually for this we use the virtual environment
virtualenv or the wrapper for it
virtualenvwrapper . I will not talk about this, as there are already many similar articles, and the documentation for the utilities themselves is very well explained. It is enough to score
virtualenv or
virtualenvwrapper in a search engine.
But in addition to them, I want to tell you in this article about the python version manager. Who is curious to ask under the cat.
To use multiple versions of python, you can install them manually or use the version manager. There are two such:
pythonbrew (which
no longer develops ) and
pyenv . Both managers do not support windows (
pythonbrew ,
pyenv ), so that Pythonists writing on this platform will have to handle everything for the time being, or make their own utility for changing paths to the correct versions. Who can cope with this situation in the comments.
Since
pythonbrew is no longer supported in this article, it will not be covered.
PS The article contains examples verified for
OS Ubuntu 12.04. When you try to repeat them, make amendments regarding your distribution.
')
Manual mode
In order to work with several versions of Python, you can install the necessary versions in the specified prefix. For example, in order not to be wise with rights, we will install an additional 2 versions of python (2.7.6 and 3.3.2) in the user directory:
2.7.6$ mkdir -p ~/python/src/ && cd ~/python/src/ $ wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz $ tar -xf ~/python/src/Python-2.7.6.tar.xz && cd ./Python-2.7.6 $ ./configure --prefix=$HOME/python/2.7.6/ $ make && make install
for
3.3.2 we do the same operations:
$ wget http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz ~/python/src/ $ tar -xf ~/python/src/Python-3.3.2.tar.xz && cd ./Python-3.3.2 $ ./configure --prefix=$HOME/python/3.3.2/ $ make && make install
Now you can create a virtual environment to use these versions:
$ virtualenv -p ~/python/2.7.6/bin/python env && . ./env/bin/activate
or through virtualenvwrapper:
$ mkvirtualenv -p ~/python/2.7.6/bin/python evnwrapper
Actually, based on this method, an article on
creating a multi-hosting is described.
Further, if you need to use one of these versions as python by default, then you need to add the path to the python interpreter to the environment variable.
$ echo 'export PATH=~/python/2.7.6/bin/' >> ~/.bashrc
Accordingly, instead of bashrc, you set bash_profile, zshrc, kshrc, profile depending on your command shell.
$ . ~/.bashrc
And if necessary, you can
install pip , after installing
setuptools .
$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python $ wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py -O - | python
Fuh, well, everything seems to be. And now about how to make it easier using the python version manager.
Pyenv
In general, if you are lazy enough, then you can not do all the things described above and use the pyenv utility, which will simplify this interaction with your environment and ways.
So what is the feature of this utility? Here's what she can according to the project author:
- Let you change the global Python version on a per-user basis.
- Provide support for per-project Python versions .
- Allow you to override the Python version with an environment variable.
- Search commands from multiple versions of Python at a time . This may be helpful to test across Python versions with tox .
By default, all versions of Python will be available in
~/.pyenv/versions/
. You can change Python versions both in the global context and in the local (for example, for a specific project).
How to put pyenv well described in the
instructions . Also, the author has a
script that by itself pyenv puts more plug-ins, including for
virtualenv . It is possible to install a plugin for
virtualenvwrapper .
Before installing, you must
put some dependencies :
Before you start the installation, make sure you have git installed:
Next, install the
instructions :
$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
Or so:
$ curl https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
In the second case, the installation will happen with additional plugins.
Next, in order for everything to work, let's add our bashrc and reload the shell:
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc $ echo 'eval "$(pyenv init -)"' >> ~/.bashrc $ . ~/.bashrc
To update the utility or change its version use git.
InstructionTo manage
pyenv versions, go to the directory with the utility:
$ cd ~/.pyenv
To view available versions:
$ git tag
to change version
$ git checkout <version>
for update
$ git pull
Usage example
~ $ pyenv install 2.7.5 ~ $ pyenv install 3.3.2 ~ $ pyenv rehash ~ $ pyenv versions * system 2.7.5 3.3.2 ~ $ pyenv global 2.7.5 ~ $ python --version Python 2.7.5 ~ $ cd projects/ ~/projects $ pyenv local 3.3.2 ~/projects $ python --version Python 3.3.2 ~/projects $ cd test_prj/ ~/projects/test_prj $ python --version Python 3.3.2 ~/projects/test_prj $ cd .. ~/projects $ pyenv local --unset ~/projects $ python --version Python 2.7.5
In addition, everything is quite detailed and detailed with the author of the project in his
repositories on github .
Virtual environment
Everything, and then whatever you want. If you are using 3 python branches, then to create a virtual environment, you can use the
venv utility
, which works out of the box. About this there is
an article on Habré. If you are more accustomed to
virtualenv or its
virtualenvwrapper wrapper, then there are two options: either install a plugin for
pyenv , or use them for the version of python you work with. Accordingly, if you choose the first option, then the environments you created will be added to your python versions and accessible via the command:
$ pyenv versions
Adding a plugin is easy, just clone it from the
pyenv-virtualenv or
pyenv-virtualenvwrapper repository:
$ mkdir -p ~/.pyenv/plugins $ git clone git://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv $ git clone git://github.com/yyuu/pyenv-virtualenvwrapper.git ~/.pyenv/plugins/pyenv-virtualenvwrapper
An example of use can be found in the documentation for
pyenv-virtualenv and
pyenv-virtualenvwrapper .
All, and then use, as you like.
Usage example
$ pyenv versions * system 2.7.5 3.3.2 $ mkdir -p ~/test_project/prj_for_2.7.5 && cd ~/test_project/prj_for_2.7.5 $ pyenv virtualenv 2.7.5 my-virtualenv-2.7.5 $ pyenv local my-virtualenv-2.7.5 $ pip install django==1.4 $ pip freeze Django==1.4 wsgiref==0.1.2 $ python --version Python 2.7.5 $ mkdir -p ~/test_project/test_project && cd ~/test_project/test_project $ pyenv virtualenv 3.3.2 my-virtualenv-3.3.2 $ pyenv local my-virtualenv-3.3.2 $ pip install django==1.5 $ pip freeze Django==1.5 $ python --version Python 3.3.2
Now being in the project directory, you can run a script from the desired version of python without attaching any action.
pyenv creates a
.python-version file in the directory which contains information about which version of python with which environment to use for this project.
useful links
github.com/utahta/pythonbrewgithub.com/yyuu/pyenvgithub.com/yyuu/pyenv-installergithub.com/yyuu/pyenv-virtualenvgithub.com/yyuu/pyenv-virtualenvwrapperdocs.python.org/dev/library/venv.htmlwww.virtualenv.org/en/latestvirtualenvwrapper.readthedocs.org/en/latest