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.

398 lines
16 KiB

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