You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
14 KiB

9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
13 years ago
11 years ago
11 years ago
13 years ago
  1. # Simple Python Version Management: pyenv
  2. [![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)
  3. [![Build Status](https://travis-ci.org/yyuu/pyenv.svg?branch=master)](https://travis-ci.org/yyuu/pyenv)
  4. pyenv lets you easily switch between multiple versions of Python. It's
  5. simple, unobtrusive, and follows the UNIX tradition of single-purpose
  6. tools that do one thing well.
  7. This project was forked from [rbenv](https://github.com/rbenv/rbenv) and
  8. [ruby-build](https://github.com/rbenv/ruby-build), and modified for Python.
  9. <img src="https://i.gyazo.com/699a58927b77e46e71cd674c7fc7a78d.png" width="735" height="490" />
  10. ### pyenv _does..._
  11. * Let you **change the global Python version** on a per-user basis.
  12. * Provide support for **per-project Python versions**.
  13. * Allow you to **override the Python version** with an environment
  14. variable.
  15. * Search commands from **multiple versions of Python at a time**.
  16. This may be helpful to test across Python versions with [tox](https://pypi.python.org/pypi/tox).
  17. ### In contrast with pythonbrew and pythonz, pyenv _does not..._
  18. * **Depend on Python itself.** pyenv was made from pure shell scripts.
  19. There is no bootstrap problem of Python.
  20. * **Need to be loaded into your shell.** Instead, pyenv's shim
  21. approach works by adding a directory to your `$PATH`.
  22. * **Manage virtualenv.** Of course, you can create [virtualenv](https://pypi.python.org/pypi/virtualenv)
  23. yourself, or [pyenv-virtualenv](https://github.com/yyuu/pyenv-virtualenv)
  24. to automate the process.
  25. ----
  26. ## Table of Contents
  27. * **[How It Works](#how-it-works)**
  28. * [Understanding PATH](#understanding-path)
  29. * [Understanding Shims](#understanding-shims)
  30. * [Choosing the Python Version](#choosing-the-python-version)
  31. * [Locating the Python Installation](#locating-the-python-installation)
  32. * **[Installation](#installation)**
  33. * [Basic GitHub Checkout](#basic-github-checkout)
  34. * [Upgrading](#upgrading)
  35. * [Homebrew on Mac OS X](#homebrew-on-mac-os-x)
  36. * [Advanced Configuration](#advanced-configuration)
  37. * [Uninstalling Python Versions](#uninstalling-python-versions)
  38. * **[Command Reference](#command-reference)**
  39. * **[Development](#development)**
  40. * [Version History](#version-history)
  41. * [License](#license)
  42. ----
  43. ## How It Works
  44. At a high level, pyenv intercepts Python commands using shim
  45. executables injected into your `PATH`, determines which Python version
  46. has been specified by your application, and passes your commands along
  47. to the correct Python installation.
  48. ### Understanding PATH
  49. When you run a command like `python` or `pip`, your operating system
  50. searches through a list of directories to find an executable file with
  51. that name. This list of directories lives in an environment variable
  52. called `PATH`, with each directory in the list separated by a colon:
  53. /usr/local/bin:/usr/bin:/bin
  54. Directories in `PATH` are searched from left to right, so a matching
  55. executable in a directory at the beginning of the list takes
  56. precedence over another one at the end. In this example, the
  57. `/usr/local/bin` directory will be searched first, then `/usr/bin`,
  58. then `/bin`.
  59. ### Understanding Shims
  60. pyenv works by inserting a directory of _shims_ at the front of your
  61. `PATH`:
  62. ~/.pyenv/shims:/usr/local/bin:/usr/bin:/bin
  63. Through a process called _rehashing_, pyenv maintains shims in that
  64. directory to match every Python command across every installed version
  65. of Python—`python`, `pip`, and so on.
  66. Shims are lightweight executables that simply pass your command along
  67. to pyenv. So with pyenv installed, when you run, say, `pip`, your
  68. operating system will do the following:
  69. * Search your `PATH` for an executable file named `pip`
  70. * Find the pyenv shim named `pip` at the beginning of your `PATH`
  71. * Run the shim named `pip`, which in turn passes the command along to
  72. pyenv
  73. ### Choosing the Python Version
  74. When you execute a shim, pyenv determines which Python version to use by
  75. reading it from the following sources, in this order:
  76. 1. The `PYENV_VERSION` environment variable (if specified). You can use
  77. the [`pyenv shell`](https://github.com/yyuu/pyenv/blob/master/COMMANDS.md#pyenv-shell) command to set this environment
  78. variable in your current shell session.
  79. 2. The application-specific `.python-version` file in the current
  80. directory (if present). You can modify the current directory's
  81. `.python-version` file with the [`pyenv local`](https://github.com/yyuu/pyenv/blob/master/COMMANDS.md#pyenv-local)
  82. command.
  83. 3. The first `.python-version` file found (if any) by searching each parent
  84. directory, until reaching the root of your filesystem.
  85. 4. The global `~/.pyenv/version` file. You can modify this file using
  86. the [`pyenv global`](https://github.com/yyuu/pyenv/blob/master/COMMANDS.md#pyenv-global) command. If the global version
  87. file is not present, pyenv assumes you want to use the "system"
  88. Python. (In other words, whatever version would run if pyenv weren't in your
  89. `PATH`.)
  90. **NOTE:** You can activate multiple versions at the same time, including multiple
  91. versions of Python2 or Python3 simultaneously. This allows for parallel usage of
  92. Python2 and Python3, and is required with tools like `tox`. For example, to set
  93. your path to first use your `system` Python and Python3 (set to 2.7.9 and 3.4.2
  94. in this example), but also have Python 3.3.6, 3.2, and 2.5 available on your
  95. `PATH`, one would first `pyenv install` the missing versions, then set `pyenv
  96. global system 3.3.6 3.2 2.5`. At this point, one should be able to find the full
  97. executable path to each of these using `pyenv which`, e.g. `pyenv which python2.5`
  98. (should display `$PYENV_ROOT/versions/2.5/bin/python2.5`), or `pyenv which
  99. python3.4` (should display path to system Python3).
  100. ### Locating the Python Installation
  101. Once pyenv has determined which version of Python your application has
  102. specified, it passes the command along to the corresponding Python
  103. installation.
  104. Each Python version is installed into its own directory under
  105. `~/.pyenv/versions`.
  106. For example, you might have these versions installed:
  107. * `~/.pyenv/versions/2.7.8/`
  108. * `~/.pyenv/versions/3.4.2/`
  109. * `~/.pyenv/versions/pypy-2.4.0/`
  110. As far as pyenv is concerned, version names are simply the directories in
  111. `~/.pyenv/versions`.
  112. ----
  113. ## Installation
  114. If you're on Mac OS X, consider [installing with Homebrew](#homebrew-on-mac-os-x).
  115. ### The automatic installer
  116. Visit my other project:
  117. https://github.com/yyuu/pyenv-installer
  118. ### Basic GitHub Checkout
  119. This will get you going with the latest version of pyenv and make it
  120. easy to fork and contribute any changes back upstream.
  121. 1. **Check out pyenv where you want it installed.**
  122. A good place to choose is `$HOME/.pyenv` (but you can install it somewhere else).
  123. $ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
  124. 2. **Define environment variable `PYENV_ROOT`** to point to the path where
  125. pyenv repo is cloned and add `$PYENV_ROOT/bin` to your `$PATH` for access
  126. to the `pyenv` command-line utility.
  127. $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
  128. $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
  129. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  130. **Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
  131. 3. **Add `pyenv init` to your shell** to enable shims and autocompletion.
  132. Please make sure `eval "$(pyenv init -)"` is placed toward the end of the shell
  133. configuration file since it manipulates `PATH` during the initialization.
  134. $ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
  135. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  136. **Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
  137. **General warning**: There are some systems where the `BASH_ENV` variable is configured
  138. to point to `.bashrc`. On such systems you should almost certainly put the abovementioned line
  139. `eval "$(pyenv init -)` into `.bash_profile`, and **not** into `.bashrc`. Otherwise you
  140. may observe strange behaviour, such as `pyenv` getting into an infinite loop.
  141. See [#264](https://github.com/yyuu/pyenv/issues/264) for details.
  142. 4. **Restart your shell so the path changes take effect.**
  143. You can now begin using pyenv.
  144. $ exec $SHELL
  145. 5. **Install Python versions into `$PYENV_ROOT/versions`.**
  146. For example, to download and install Python 2.7.8, run:
  147. $ pyenv install 2.7.8
  148. **NOTE:** If you need to pass configure option to build, please use
  149. ```CONFIGURE_OPTS``` environment variable.
  150. **NOTE:** If you want to use proxy to download, please use `http_proxy` and `https_proxy`
  151. environment variable.
  152. **NOTE:** If you are having trouble installing a python version,
  153. please visit the wiki page about
  154. [Common Build Problems](https://github.com/yyuu/pyenv/wiki/Common-build-problems)
  155. 6. **Rebuild the shim binaries.**
  156. You should do this any time you install a new Python binary.
  157. (Examples: installing a new Python version, or installing a package that provides a binary.)
  158. $ pyenv rehash
  159. This can be automated for pip using
  160. [pyenv-pip-rehash](https://github.com/yyuu/pyenv-pip-rehash), which invokes
  161. `pyenv rehash` after (un)installing packages using pip.
  162. #### Upgrading
  163. If you've installed pyenv using the instructions above, you can
  164. upgrade your installation at any time using git.
  165. To upgrade to the latest development version of pyenv, use `git pull`:
  166. $ cd ~/.pyenv
  167. $ git pull
  168. To upgrade to a specific release of pyenv, check out the corresponding tag:
  169. $ cd ~/.pyenv
  170. $ git fetch
  171. $ git tag
  172. v0.1.0
  173. $ git checkout v0.1.0
  174. ### Uninstalling pyenv
  175. The simplicity of pyenv makes it easy to temporarily disable it, or
  176. uninstall from the system.
  177. 1. To **disable** pyenv managing your Python versions, simply remove the
  178. `pyenv init` line from your shell startup configuration. This will
  179. remove pyenv shims directory from PATH, and future invocations like
  180. `python` will execute the system Python version, as before pyenv.
  181. `pyenv` will still be accessible on the command line, but your Python
  182. apps won't be affected by version switching.
  183. 2. To completely **uninstall** pyenv, perform step (1) and then remove
  184. its root directory. This will **delete all Python versions** that were
  185. installed under `` `pyenv root`/versions/ `` directory:
  186. rm -rf `pyenv root`
  187. If you've installed pyenv using a package manager, as a final step
  188. perform the pyenv package removal. For instance, for Homebrew:
  189. brew uninstall pyenv
  190. ## Command Reference
  191. ### Homebrew on Mac OS X
  192. You can also install pyenv using the [Homebrew](http://brew.sh)
  193. package manager for Mac OS X.
  194. $ brew update
  195. $ brew install pyenv
  196. To upgrade pyenv in the future, use `upgrade` instead of `install`.
  197. After installation, you'll need to add `eval "$(pyenv init -)"` to your profile (as stated in the caveats displayed by Homebrew — to display them again, use `brew info pyenv`). You only need to add that to your profile once.
  198. Then follow the rest of the post-installation steps under "Basic GitHub Checkout" above, starting with #4 ("restart your shell so the path changes take effect").
  199. ### Advanced Configuration
  200. Skip this section unless you must know what every line in your shell
  201. profile is doing.
  202. `pyenv init` is the only command that crosses the line of loading
  203. extra commands into your shell. Coming from rvm, some of you might be
  204. opposed to this idea. Here's what `pyenv init` actually does:
  205. 1. **Sets up your shims path.** This is the only requirement for pyenv to
  206. function properly. You can do this by hand by prepending
  207. `~/.pyenv/shims` to your `$PATH`.
  208. 2. **Installs autocompletion.** This is entirely optional but pretty
  209. useful. Sourcing `~/.pyenv/completions/pyenv.bash` will set that
  210. up. There is also a `~/.pyenv/completions/pyenv.zsh` for Zsh
  211. users.
  212. 3. **Rehashes shims.** From time to time you'll need to rebuild your
  213. shim files. Doing this on init makes sure everything is up to
  214. date. You can always run `pyenv rehash` manually.
  215. 4. **Installs the sh dispatcher.** This bit is also optional, but allows
  216. pyenv and plugins to change variables in your current shell, making
  217. commands like `pyenv shell` possible. The sh dispatcher doesn't do
  218. anything crazy like override `cd` or hack your shell prompt, but if
  219. for some reason you need `pyenv` to be a real script rather than a
  220. shell function, you can safely skip it.
  221. To see exactly what happens under the hood for yourself, run `pyenv init -`.
  222. ### Uninstalling Python Versions
  223. As time goes on, you will accumulate Python versions in your
  224. `~/.pyenv/versions` directory.
  225. To remove old Python versions, `pyenv uninstall` command to automate
  226. the removal process.
  227. Alternatively, simply `rm -rf` the directory of the version you want
  228. to remove. You can find the directory of a particular Python version
  229. with the `pyenv prefix` command, e.g. `pyenv prefix 2.6.8`.
  230. ----
  231. ## Command Reference
  232. See [COMMANDS.md](COMMANDS.md).
  233. ----
  234. ## Environment variables
  235. You can affect how pyenv operates with the following settings:
  236. name | default | description
  237. -----|---------|------------
  238. `PYENV_VERSION` | | Specifies the Python version to be used.<br>Also see [`pyenv shell`](#pyenv-shell)
  239. `PYENV_ROOT` | `~/.pyenv` | Defines the directory under which Python versions and shims reside.<br>Also see `pyenv root`
  240. `PYENV_DEBUG` | | Outputs debug information.<br>Also as: `pyenv --debug <subcommand>`
  241. `PYENV_HOOK_PATH` | [_see wiki_][hooks] | Colon-separated list of paths searched for pyenv hooks.
  242. `PYENV_DIR` | `$PWD` | Directory to start searching for `.python-version` files.
  243. ## Development
  244. The pyenv source code is [hosted on
  245. GitHub](https://github.com/yyuu/pyenv). It's clean, modular,
  246. and easy to understand, even if you're not a shell hacker.
  247. Tests are executed using [Bats](https://github.com/sstephenson/bats):
  248. $ bats test
  249. $ bats/test/<file>.bats
  250. Please feel free to submit pull requests and file bugs on the [issue
  251. tracker](https://github.com/yyuu/pyenv/issues).
  252. [pyenv-virtualenv]: https://github.com/yyuu/pyenv-virtualenv#readme
  253. [hooks]: https://github.com/yyuu/pyenv/wiki/Authoring-plugins#pyenv-hooks