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.

366 regels
13 KiB

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