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.
(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.
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
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:
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 Ubuntu Server:
Mac OS X users can install pyenv using the Homebrew package manager:
To list all available Python versions:
To list the installed Python versions:
Note: Virtual environments will also show up as versions after they’re added.
To install a new Python version, execute:
To set the global Python version used for your account, use:
Can use pyenv to create virtualenvs:
$ pyenv virtualenv test_venv
For activation or de-activation virtualenv, execute:
$ pyenv activate test_venv
To list all the virtualenvs you have created, you may use: