Python Version Management with Pyenv


pyenv is a replacement for pythonbrew, which was previously used to manage Python installations on and is documented here.

Installing Pyenv

Simply clone the git repository where you want it installed. It is currently installed in the home directory in a hidden directory called .pyenv

git clone git:// .pyenv

Then you have to include it in your .profile file.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile

echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

This will not take effect until you refresh your shell - e.g. by logging out of and logging back in again. You can also

Using Pyenv

Documentation for pyenv is somewhat lacking at the time of writing, and most of what can be found online is on the homepage.

Some useful commands include:

  1. pyenv install {version}

Install a python version (e.g. pyenv install 2.7.5)

  1. pyenv shell {version}

This switches between Python versions

  1. pyenv shell --unset

Takes you back to the system Python

  1. pyenv local {version}

Marks the current directory as set up to use a specific version

  1. pyenv shell {virtualenv}

Activates a virtual environment (provided you have the extension sinstalled - see next section)

Vitualenv Extension

There is an extension for creating and managing virtual environments. The simple one-line install is:

git clone git:// ~/.pyenv/plugins/pyenv-virtualenv

Note that this assumes that you installed pyenv in ~/.pyenv, which is the default. If you didn't, you'll need to adjust.

Some useful commands include:

  1. pyenv virtualenv 2.7.5 myenv

This will create a virtualenv called myenv using version 2.7.5 (assuming it's installed)

  1. pyenv shell myenv

Switch to virtualenv myenv

  1. pyenv virtualenvs

List virtual environments. An asterisk will appear next to the one currently being used (if any)

Installaton Pitfalls

While pyenv itself install smoothly, installing individual pythons on 1and1 is frought with difficulties. The following steps were ncessary to get it working:

  1. set $TMPDIR to ~/tmp

pyenv uses /tmp by default, and the hosting provider has noexec set on that directory

export $TMPDIR=/kunden/homepages/43/d109612362/htdocs/tmp

  1. set $PYTHON_BIN to /usr/bin/python

pyenv runs some sanity checks post-install, and in some cases it won't find what it's looking for even if they're there. In particular, readline and sqlite3. Fixing this variable will solve some, but not all, of these problems. In the case of readline, you probably need to go comment out the section of ~/.pyenv/plugins/python-build/bin/python-build that contains the sanity check - which at the time of writing is a function called build_package_verify_readline. Comment out the internals and replace with an echo command (or something equally harmless - bash shells do not allow noop functions, so you have to put something there.)

export $PYTHON_BIN=/usr/bin/python

(3. At the time of writing, no adequate solution has been found fo (2), so we're still using pythonbrew. A promising path is here:

Note that sqlite3 is installed on the server because the main site is using it!)