您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

396 行
15 KiB

7 年前
13 年前
11 年前
11 年前
13 年前
  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/pyenv/pyenv.svg?branch=master)](https://travis-ci.org/pyenv/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. ![Terminal output example](/terminal_output.png)
  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/pyenv/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 root)/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/pyenv/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/pyenv/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 root)/version` file. You can modify this file using
  86. the [`pyenv global`](https://github.com/pyenv/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). You can also specify multiple
  100. versions in a `.python-version` file, separated by newlines or any whitespace.
  101. ### Locating the Python Installation
  102. Once pyenv has determined which version of Python your application has
  103. specified, it passes the command along to the corresponding Python
  104. installation.
  105. Each Python version is installed into its own directory under
  106. `$(pyenv root)/versions`.
  107. For example, you might have these versions installed:
  108. * `$(pyenv root)/versions/2.7.8/`
  109. * `$(pyenv root)/versions/3.4.2/`
  110. * `$(pyenv root)/versions/pypy-2.4.0/`
  111. As far as pyenv is concerned, version names are simply the directories in
  112. `$(pyenv root)/versions`.
  113. ### Managing Virtual Environments
  114. 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.
  115. 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.
  116. We'd recommend to install pyenv-virtualenv as well if you have some plan to play with those virtual environments.
  117. ----
  118. ## Installation
  119. If you're on Mac OS X, consider [installing with Homebrew](#homebrew-on-mac-os-x).
  120. ### The automatic installer
  121. Visit my other project:
  122. https://github.com/pyenv/pyenv-installer
  123. ### Basic GitHub Checkout
  124. This will get you going with the latest version of pyenv and make it
  125. easy to fork and contribute any changes back upstream.
  126. 1. **Check out pyenv where you want it installed.**
  127. A good place to choose is `$HOME/.pyenv` (but you can install it somewhere else).
  128. $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  129. 2. **Define environment variable `PYENV_ROOT`** to point to the path where
  130. pyenv repo is cloned and add `$PYENV_ROOT/bin` to your `$PATH` for access
  131. to the `pyenv` command-line utility.
  132. ```sh
  133. $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
  134. $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
  135. ```
  136. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  137. **Ubuntu and Fedora note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
  138. **Proxy note**: If you use a proxy, export `http_proxy` and `HTTPS_PROXY` too.
  139. 3. **Add `pyenv init` to your shell** to enable shims and autocompletion.
  140. Please make sure `eval "$(pyenv init -)"` is placed toward the end of the shell
  141. configuration file since it manipulates `PATH` during the initialization.
  142. ```sh
  143. $ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
  144. ```
  145. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  146. **Ubuntu and Fedora note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
  147. **General warning**: There are some systems where the `BASH_ENV` variable is configured
  148. to point to `.bashrc`. On such systems you should almost certainly put the abovementioned line
  149. `eval "$(pyenv init -)"` into `.bash_profile`, and **not** into `.bashrc`. Otherwise you
  150. may observe strange behaviour, such as `pyenv` getting into an infinite loop.
  151. See [#264](https://github.com/pyenv/pyenv/issues/264) for details.
  152. 4. **Restart your shell so the path changes take effect.**
  153. You can now begin using pyenv.
  154. ```sh
  155. $ exec "$SHELL"
  156. ```
  157. 5. **Install Python build dependencies** before attempting to install a new Python version. The
  158. [pyenv wiki](https://github.com/pyenv/pyenv/wiki) provides suggested installation packages
  159. and commands for various operating systems.
  160. 6. **Install Python versions into `$(pyenv root)/versions`.**
  161. For example, to download and install Python 2.7.8, run:
  162. ```sh
  163. $ pyenv install 2.7.8
  164. ```
  165. **NOTE:** If you need to pass configure option to build, please use
  166. ```CONFIGURE_OPTS``` environment variable.
  167. **NOTE:** If you want to use proxy to download, please use `http_proxy` and `https_proxy`
  168. environment variable.
  169. **NOTE:** If you are having trouble installing a python version,
  170. please visit the wiki page about
  171. [Common Build Problems](https://github.com/pyenv/pyenv/wiki/Common-build-problems)
  172. #### Upgrading
  173. If you've installed pyenv using the instructions above, you can
  174. upgrade your installation at any time using git.
  175. To upgrade to the latest development version of pyenv, use `git pull`:
  176. ```sh
  177. $ cd $(pyenv root)
  178. $ git pull
  179. ```
  180. To upgrade to a specific release of pyenv, check out the corresponding tag:
  181. ```sh
  182. $ cd $(pyenv root)
  183. $ git fetch
  184. $ git tag
  185. v0.1.0
  186. $ git checkout v0.1.0
  187. ```
  188. ### Uninstalling pyenv
  189. The simplicity of pyenv makes it easy to temporarily disable it, or
  190. uninstall from the system.
  191. 1. To **disable** pyenv managing your Python versions, simply remove the
  192. `pyenv init` line from your shell startup configuration. This will
  193. remove pyenv shims directory from PATH, and future invocations like
  194. `python` will execute the system Python version, as before pyenv.
  195. `pyenv` will still be accessible on the command line, but your Python
  196. apps won't be affected by version switching.
  197. 2. To completely **uninstall** pyenv, perform step (1) and then remove
  198. its root directory. This will **delete all Python versions** that were
  199. installed under `` $(pyenv root)/versions/ `` directory:
  200. ```sh
  201. rm -rf $(pyenv root)
  202. ```
  203. If you've installed pyenv using a package manager, as a final step
  204. perform the pyenv package removal. For instance, for Homebrew:
  205. brew uninstall pyenv
  206. ### Homebrew on Mac OS X
  207. You can also install pyenv using the [Homebrew](http://brew.sh)
  208. package manager for Mac OS X.
  209. $ brew update
  210. $ brew install pyenv
  211. To upgrade pyenv in the future, use `upgrade` instead of `install`.
  212. 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").
  213. ### Advanced Configuration
  214. Skip this section unless you must know what every line in your shell
  215. profile is doing.
  216. `pyenv init` is the only command that crosses the line of loading
  217. extra commands into your shell. Coming from rvm, some of you might be
  218. opposed to this idea. Here's what `pyenv init` actually does:
  219. 1. **Sets up your shims path.** This is the only requirement for pyenv to
  220. function properly. You can do this by hand by prepending
  221. `$(pyenv root)/shims` to your `$PATH`.
  222. 2. **Installs autocompletion.** This is entirely optional but pretty
  223. useful. Sourcing `$(pyenv root)/completions/pyenv.bash` will set that
  224. up. There is also a `$(pyenv root)/completions/pyenv.zsh` for Zsh
  225. users.
  226. 3. **Rehashes shims.** From time to time you'll need to rebuild your
  227. shim files. Doing this on init makes sure everything is up to
  228. date. You can always run `pyenv rehash` manually.
  229. 4. **Installs the sh dispatcher.** This bit is also optional, but allows
  230. pyenv and plugins to change variables in your current shell, making
  231. commands like `pyenv shell` possible. The sh dispatcher doesn't do
  232. anything crazy like override `cd` or hack your shell prompt, but if
  233. for some reason you need `pyenv` to be a real script rather than a
  234. shell function, you can safely skip it.
  235. To see exactly what happens under the hood for yourself, run `pyenv init -`.
  236. ### Uninstalling Python Versions
  237. As time goes on, you will accumulate Python versions in your
  238. `$(pyenv root)/versions` directory.
  239. To remove old Python versions, `pyenv uninstall` command to automate
  240. the removal process.
  241. Alternatively, simply `rm -rf` the directory of the version you want
  242. to remove. You can find the directory of a particular Python version
  243. with the `pyenv prefix` command, e.g. `pyenv prefix 2.6.8`.
  244. ----
  245. ## Command Reference
  246. See [COMMANDS.md](COMMANDS.md).
  247. ----
  248. ## Environment variables
  249. You can affect how pyenv operates with the following settings:
  250. name | default | description
  251. -----|---------|------------
  252. `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)
  253. `PYENV_ROOT` | `~/.pyenv` | Defines the directory under which Python versions and shims reside.<br>Also see `pyenv root`
  254. `PYENV_DEBUG` | | Outputs debug information.<br>Also as: `pyenv --debug <subcommand>`
  255. `PYENV_HOOK_PATH` | [_see wiki_][hooks] | Colon-separated list of paths searched for pyenv hooks.
  256. `PYENV_DIR` | `$PWD` | Directory to start searching for `.python-version` files.
  257. `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
  258. ## Development
  259. The pyenv source code is [hosted on
  260. GitHub](https://github.com/pyenv/pyenv). It's clean, modular,
  261. and easy to understand, even if you're not a shell hacker.
  262. Tests are executed using [Bats](https://github.com/sstephenson/bats):
  263. $ bats test
  264. $ bats/test/<file>.bats
  265. Please feel free to submit pull requests and file bugs on the [issue
  266. tracker](https://github.com/pyenv/pyenv/issues).
  267. [pyenv-virtualenv]: https://github.com/pyenv/pyenv-virtualenv#readme
  268. [hooks]: https://github.com/pyenv/pyenv/wiki/Authoring-plugins#pyenv-hooks
  269. ### Version History
  270. See [CHANGELOG.md](CHANGELOG.md).
  271. ### License
  272. [The MIT License](LICENSE)