pyenv – managing multiple Python versions

You may be a mid-level Python programmer using Python 2.7.5 for sometime, but also curious about Python 3.x.x, or check for yourself what is this all the Python 2 vs 3 debate over Internet, or simply the new functionalities or conventions being introduced in Python 3.x.x. or, just a Python newbie starting off and all so confused given the Python version debate. Don’t worry, my friends you have been rescued – you can keep multiple versions of Python (as many as you like).
There may be lot ways to install multiple Python versions, in the post I’ll be focusing on doing it using penv. – as it’s the most elegant way to do so.


pyenv

(previously known as pythonbrew)- to put it simply, it’s Python version management tool. penv lets you have multiple Python installations i.e. multiple Python versions, from which you can set the global Python version (the default version to use), and also the local Python version i.e. project specific. Apart from this you can also create virtualenv – it has a separate sub-command for it.

Once installed, penv is a bash command not needing python to for execution – it’s user-level command, so no need to use sudo.

The dependences

presuming you’re installing it on a fresh CentOS 7 machine – in case you have them installed, start with the installation step (anchor here).
$ sudo yum -y install epel-release
$ sudo yum install git gcc zlib-devel bzip2-devel readline-devel sqlite-devel openssl-devel
If you’re using Ubuntu Server, install all build dependencies like this:
$ sudo apt-get install curl git-core gcc make zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libssl-dev

Installation:

Now install pyenv as a regular user:
$ curl -L https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
At the end of the installation you’ll get a warning:
WARNING: seems you still have not added 'pyenv' to the load path.

# Load pyenv automatically by adding
# the following to ~/.bash_profile:

export PATH="/home/nahmed/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
You must add the above 3 lines at the end of your ~/.bash_profile (~/.profile in case of Ubuntu)- can use any editor i.e. gedit or if you’re familiar with the command-line use vi or vim, or can add them executing the following commands:
$ echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
Once the above lines are added, restart your shell or simply reload the profile:
On CentOS:
$ source ~/.bash_profile
On Ubuntu Server:
$ source ~/.profile
 pyenv install
Mac OS X users can install pyenv using the Homebrew package manager:
$ brew update
$ brew install pyenv

Using pyenv:

To list all available Python versions:
$ pyenv install -l
To list the installed Python versions:
$ pyenv versions
Note: Virtual environments will also show up as versions after they’re added.
To install a new Python version, execute:
$ pyenv install x.x.x 
e.g.
$ pyenv install 3.4.1
To set the global Python version used for your account, use:
$ pyenv global 3.4.1
Can use pyenv to create virtualenvs:
$ pyenv virtualenv test_venv
For activation or de-activation virtualenv, execute:
$ pyenv activate test_venv
$ pyenv deactivate
To list all the virtualenvs you have created, you may use:
$ pyenv virtualenvs

The full list of pyenv commands. – https://github.com/yyuu/pyenv/blob/master/COMMANDS.md

Leave a Reply

Your email address will not be published. Required fields are marked *