Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

461 wiersze
15 KiB

12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
11 lat temu
12 lat temu
12 lat temu
11 lat temu
12 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
11 lat temu
12 lat temu
12 lat temu
11 lat temu
12 lat temu
12 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
  1. # Simple Python Version Management: pyenv
  2. pyenv lets you easily switch between multiple versions of Python. It's
  3. simple, unobtrusive, and follows the UNIX tradition of single-purpose
  4. tools that do one thing well.
  5. This project was forked from [rbenv](https://github.com/sstephenson/rbenv) and
  6. [ruby-build](https://github.com/sstephenson/ruby-build), and modified for Python.
  7. <img src="http://gyazo.com/9c829fafdf5e58880c820349c4e9197e.png?1346414267" width="849" height="454">
  8. ### pyenv _does..._
  9. * Let you **change the global Python version** on a per-user basis.
  10. * Provide support for **per-project Python versions**.
  11. * Allow you to **override the Python version** with an environment
  12. variable.
  13. * Search commands from **multiple versions of Python at a time**.
  14. This may be helpful to test across Python versions with [tox](http://pypi.python.org/pypi/tox).
  15. ### In contrast with pythonbrew and pythonz, pyenv _does not..._
  16. * **Depend on Python itself.** pyenv was made from pure shell scripts.
  17. There is no bootstrap problem of Python.
  18. * **Need to be loaded into your shell.** Instead, pyenv's shim
  19. approach works by adding a directory to your `$PATH`.
  20. * **Manage virtualenv.** Of course, you can create [virtualenv](http://pypi.python.org/pypi/virtualenv)
  21. yourself, or [pyenv-virtualenv](https://github.com/yyuu/pyenv-virtualenv)
  22. to automate the process.
  23. ## Table of Contents
  24. * [How It Works](#how-it-works)
  25. * [Understanding PATH](#understanding-path)
  26. * [Understanding Shims](#understanding-shims)
  27. * [Choosing the Python Version](#choosing-the-python-version)
  28. * [Locating the Python Installation](#locating-the-python-installation)
  29. * [Installation](#installation)
  30. * [Basic GitHub Checkout](#basic-github-checkout)
  31. * [Upgrading](#upgrading)
  32. * [Homebrew on Mac OS X](#homebrew-on-mac-os-x)
  33. * [Neckbeard Configuration](#neckbeard-configuration)
  34. * [Uninstalling Python Versions](#uninstalling-python-versions)
  35. * [Command Reference](#command-reference)
  36. * [pyenv local](#pyenv-local)
  37. * [pyenv global](#pyenv-global)
  38. * [pyenv shell](#pyenv-shell)
  39. * [pyenv versions](#pyenv-versions)
  40. * [pyenv version](#pyenv-version)
  41. * [pyenv rehash](#pyenv-rehash)
  42. * [pyenv which](#pyenv-which)
  43. * [pyenv whence](#pyenv-whence)
  44. * [Development](#development)
  45. * [Version History](#version-history)
  46. * [License](#license)
  47. ## How It Works
  48. At a high level, pyenv intercepts Python commands using shim
  49. executables injected into your `PATH`, determines which Python version
  50. has been specified by your application, and passes your commands along
  51. to the correct Python installation.
  52. ### Understanding PATH
  53. When you run a command like `python` or `pip`, your operating system
  54. searches through a list of directories to find an executable file with
  55. that name. This list of directories lives in an environment variable
  56. called `PATH`, with each directory in the list separated by a colon:
  57. /usr/local/bin:/usr/bin:/bin
  58. Directories in `PATH` are searched from left to right, so a matching
  59. executable in a directory at the beginning of the list takes
  60. precedence over another one at the end. In this example, the
  61. `/usr/local/bin` directory will be searched first, then `/usr/bin`,
  62. then `/bin`.
  63. ### Understanding Shims
  64. pyenv works by inserting a directory of _shims_ at the front of your
  65. `PATH`:
  66. ~/.pyenv/shims:/usr/local/bin:/usr/bin:/bin
  67. Through a process called _rehashing_, pyenv maintains shims in that
  68. directory to match every Python command across every installed version
  69. of Python—`python`, `pip`, and so on.
  70. Shims are lightweight executables that simply pass your command along
  71. to pyenv. So with pyenv installed, when you run, say, `pip`, your
  72. operating system will do the following:
  73. * Search your `PATH` for an executable file named `pip`
  74. * Find the pyenv shim named `pip` at the beginning of your `PATH`
  75. * Run the shim named `pip`, which in turn passes the command along to
  76. pyenv
  77. ### Choosing the Python Version
  78. When you execute a shim, pyenv determines which Python version to use by
  79. reading it from the following sources, in this order:
  80. 1. The `PYENV_VERSION` environment variable, if specified. You can use
  81. the [`pyenv shell`](#pyenv-shell) command to set this environment
  82. variable in your current shell session.
  83. 2. The application-specific `.python-version` file in the current
  84. directory, if present. You can modify the current directory's
  85. `.python-version` file with the [`pyenv local`](#pyenv-local)
  86. command.
  87. 3. The first `.python-version` file found by searching each parent
  88. directory until reaching the root of your filesystem, if any.
  89. 4. The global `~/.pyenv/version` file. You can modify this file using
  90. the [`pyenv global`](#pyenv-global) command. If the global version
  91. file is not present, pyenv assumes you want to use the "system"
  92. Python—i.e. whatever version would be run if pyenv weren't in your
  93. path.
  94. ### Locating the Python Installation
  95. Once pyenv has determined which version of Python your application has
  96. specified, it passes the command along to the corresponding Python
  97. installation.
  98. Each Python version is installed into its own directory under
  99. `~/.pyenv/versions`. For example, you might have these versions
  100. installed:
  101. * `~/.pyenv/versions/2.7.5/`
  102. * `~/.pyenv/versions/3.3.2/`
  103. * `~/.pyenv/versions/pypy-1.9/`
  104. Version names to pyenv are simply the names of the directories in
  105. `~/.pyenv/versions`.
  106. ## Installation
  107. If you're on Mac OS X, consider
  108. [installing with Homebrew](#homebrew-on-mac-os-x).
  109. ### Basic GitHub Checkout
  110. This will get you going with the latest version of pyenv and make it
  111. easy to fork and contribute any changes back upstream.
  112. 1. Check out pyenv where you want it installed. A good place to choose is
  113. `$HOME/.pyenv` but you may install it somewhere else.
  114. $ cd
  115. $ git clone git://github.com/yyuu/pyenv.git .pyenv
  116. 2. Define environment variable `PYENV_ROOT` to point to the path where
  117. pyenv repo is cloned and add `$PYENV_ROOT/bin` to your `$PATH` for access
  118. to the `pyenv` command-line utility.
  119. $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
  120. $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
  121. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  122. **Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
  123. 3. Add pyenv init to your shell to enable shims and autocompletion.
  124. $ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
  125. **Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
  126. **Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
  127. 4. Restart your shell so the path changes take effect. You can now
  128. begin using pyenv.
  129. $ exec $SHELL
  130. 5. Install Python versions into `$PYENV_ROOT/versions`. For example, to
  131. install Python 2.7.5, download and unpack the source, then run:
  132. $ pyenv install 2.7.5
  133. **NOTE** If you need to pass configure option to build, please use
  134. ```CONFIGURE_OPTS``` environment variable.
  135. 6. Rebuild the shim binaries. You should do this any time you install
  136. a new Python binary (for example, when installing a new Python version,
  137. or when installing a package that provides a binary).
  138. $ pyenv rehash
  139. #### Upgrading
  140. If you've installed pyenv using the instructions above, you can
  141. upgrade your installation at any time using git.
  142. To upgrade to the latest development version of pyenv, use `git pull`:
  143. $ cd ~/.pyenv
  144. $ git pull
  145. To upgrade to a specific release of pyenv, check out the corresponding
  146. tag:
  147. $ cd ~/.pyenv
  148. $ git fetch
  149. $ git tag
  150. v0.1.0
  151. $ git checkout v0.1.0
  152. ### Homebrew on Mac OS X
  153. You can also install pyenv using the
  154. [Homebrew](http://brew.sh) package manager on Mac OS
  155. X.
  156. ~~~
  157. $ brew update
  158. $ brew install pyenv
  159. ~~~
  160. To later update these installs, use `upgrade` instead of `install`.
  161. Afterwards you'll still need to add `eval "$(pyenv init -)"` to your
  162. profile as stated in the caveats. You'll only ever have to do this
  163. once.
  164. ### Neckbeard Configuration
  165. Skip this section unless you must know what every line in your shell
  166. profile is doing.
  167. `pyenv init` is the only command that crosses the line of loading
  168. extra commands into your shell. Coming from rvm, some of you might be
  169. opposed to this idea. Here's what `pyenv init` actually does:
  170. 1. Sets up your shims path. This is the only requirement for pyenv to
  171. function properly. You can do this by hand by prepending
  172. `~/.pyenv/shims` to your `$PATH`.
  173. 2. Installs autocompletion. This is entirely optional but pretty
  174. useful. Sourcing `~/.pyenv/completions/pyenv.bash` will set that
  175. up. There is also a `~/.pyenv/completions/pyenv.zsh` for Zsh
  176. users.
  177. 3. Rehashes shims. From time to time you'll need to rebuild your
  178. shim files. Doing this on init makes sure everything is up to
  179. date. You can always run `pyenv rehash` manually.
  180. 4. Installs the sh dispatcher. This bit is also optional, but allows
  181. pyenv and plugins to change variables in your current shell, making
  182. commands like `pyenv shell` possible. The sh dispatcher doesn't do
  183. anything crazy like override `cd` or hack your shell prompt, but if
  184. for some reason you need `pyenv` to be a real script rather than a
  185. shell function, you can safely skip it.
  186. Run `pyenv init -` for yourself to see exactly what happens under the
  187. hood.
  188. ### Uninstalling Python Versions
  189. As time goes on, Python versions you install will accumulate in your
  190. `~/.pyenv/versions` directory.
  191. To remove old Python versions, `pyenv uninstall` command to automate
  192. the removal process.
  193. Or, simply `rm -rf` the directory of the
  194. version you want to remove. You can find the directory of a particular
  195. Python version with the `pyenv prefix` command, e.g. `pyenv prefix
  196. 2.6.8`.
  197. ## Command Reference
  198. Like `git`, the `pyenv` command delegates to subcommands based on its
  199. first argument. The most common subcommands are:
  200. ### pyenv local
  201. Sets a local application-specific Python version by writing the version
  202. name to a `.python-version` file in the current directory. This version
  203. overrides the global version, and can be overridden itself by setting
  204. the `PYENV_VERSION` environment variable or with the `pyenv shell`
  205. command.
  206. $ pyenv local 2.7.5
  207. When run without a version number, `pyenv local` reports the currently
  208. configured local version. You can also unset the local version:
  209. $ pyenv local --unset
  210. Previous versions of pyenv stored local version specifications in a
  211. file named `.pyenv-version`. For backwards compatibility, pyenv will
  212. read a local version specified in an `.pyenv-version` file, but a
  213. `.python-version` file in the same directory will take precedence.
  214. **pyenv feature**
  215. You can specify multiple versions as local Python. Commands
  216. within these Python versions are searched by specified order.
  217. $ pyenv local 2.7.5 3.2.5
  218. $ pyenv local
  219. 2.7.5
  220. 3.2.5
  221. $ pyenv which python2.7
  222. /home/yyuu/.pyenv/versions/2.7.5/bin/python2.7
  223. $ pyenv which python3.2
  224. /home/yyuu/.pyenv/versions/3.2.5/bin/python3.2
  225. $ pyenv which python
  226. /home/yyuu/.pyenv/versions/2.7.5/bin/python
  227. ### pyenv global
  228. Sets the global version of Python to be used in all shells by writing
  229. the version name to the `~/.pyenv/version` file. This version can be
  230. overridden by an application-specific `.python-version` file, or by
  231. setting the `PYENV_VERSION` environment variable.
  232. $ pyenv global 2.7.5
  233. The special version name `system` tells pyenv to use the system Python
  234. (detected by searching your `$PATH`).
  235. When run without a version number, `pyenv global` reports the
  236. currently configured global version.
  237. **pyenv feature**
  238. You can specify multiple versions as global Python. Commands
  239. within these Python versions are searched by specified order.
  240. $ pyenv global 2.7.5 3.2.5
  241. $ pyenv global
  242. 2.7.5
  243. 3.2.5
  244. $ pyenv which python2.7
  245. /home/yyuu/.pyenv/versions/2.7.5/bin/python2.7
  246. $ pyenv which python3.2
  247. /home/yyuu/.pyenv/versions/3.2.5/bin/python3.2
  248. $ pyenv which python
  249. /home/yyuu/.pyenv/versions/2.7.5/bin/python
  250. ### pyenv shell
  251. Sets a shell-specific Python version by setting the `PYENV_VERSION`
  252. environment variable in your shell. This version overrides
  253. application-specific versions and the global version.
  254. $ pyenv shell pypy-1.9
  255. When run without a version number, `pyenv shell` reports the current
  256. value of `PYENV_VERSION`. You can also unset the shell version:
  257. $ pyenv shell --unset
  258. Note that you'll need pyenv's shell integration enabled (step 3 of
  259. the installation instructions) in order to use this command. If you
  260. prefer not to use shell integration, you may simply set the
  261. `PYENV_VERSION` variable yourself:
  262. $ export PYENV_VERSION=pypy-1.9
  263. **pyenv feature**
  264. You can specify multiple versions via `PYENV_VERSION`
  265. environment variable in your shell.
  266. $ pyenv shell pypy-1.9 2.7.5
  267. $ echo $PYENV_VERSION
  268. pypy-1.9:2.7.5
  269. $ pyenv version
  270. pypy-1.9 (set by PYENV_VERSION environment variable)
  271. 2.7.5 (set by PYENV_VERSION environment variable)
  272. ### pyenv versions
  273. Lists all Python versions known to pyenv, and shows an asterisk next to
  274. the currently active version.
  275. $ pyenv versions
  276. 2.5.6
  277. 2.6.8
  278. * 2.7.5 (set by /home/yyuu/.pyenv/version)
  279. 3.2.5
  280. jython-2.5.3
  281. pypy-1.9
  282. ### pyenv version
  283. Displays the currently active Python version, along with information on
  284. how it was set.
  285. $ pyenv version
  286. 2.7.5 (set by /home/yyuu/.pyenv/version)
  287. ### pyenv rehash
  288. Installs shims for all Python binaries known to pyenv (i.e.,
  289. `~/.pyenv/versions/*/bin/*`). Run this command after you install a new
  290. version of Python, or install a package that provides binaries.
  291. $ pyenv rehash
  292. ### pyenv which
  293. Displays the full path to the executable that pyenv will invoke when
  294. you run the given command.
  295. $ pyenv which python3.2
  296. /home/yyuu/.pyenv/versions/3.2.5/bin/python3.2
  297. ### pyenv whence
  298. Lists all Python versions with the given command installed.
  299. $ pyenv whence 2to3
  300. 2.6.8
  301. 2.7.5
  302. 3.2.5
  303. ## Development
  304. The pyenv source code is [hosted on
  305. GitHub](https://github.com/yyuu/pyenv). It's clean, modular,
  306. and easy to understand, even if you're not a shell hacker.
  307. Please feel free to submit pull requests and file bugs on the [issue
  308. tracker](https://github.com/yyuu/pyenv/issues).
  309. ### Version History
  310. See CHANGELOG.md.
  311. ### License
  312. (The MIT license)
  313. * Copyright (c) 2013 Yamashita, Yuu
  314. * Copyright (c) 2013 Sam Stephenson
  315. Permission is hereby granted, free of charge, to any person obtaining
  316. a copy of this software and associated documentation files (the
  317. "Software"), to deal in the Software without restriction, including
  318. without limitation the rights to use, copy, modify, merge, publish,
  319. distribute, sublicense, and/or sell copies of the Software, and to
  320. permit persons to whom the Software is furnished to do so, subject to
  321. the following conditions:
  322. The above copyright notice and this permission notice shall be
  323. included in all copies or substantial portions of the Software.
  324. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  325. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  326. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  327. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  328. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  329. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  330. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.