2012-08-31 02:23:41 -04:00
# Simple Python Version Management: pyenv
2015-09-20 22:43:40 -04:00
[![Join the chat at https://gitter.im/yyuu/pyenv ](https://badges.gitter.im/Join%20Chat.svg )](https://gitter.im/yyuu/pyenv?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2017-03-05 23:42:15 -05:00
[![Build Status ](https://travis-ci.org/pyenv/pyenv.svg?branch=master )](https://travis-ci.org/pyenv/pyenv)
2014-01-08 07:47:36 -05:00
2012-08-31 02:23:41 -04:00
pyenv lets you easily switch between multiple versions of Python. It's
simple, unobtrusive, and follows the UNIX tradition of single-purpose
tools that do one thing well.
2015-11-27 22:12:54 -05:00
This project was forked from [rbenv ](https://github.com/rbenv/rbenv ) and
[ruby-build ](https://github.com/rbenv/ruby-build ), and modified for Python.
2012-08-31 02:23:41 -04:00
2018-02-08 19:08:24 -05:00
< img src = "/terminal_output.png" width = "595" height = "490" / >
2014-03-19 15:00:54 -04:00
2012-08-31 07:59:24 -04:00
2012-10-02 22:31:10 -04:00
### pyenv _does..._
2012-08-31 02:23:41 -04:00
* Let you **change the global Python version** on a per-user basis.
* Provide support for **per-project Python versions** .
* Allow you to **override the Python version** with an environment
variable.
2012-09-03 06:40:30 -04:00
* Search commands from **multiple versions of Python at a time** .
2015-11-27 22:12:54 -05:00
This may be helpful to test across Python versions with [tox ](https://pypi.python.org/pypi/tox ).
2012-08-31 02:23:41 -04:00
2014-03-19 15:00:54 -04:00
2012-10-02 22:31:10 -04:00
### In contrast with pythonbrew and pythonz, pyenv _does not..._
2013-08-27 15:27:57 -04:00
* **Depend on Python itself.** pyenv was made from pure shell scripts.
2012-10-02 22:31:10 -04:00
There is no bootstrap problem of Python.
* **Need to be loaded into your shell.** Instead, pyenv's shim
approach works by adding a directory to your `$PATH` .
2015-11-27 22:12:54 -05:00
* **Manage virtualenv.** Of course, you can create [virtualenv ](https://pypi.python.org/pypi/virtualenv )
2017-03-05 23:14:12 -05:00
yourself, or [pyenv-virtualenv ](https://github.com/pyenv/pyenv-virtualenv )
2012-10-02 22:31:10 -04:00
to automate the process.
2014-03-19 15:00:54 -04:00
2014-03-20 09:19:11 -04:00
----
2012-08-31 02:23:41 -04:00
## Table of Contents
2014-03-20 09:17:53 -04:00
* **[How It Works](#how-it-works)**
2013-02-06 08:08:16 -05:00
* [Understanding PATH ](#understanding-path )
* [Understanding Shims ](#understanding-shims )
* [Choosing the Python Version ](#choosing-the-python-version )
* [Locating the Python Installation ](#locating-the-python-installation )
2014-03-20 09:17:53 -04:00
* **[Installation](#installation)**
2013-02-06 08:08:16 -05:00
* [Basic GitHub Checkout ](#basic-github-checkout )
* [Upgrading ](#upgrading )
2013-06-13 07:04:37 -04:00
* [Homebrew on Mac OS X ](#homebrew-on-mac-os-x )
2015-11-17 19:51:03 -05:00
* [Advanced Configuration ](#advanced-configuration )
2013-02-06 08:08:16 -05:00
* [Uninstalling Python Versions ](#uninstalling-python-versions )
2014-03-20 09:17:53 -04:00
* **[Command Reference](#command-reference)**
* **[Development](#development)**
2013-02-06 08:08:16 -05:00
* [Version History ](#version-history )
* [License ](#license )
2014-03-19 15:00:54 -04:00
2014-03-20 09:19:11 -04:00
----
2013-02-06 08:08:16 -05:00
## How It Works
At a high level, pyenv intercepts Python commands using shim
executables injected into your `PATH` , determines which Python version
has been specified by your application, and passes your commands along
to the correct Python installation.
### Understanding PATH
When you run a command like `python` or `pip` , your operating system
searches through a list of directories to find an executable file with
that name. This list of directories lives in an environment variable
called `PATH` , with each directory in the list separated by a colon:
/usr/local/bin:/usr/bin:/bin
Directories in `PATH` are searched from left to right, so a matching
executable in a directory at the beginning of the list takes
precedence over another one at the end. In this example, the
`/usr/local/bin` directory will be searched first, then `/usr/bin` ,
then `/bin` .
### Understanding Shims
pyenv works by inserting a directory of _shims_ at the front of your
`PATH` :
2017-02-06 08:26:13 -05:00
$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin
2013-02-06 08:08:16 -05:00
Through a process called _rehashing_ , pyenv maintains shims in that
directory to match every Python command across every installed version
of Python—`python`, `pip` , and so on.
Shims are lightweight executables that simply pass your command along
to pyenv. So with pyenv installed, when you run, say, `pip` , your
operating system will do the following:
* Search your `PATH` for an executable file named `pip`
* Find the pyenv shim named `pip` at the beginning of your `PATH`
* Run the shim named `pip` , which in turn passes the command along to
pyenv
### Choosing the Python Version
When you execute a shim, pyenv determines which Python version to use by
reading it from the following sources, in this order:
2014-03-19 15:00:54 -04:00
1. The `PYENV_VERSION` environment variable (if specified). You can use
2017-03-05 23:14:12 -05:00
the [`pyenv shell` ](https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-shell ) command to set this environment
2013-02-06 08:08:16 -05:00
variable in your current shell session.
2. The application-specific `.python-version` file in the current
2014-03-19 15:00:54 -04:00
directory (if present). You can modify the current directory's
2017-03-05 23:14:12 -05:00
`.python-version` file with the [`pyenv local` ](https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-local )
2013-02-06 08:08:16 -05:00
command.
2014-03-19 15:00:54 -04:00
3. The first `.python-version` file found (if any) by searching each parent
directory, until reaching the root of your filesystem.
2013-02-06 08:08:16 -05:00
2017-02-09 20:04:35 -05:00
4. The global `$(pyenv root)/version` file. You can modify this file using
2017-03-05 23:14:12 -05:00
the [`pyenv global` ](https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-global ) command. If the global version
2013-02-06 08:08:16 -05:00
file is not present, pyenv assumes you want to use the "system"
2014-03-19 15:00:54 -04:00
Python. (In other words, whatever version would run if pyenv weren't in your
`PATH` .)
2015-06-22 18:54:03 -04:00
**NOTE:** You can activate multiple versions at the same time, including multiple
2015-04-17 18:59:04 -04:00
versions of Python2 or Python3 simultaneously. This allows for parallel usage of
Python2 and Python3, and is required with tools like `tox` . For example, to set
your path to first use your `system` Python and Python3 (set to 2.7.9 and 3.4.2
in this example), but also have Python 3.3.6, 3.2, and 2.5 available on your
`PATH` , one would first `pyenv install` the missing versions, then set `pyenv
global system 3.3.6 3.2 2.5`. At this point, one should be able to find the full
executable path to each of these using `pyenv which` , e.g. `pyenv which python2.5`
2017-02-06 08:26:13 -05:00
(should display `$(pyenv root)/versions/2.5/bin/python2.5` ), or `pyenv which
2017-01-02 06:05:36 -05:00
python3.4` (should display path to system Python3). You can also specify multiple
versions in a `.python-version` file, separated by newlines or any whitespace.
2013-02-06 08:08:16 -05:00
### Locating the Python Installation
Once pyenv has determined which version of Python your application has
specified, it passes the command along to the corresponding Python
installation.
Each Python version is installed into its own directory under
2017-02-06 08:26:13 -05:00
`$(pyenv root)/versions` .
2014-03-19 15:00:54 -04:00
For example, you might have these versions installed:
2013-02-06 08:08:16 -05:00
2017-02-06 08:26:13 -05:00
* `$(pyenv root)/versions/2.7.8/`
* `$(pyenv root)/versions/3.4.2/`
* `$(pyenv root)/versions/pypy-2.4.0/`
2013-02-06 08:08:16 -05:00
2014-03-19 15:00:54 -04:00
As far as pyenv is concerned, version names are simply the directories in
2017-02-06 08:26:13 -05:00
`$(pyenv root)/versions` .
2013-02-06 08:08:16 -05:00
2016-12-26 19:51:29 -05:00
### Managing Virtual Environments
2017-03-05 23:14:12 -05:00
There is a pyenv plugin named [pyenv-virtualenv ](https://github.com/pyenv/pyenv-virtualenv ) which comes with various features to help pyenv users to manage virtual environments created by virtualenv or Anaconda.
2016-12-26 19:51:29 -05:00
Because the `activate` script of those virtual environments are relying on mutating `$PATH` variable of user's interactive shell, it will intercept pyenv's shim style command execution hooks.
We'd recommend to install pyenv-virtualenv as well if you have some plan to play with those virtual environments.
2014-03-19 15:00:54 -04:00
2014-03-20 09:19:11 -04:00
----
2013-02-06 08:08:16 -05:00
## Installation
2014-03-20 09:17:53 -04:00
If you're on Mac OS X, consider [installing with Homebrew ](#homebrew-on-mac-os-x ).
2013-06-13 07:04:37 -04:00
2014-03-19 15:00:54 -04:00
2014-02-20 22:57:42 -05:00
### The automatic installer
Visit my other project:
2017-03-05 23:14:12 -05:00
https://github.com/pyenv/pyenv-installer
2014-02-20 22:57:42 -05:00
2014-03-19 15:00:54 -04:00
2013-02-06 08:08:16 -05:00
### Basic GitHub Checkout
2012-08-31 02:23:41 -04:00
This will get you going with the latest version of pyenv and make it
easy to fork and contribute any changes back upstream.
2014-09-02 20:51:22 -04:00
1. **Check out pyenv where you want it installed.**
2014-03-20 09:17:53 -04:00
A good place to choose is `$HOME/.pyenv` (but you can install it somewhere else).
2012-08-31 02:23:41 -04:00
2017-03-05 23:14:12 -05:00
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
2012-08-31 02:23:41 -04:00
2014-03-19 15:00:54 -04:00
2. **Define environment variable `PYENV_ROOT`** to point to the path where
2014-09-02 20:51:22 -04:00
pyenv repo is cloned and add `$PYENV_ROOT/bin` to your `$PATH` for access
2014-03-20 09:17:53 -04:00
to the `pyenv` command-line utility.
2013-08-28 02:33:18 -04:00
2017-01-01 05:39:20 -05:00
```sh
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
```
2017-03-12 10:50:35 -04:00
**Zsh note** : Modify your `~/.zshenv` file instead of `~/.bash_profile` .
**Ubuntu and Fedora note** : Modify your `~/.bashrc` file instead of `~/.bash_profile` .
2017-03-12 10:53:15 -04:00
**Proxy note** : If you use a proxy, export `http_proxy` and `HTTPS_PROXY` too.
2012-08-31 02:23:41 -04:00
2014-03-19 15:00:54 -04:00
3. **Add `pyenv init` to your shell** to enable shims and autocompletion.
2015-11-18 19:50:03 -05:00
Please make sure `eval "$(pyenv init -)"` is placed toward the end of the shell
2015-04-15 09:41:09 -04:00
configuration file since it manipulates `PATH` during the initialization.
2017-01-01 05:39:20 -05:00
```sh
2017-09-08 11:39:02 -04:00
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
2017-01-01 05:39:20 -05:00
```
2016-10-11 10:13:04 -04:00
**Zsh note** : Modify your `~/.zshenv` file instead of `~/.bash_profile` .
2016-10-11 15:34:29 -04:00
**Ubuntu and Fedora note** : Modify your `~/.bashrc` file instead of `~/.bash_profile` .
2017-05-02 11:52:53 -04:00
2015-11-18 19:50:03 -05:00
**General warning** : There are some systems where the `BASH_ENV` variable is configured
2014-11-04 11:54:19 -05:00
to point to `.bashrc` . On such systems you should almost certainly put the abovementioned line
2017-06-23 20:50:54 -04:00
`eval "$(pyenv init -)"` into `.bash_profile` , and **not** into `.bashrc` . Otherwise you
2014-11-04 11:54:19 -05:00
may observe strange behaviour, such as `pyenv` getting into an infinite loop.
2017-03-05 23:14:12 -05:00
See [#264 ](https://github.com/pyenv/pyenv/issues/264 ) for details.
2012-08-31 02:23:41 -04:00
2014-09-02 20:51:22 -04:00
4. **Restart your shell so the path changes take effect.**
2014-03-20 09:17:53 -04:00
You can now begin using pyenv.
2017-01-01 05:39:20 -05:00
```sh
2017-08-05 02:11:30 -04:00
$ exec "$SHELL"
2017-01-01 05:39:20 -05:00
```
2017-02-06 08:26:13 -05:00
5. **Install Python versions into `$(pyenv root)/versions`.**
2015-11-17 19:35:35 -05:00
For example, to download and install Python 2.7.8, run:
2017-01-01 05:39:20 -05:00
```sh
$ pyenv install 2.7.8
```
2014-03-20 09:17:53 -04:00
**NOTE:** If you need to pass configure option to build, please use
2013-05-16 10:24:21 -04:00
```CONFIGURE_OPTS``` environment variable.
2017-05-02 11:52:53 -04:00
2015-09-24 09:46:34 -04:00
**NOTE:** If you want to use proxy to download, please use `http_proxy` and `https_proxy`
environment variable.
2014-09-02 20:51:22 -04:00
**NOTE:** If you are having trouble installing a python version,
please visit the wiki page about
2017-03-05 23:14:12 -05:00
[Common Build Problems ](https://github.com/pyenv/pyenv/wiki/Common-build-problems )
2012-08-31 02:23:41 -04:00
2014-03-19 15:00:54 -04:00
2013-02-06 08:08:16 -05:00
#### Upgrading
2012-08-31 02:23:41 -04:00
If you've installed pyenv using the instructions above, you can
upgrade your installation at any time using git.
To upgrade to the latest development version of pyenv, use `git pull` :
2017-01-01 05:39:20 -05:00
```sh
2017-02-06 08:26:13 -05:00
$ cd $(pyenv root)
2017-01-01 05:39:20 -05:00
$ git pull
```
2012-08-31 02:23:41 -04:00
2014-03-20 09:17:53 -04:00
To upgrade to a specific release of pyenv, check out the corresponding tag:
2012-08-31 02:23:41 -04:00
2017-01-01 05:39:20 -05:00
```sh
2017-02-06 08:26:13 -05:00
$ cd $(pyenv root)
2017-01-01 05:39:20 -05:00
$ git fetch
$ git tag
v0.1.0
$ git checkout v0.1.0
```
2012-08-31 07:59:24 -04:00
2015-11-20 23:13:52 -05:00
### Uninstalling pyenv
2015-06-09 17:27:21 -04:00
2015-11-20 23:13:52 -05:00
The simplicity of pyenv makes it easy to temporarily disable it, or
2015-06-09 17:27:21 -04:00
uninstall from the system.
2015-11-20 23:13:52 -05:00
1. To **disable** pyenv managing your Python versions, simply remove the
`pyenv init` line from your shell startup configuration. This will
remove pyenv shims directory from PATH, and future invocations like
`python` will execute the system Python version, as before pyenv.
2015-06-09 17:27:21 -04:00
2015-11-20 23:13:52 -05:00
`pyenv` will still be accessible on the command line, but your Python
2015-06-09 17:27:21 -04:00
apps won't be affected by version switching.
2015-11-20 23:13:52 -05:00
2. To completely **uninstall** pyenv, perform step (1) and then remove
its root directory. This will **delete all Python versions** that were
2017-02-06 08:26:13 -05:00
installed under `` $(pyenv root)/versions/ `` directory:
2017-01-01 05:39:20 -05:00
```sh
2017-02-06 08:26:13 -05:00
rm -rf $(pyenv root)
2017-01-01 05:39:20 -05:00
```
2015-11-20 23:13:52 -05:00
If you've installed pyenv using a package manager, as a final step
perform the pyenv package removal. For instance, for Homebrew:
2015-06-09 17:27:21 -04:00
2015-11-20 23:13:52 -05:00
brew uninstall pyenv
2015-06-09 17:27:21 -04:00
2013-06-13 07:04:37 -04:00
### Homebrew on Mac OS X
2014-09-02 20:51:22 -04:00
You can also install pyenv using the [Homebrew ](http://brew.sh )
2014-03-20 09:17:53 -04:00
package manager for Mac OS X.
$ brew update
$ brew install pyenv
2013-06-13 07:04:37 -04:00
2015-11-18 19:48:57 -05:00
To upgrade pyenv in the future, use `upgrade` instead of `install` .
2013-06-13 07:04:37 -04:00
2017-03-21 16:00:37 -04:00
Then follow the rest of the post-installation steps under [Basic GitHub Checkout ](https://github.com/pyenv/pyenv#basic-github-checkout ) above, starting with #3 ("Add `pyenv init` to your shell to enable shims and autocompletion").
2014-03-19 15:00:54 -04:00
2015-11-17 19:51:03 -05:00
### Advanced Configuration
2012-08-31 07:59:24 -04:00
Skip this section unless you must know what every line in your shell
profile is doing.
`pyenv init` is the only command that crosses the line of loading
extra commands into your shell. Coming from rvm, some of you might be
opposed to this idea. Here's what `pyenv init` actually does:
2014-03-19 15:00:54 -04:00
1. **Sets up your shims path.** This is the only requirement for pyenv to
2012-08-31 07:59:24 -04:00
function properly. You can do this by hand by prepending
2017-02-06 08:26:13 -05:00
`$(pyenv root)/shims` to your `$PATH` .
2012-08-31 07:59:24 -04:00
2014-03-19 15:00:54 -04:00
2. **Installs autocompletion.** This is entirely optional but pretty
2017-02-06 08:26:13 -05:00
useful. Sourcing `$(pyenv root)/completions/pyenv.bash` will set that
up. There is also a `$(pyenv root)/completions/pyenv.zsh` for Zsh
2012-08-31 07:59:24 -04:00
users.
2014-03-19 15:00:54 -04:00
3. **Rehashes shims.** From time to time you'll need to rebuild your
2012-08-31 07:59:24 -04:00
shim files. Doing this on init makes sure everything is up to
date. You can always run `pyenv rehash` manually.
2014-03-19 15:00:54 -04:00
4. **Installs the sh dispatcher.** This bit is also optional, but allows
2012-08-31 07:59:24 -04:00
pyenv and plugins to change variables in your current shell, making
commands like `pyenv shell` possible. The sh dispatcher doesn't do
anything crazy like override `cd` or hack your shell prompt, but if
for some reason you need `pyenv` to be a real script rather than a
shell function, you can safely skip it.
2014-03-19 15:00:54 -04:00
To see exactly what happens under the hood for yourself, run `pyenv init -` .
2012-08-31 02:23:41 -04:00
2013-02-06 08:08:16 -05:00
### Uninstalling Python Versions
2014-03-19 15:00:54 -04:00
As time goes on, you will accumulate Python versions in your
2017-02-06 08:26:13 -05:00
`$(pyenv root)/versions` directory.
2013-02-06 08:08:16 -05:00
To remove old Python versions, `pyenv uninstall` command to automate
the removal process.
2014-09-02 20:51:22 -04:00
Alternatively, simply `rm -rf` the directory of the version you want
to remove. You can find the directory of a particular Python version
2014-03-19 15:00:54 -04:00
with the `pyenv prefix` command, e.g. `pyenv prefix 2.6.8` .
2013-02-06 08:08:16 -05:00
2014-03-20 09:19:11 -04:00
----
2013-02-06 08:08:16 -05:00
## Command Reference
2012-08-31 02:23:41 -04:00
2014-03-20 09:13:15 -04:00
See [COMMANDS.md ](COMMANDS.md ).
2014-03-03 17:11:23 -05:00
2012-08-31 07:59:24 -04:00
2014-03-20 09:19:11 -04:00
----
2015-03-13 04:14:24 -04:00
## Environment variables
2014-03-20 09:19:11 -04:00
2015-11-20 23:13:52 -05:00
You can affect how pyenv operates with the following settings:
2012-08-31 02:23:41 -04:00
2015-03-13 04:14:24 -04:00
name | default | description
-----|---------|------------
2017-05-02 11:52:53 -04:00
`PYENV_VERSION` | | Specifies the Python version to be used.< br > Also see [`pyenv shell` ](https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-shell )
2015-11-20 23:13:52 -05:00
`PYENV_ROOT` | `~/.pyenv` | Defines the directory under which Python versions and shims reside.< br > Also see `pyenv root`
`PYENV_DEBUG` | | Outputs debug information.< br > Also as: `pyenv --debug <subcommand>`
`PYENV_HOOK_PATH` | [_see wiki_][hooks] | Colon-separated list of paths searched for pyenv hooks.
`PYENV_DIR` | `$PWD` | Directory to start searching for `.python-version` files.
2017-03-01 18:20:59 -05:00
`PYTHON_BUILD_ARIA2_OPTS` | | Used to pass additional parameters to [`aria2` ](https://aria2.github.io/ ).< br > if `aria2c` binary is available on PATH, pyenv use `aria2c` instead of `curl` or `wget` to download the Python Source code. If you have an unstable internet connection, you can use this variable to instruct `aria2` to accelerate the download.< br > In most cases, you will only need to use `-x 10 -k 1M` as value to `PYTHON_BUILD_ARIA2_OPTS` environment variable
2016-12-16 12:08:44 -05:00
2014-03-19 15:00:54 -04:00
2013-01-03 22:18:05 -05:00
## Development
2012-08-31 02:23:41 -04:00
2015-11-20 23:13:52 -05:00
The pyenv source code is [hosted on
2017-03-05 23:14:12 -05:00
GitHub](https://github.com/pyenv/pyenv). It's clean, modular,
2011-08-11 14:11:37 -04:00
and easy to understand, even if you're not a shell hacker.
2012-08-31 02:23:41 -04:00
2013-03-07 15:05:49 -05:00
Tests are executed using [Bats ](https://github.com/sstephenson/bats ):
2012-08-31 02:23:41 -04:00
2013-03-07 15:05:49 -05:00
$ bats test
2015-11-20 23:13:52 -05:00
$ bats/test/< file > .bats
2012-08-31 02:23:41 -04:00
2011-08-11 14:11:37 -04:00
Please feel free to submit pull requests and file bugs on the [issue
2017-03-05 23:14:12 -05:00
tracker](https://github.com/pyenv/pyenv/issues).
2012-08-31 02:23:41 -04:00
2017-03-05 23:14:12 -05:00
[pyenv-virtualenv]: https://github.com/pyenv/pyenv-virtualenv#readme
[hooks]: https://github.com/pyenv/pyenv/wiki/Authoring-plugins#pyenv-hooks