Skip to content

Instantly share code, notes, and snippets.

@rollick
Created August 26, 2017 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rollick/24198f7328e4fd5a1d97fad41bb52806 to your computer and use it in GitHub Desktop.
Save rollick/24198f7328e4fd5a1d97fad41bb52806 to your computer and use it in GitHub Desktop.

Using Pyenv with virtualenv ( Ubuntu / Linux. Sorry Windows :-( )

This is a little guide to installing pyenv with virtualenv so that you never again need to worry about whether you your version of Python and pip packages are working. Pyenv is like virtualenv except that it lets you install any version of Python and then by using the pyenv plugin for virtualenv it is possible to create a box with the Python version you want plus all the packages you need.

Installing Pyenv

Run the command below to git checkout pyenv to your home directory

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Run the next two commands so your system knows how to find pyenv

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

Run the next command so that pyenv starts when you login

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

Run the next command to install the virtualenv plugin for pyenv

git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

Run this final install command to restart your shell so that pyenv + virtualenv will start working

exec "$SHELL"

Take it for a test drive!

Now it should be possible to start using pyenv. Type pyenv and see what happens. It should look something like this:

> pyenv

pyenv 1.1.3-5-g7dae197
Usage: pyenv <command> [<args>]
....

Try installing the latest Python 2.7 by running the next command:

pyenv install 2.7.13

If that was successful, create a "my-pyenv-test" project directory and in that directory run:

pyenv virtualenv 2.7.13 my-pyenv-test

This will create a virtualenv using Python 2.7.13 and call it my-pyenv-test. Note: the name doesn't need to match the directory name.

Now start using the new environment by running this command:

pyenv local my-pyenv-test

The command will switch to the new environment and also create a file called .python-version. In this file you will see the text "my-pyenv-test". Now every time you cd into this project directory the pyenv program will see the .python-version file and load the environment it finds in the file.

What next?

Now it is possible to use pyenv with virtualenv for any project you want. If you would rather be using Python 3 for your next project then you can do the following:

pyenv install 3.6.1
pyenv virtualenv 3.6.1 my-cool-python3-project
pyenv local my-cool-python3-project

And each time you switch between different project directories it will automatically load the correct environment because there is a .python-version in the directory.

@sonlinux
Copy link

Wonderfully guided

@loynaps
Copy link

loynaps commented Aug 27, 2017

This is great

@blesschews
Copy link

wonderful managed to get through now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment