> For the complete documentation index, see [llms.txt](https://jzlab.gitbook.io/jzkb/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jzlab.gitbook.io/jzkb/python/managing-multiple-python-versions.md).

# Managing Multiple Python Versions

## Why you should care

It's good python practice to segregate your python project environments with virtual environments.

This keeps your dependencies separate between projects which can be vital if they depend on different versions packages/modules and/or python.

The most popular virtual environment tool is [virtualenv](https://github.com/pypa/virtualenv) (or often just venv).

[Pyenv](https://github.com/pyenv/pyenv) has the ability to wrap virtualenv and makes installing and working with multiple versions of python seamless and effortless.

Basically, pyenv intercepts any shell commands you would normally make to python in your path and reroutes them to the appropriate binary or virtual environment you've selected (usually declared on a per directory basis).

## Installation

### OSX

pyenv is painless to install in OSX, just use [homebrew](https://github.com/pyenv/pyenv#homebrew-on-mac-os-x)

```bash
$ brew install pyenv
```

### Linux

The most uptodate instructions are probably best to get from the [pyenv github repo](https://github.com/pyenv/pyenv#Basic-GitHub-Checkout) itself, but below is the process I went through when installing pyenv on ubuntu.

1. **Check out pyenv to \~/.pyenv** (other places are ok too)

```bash
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
```

1. **Make the pyenv directory visible to your PATH**

   ```bash
   $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
   $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
   ```
2. **Add pyenv init to your shell to enable shims and autocompletion**

```bash
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc
```

1. **Restart shell so path changes take effect**

   ```bash
   $ exec "$SHELL"
   ```
2. **Install your new clean pyenv-managed version of python!**.

For example, for Python 2.7.10:

```bash
$ pyenv install 2.7.10
```

## Managing and using installed python versions

To see which version of python you have selected

```bash
$ pyenv which python
```

To change which python version the current directory should use

```bash
$ pyenv local <python version number>

#(e.g....2.7.10)
$ pyenv local 2.7.10
```

## Managing Virtual Environments

Being able to install and use an arbitrary version of python is great but sometimes even for the same version you need a special set of dependencies (like when I use GPU-enabled source-compiled tensorflow).

Situations like this make [pyenv-virtualenv](https://github.com/pyenv/pyenv-virtualenv) and [pyenv-virtualenvwrapper](https://github.com/pyenv/pyenv-virtualenvwrapper) essential. I'll hopefully exand on this later but don't have time today. I've you gotten this far you're off to a pretty good start.

-edc
