Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

344 řádky
14 KiB

před 7 roky
před 7 roky
před 8 roky
před 11 roky
  1. # python-build
  2. python-build is a [pyenv](https://github.com/pyenv/pyenv) plugin that
  3. provides a `pyenv install` command to compile and install different versions
  4. of Python on UNIX-like systems.
  5. You can also use python-build without pyenv in environments where you need
  6. precise control over Python version installation.
  7. See the [list of releases](https://github.com/pyenv/pyenv/releases)
  8. for changes in each version.
  9. ## Installation
  10. ### Installing as a pyenv plugin (recommended)
  11. Since python-build is bundled with pyenv by
  12. default, you do not need to do anything.
  13. ### Installing as a standalone program (advanced)
  14. Installing python-build as a standalone program will give you access to the
  15. `python-build` command for precise control over Python version installation. If you
  16. have pyenv installed, you will also be able to use the `pyenv install` command.
  17. git clone https://github.com/pyenv/pyenv.git
  18. cd pyenv/plugins/python-build
  19. ./install.sh
  20. This will install python-build into `/usr/local`. If you do not have write
  21. permission to `/usr/local`, you will need to run `sudo ./install.sh` instead.
  22. You can install to a different prefix by setting the `PREFIX` environment
  23. variable.
  24. To update python-build after it has been installed, run `git pull` in your cloned
  25. copy of the repository, then re-run the install script.
  26. ### Installing with Homebrew (for OS X users)
  27. Mac OS X users can install python-build with the [Homebrew](http://brew.sh)
  28. package manager. This will give you access to the `python-build` command. If you
  29. have pyenv installed, you will also be able to use the `pyenv install` command.
  30. *This is the recommended method of installation if you installed pyenv with
  31. Homebrew.*
  32. brew install pyenv
  33. Or, if you would like to install the latest development release:
  34. brew install --HEAD pyenv
  35. ## Usage
  36. Before you begin, you should ensure that your build environment has the proper
  37. system dependencies for compiling the wanted Python Version (see our [recommendations](https://github.com/pyenv/pyenv/wiki#suggested-build-environment)).
  38. ### Using `pyenv install` with pyenv
  39. To install a Python version for use with pyenv, run `pyenv install` with
  40. exact name of the version you want to install. For example,
  41. pyenv install 2.7.4
  42. Python versions will be installed into a directory of the same name under
  43. `~/.pyenv/versions`.
  44. To see a list of all available Python versions, run `pyenv install --list`. You
  45. may also tab-complete available Python versions if your pyenv installation is
  46. properly configured.
  47. ### Using `python-build` standalone
  48. If you have installed python-build as a standalone program, you can use the
  49. `python-build` command to compile and install Python versions into specific
  50. locations.
  51. Run the `python-build` command with the exact name of the version you want to
  52. install and the full path where you want to install it. For example,
  53. python-build 2.7.4 ~/local/python-2.7.4
  54. To see a list of all available Python versions, run `python-build --definitions`.
  55. Pass the `-v` or `--verbose` flag to `python-build` as the first argument to see
  56. what's happening under the hood.
  57. ### Custom definitions
  58. Both `pyenv install` and `python-build` accept a path to a custom definition file
  59. in place of a version name. Custom definitions let you develop and install
  60. versions of Python that are not yet supported by python-build.
  61. See the [python-build built-in definitions](https://github.com/pyenv/pyenv/tree/master/plugins/python-build/share/python-build) as a starting point for
  62. custom definition files.
  63. #### Adding definitions with a Pyenv plugin
  64. You can add your own definitions with a [Pyenv plugin](https://github.com/pyenv/pyenv?tab=readme-ov-file#pyenv-plugins) by placing them under
  65. `$PYENV_ROOT/plugins/your_plugin_name/share/python-build`.
  66. ### Default build configuration
  67. Without the user customizing the build with environment variables (see below),
  68. `python-build` builds Python with mostly default Configure options
  69. to maintain the principle of the least surprise.
  70. The exceptions -- non-default options that are set by default -- are listed below:
  71. | Option/Behavior | Rationale |
  72. |-----------------|-----------|
  73. | `--enable-shared` is on by default. Pass `--disable-shared` to Configure options to override | The official CPython Docker image uses it. It's required to embed CPython. |
  74. | argument to `--enable-universalsdk` is ignored and set to `/` |
  75. | `--with-universal-archs` defaults to `universal2` on ARM64 architecture | the only dual-architecture Macs in use today are Apple Silicon which can only build that one |
  76. | argument to `--enable-framework` is ignored and set to a specific value | CPython's build logic requires a very specific argument to avoid installing the `Applications` part globally |
  77. | argument to `--enable-unicode` in non-MacOS is overridden to `ucs4` for 2.x-3.3 |
  78. | `MACOSX_DEPLOYMENT_TARGET` defaults to the running MacOS version |
  79. #### Integration with 3rd-party package ecosystems
  80. ##### Homebrew
  81. Homebrew is used to find dependency packages if `brew` is found on `PATH`:
  82. * In MacOS, or
  83. * If the running Pyenv itself is installed with Homebrew
  84. Set `PYTHON_BUILD_USE_HOMEBREW` or `PYTHON_BUILD_SKIP_HOMEBREW` to override this default.
  85. When Homebrew is used, its `include` and `lib` paths are added to compiler search path (the latter is also set as `rpath`),
  86. and also Python dependencies that are typically keg-only are searched for in the Homebrew installation and added individually.
  87. **NOTE:** Homebrew is not used in Linux by default because it's rolling-release which causes a problem.
  88. Upgrading a Python dependency in Homebrew to a new major version (that `brew` does without warning)
  89. would break all Pyenv-managed installations that depend on it.
  90. You can use a [community plugin `fix-version`](https://github.com/pyenv/pyenv/wiki/Plugins#community-plugins)
  91. to fix installations in such a case.
  92. ##### Portage
  93. In FreeBSD, if `pkg` is on PATH, Ports are searched for some dependencies that Configure is known to not search for via `pkg-config`.
  94. (Later versions of CPython search for more packages via `pkg-config` so this may eventually become redundant.)
  95. ### Special environment variables
  96. You can set certain environment variables to control the build process.
  97. * `TMPDIR` sets the location where python-build stores temporary files.
  98. * `PYTHON_BUILD_BUILD_PATH` sets the location in which sources are downloaded and
  99. built. By default, this is a subdirectory of `TMPDIR`.
  100. * `PYTHON_BUILD_CACHE_PATH`, if set, specifies a directory to use for caching
  101. downloaded package files.
  102. * `PYTHON_BUILD_MIRROR_URL` overrides the default mirror URL root to one of your
  103. choosing.
  104. * `PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM`, if set, does not append the SHA2
  105. checksum of the file to the mirror URL.
  106. * `PYTHON_BUILD_SKIP_MIRROR`, if set, forces python-build to download packages from
  107. their original source URLs instead of using a mirror.
  108. * `PYTHON_BUILD_HTTP_CLIENT`, explicitly specify the HTTP client type to use. `aria2`, `curl` and `wget` are the supported values and by default, are searched in that order.
  109. * `PYTHON_BUILD_CURL_OPTS`, `PYTHON_BUILD_WGET_OPTS`, `PYTHON_BUILD_ARIA2_OPTS` pass additional parameters to the corresponding HTTP client.
  110. * `PYTHON_BUILD_SKIP_HOMEBREW`, if set, will not search for libraries installed by Homebrew when it would normally will.
  111. * `PYTHON_BUILD_USE_HOMEBREW`, if set, will search for libraries installed by Homebrew when it would normally not.
  112. * `PYTHON_BUILD_HOMEBREW_OPENSSL_FORMULA`, override the Homebrew OpenSSL formula to use.
  113. * `PYTHON_BUILD_ROOT` overrides the default location from where build definitions
  114. in `share/python-build/` are looked up.
  115. * `PYTHON_BUILD_DEFINITIONS` can be a list of colon-separated paths that get
  116. additionally searched when looking up build definitions.
  117. * `CC` sets the path to the C compiler.
  118. * `PYTHON_CFLAGS` lets you pass additional options to the default `CFLAGS`. Use
  119. this to override, for instance, the `-O3` option.
  120. * `CONFIGURE_OPTS` lets you pass additional options to `./configure`.
  121. * `MAKE` lets you override the command to use for `make`. Useful for specifying
  122. GNU make (`gmake`) on some systems.
  123. * `MAKE_OPTS` (or `MAKEOPTS`) lets you pass additional options to `make`.
  124. * `MAKE_INSTALL_OPTS` lets you pass additional options to `make install`.
  125. * `PYTHON_CONFIGURE_OPTS` and `PYTHON_MAKE_OPTS` and `PYTHON_MAKE_INSTALL_OPTS` allow
  126. you to specify configure and make options for building CPython. These variables
  127. will be passed to Python only, not any dependent packages (e.g. libyaml).
  128. ### Applying patches to Python before compiling
  129. Both `pyenv install` and `python-build` support the `--patch` (`-p`) flag that
  130. signals that a patch from stdin should be applied to Python, Jython or PyPy
  131. source code before the `./configure` and compilation steps.
  132. Example usage:
  133. ```sh
  134. # applying a single patch
  135. $ pyenv install --patch 2.7.10 < /path/to/python.patch
  136. # applying a patch from HTTP
  137. $ pyenv install --patch 2.7.10 < <(curl -sSL http://git.io/python.patch)
  138. # applying multiple patches
  139. $ cat fix1.patch fix2.patch | pyenv install --patch 2.7.10
  140. ```
  141. ### Building for maximum performance
  142. Building CPython with `--enable-optimizations` will result in a faster
  143. interpreter at the cost of significantly longer build times. Most notably, this
  144. enables PGO (profile guided optimization). While your mileage may vary, it is
  145. common for performance improvement from this to be in the ballpark of 30%.
  146. ```sh
  147. env PYTHON_CONFIGURE_OPTS='--enable-optimizations --with-lto' PYTHON_CFLAGS='-march=native -mtune=native' pyenv install --verbose 3.6.0
  148. ```
  149. You can also customize the task used for profile guided optimization by setting
  150. the `PROFILE_TASK` environment variable, for instance, `PROFILE_TASK='-m
  151. test.regrtest --pgo -j0'` will run much faster than the default task.
  152. ### Checksum verification
  153. If you have the `shasum`, `openssl`, or `sha256sum` tool installed, python-build will
  154. automatically verify the SHA2 checksum of each downloaded package before
  155. installing it.
  156. Checksums are optional and specified as anchors on the package URL in each
  157. definition. (All bundled definitions include checksums.)
  158. ### Package download mirrors
  159. python-build will first attempt to download package files from a mirror hosted on
  160. GitHub Pages. If this fails, it will fall back to the
  161. official URL specified in the definition file.
  162. You can point python-build to another mirror by specifying the
  163. `PYTHON_BUILD_MIRROR_URL` environment variable.
  164. Package mirror URLs are constructed by joining
  165. `$PYTHON_BUILD_MIRROR_URL` with the SHA2 checksum of the package file as specified in the URL
  166. in the installation script (the part after the hash sign). E.g.:
  167. ```
  168. https://mycache.example.com/0419e9085bf51b7a672009b3f50dbf1859acdf18ba725d0ec19aa5c8503f0ea3
  169. ```
  170. If you have replicated the directory structure of an official site, the easiest way to adapt
  171. would be to make symlinks at the mirror's root:
  172. ```
  173. 0419e9085bf51b7a672009b3f50dbf1859acdf18ba725d0ec19aa5c8503f0ea3 -> 3.10.10/Python-3.10.10.tar.xz
  174. ```
  175. The rationale is to abstract away difference between directory structures of sites
  176. of various Python flavors and their occasional changes as well as to accomodate
  177. people who only wish to cache some select downloads. This also allows to mirror multiple sites at once.
  178. If the mirror being used does not have the same checksum (*e.g.* with a
  179. pull-through cache like Artifactory), you can set the
  180. `PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM` environment variable.
  181. If you don't have an SHA2 program installed, python-build will skip the download
  182. mirror and use official URLs instead. You can force python-build to bypass the
  183. mirror by setting the `PYTHON_BUILD_SKIP_MIRROR` environment variable.
  184. The official python-build download mirror is provided by
  185. [GitHub Pages](http://yyuu.github.io/pythons/).
  186. ### Package download cache
  187. Python-build will keep a cache of downloaded package files
  188. at the location specified by the `PYTHON_BUILD_CACHE_PATH` environment variable
  189. if it exists. The default is `~/.pyenv/cache`, so you can
  190. enable caching by just creating that directory.
  191. The name of the would-be cached file is reported by Pyenv in the "Downloading &lt;filename&gt;..." message.
  192. It's possible to warm up the cache by manually putting the file there under an appropriate name.
  193. ### Keeping the build directory after installation
  194. Both `python-build` and `pyenv install` accept the `-k` or `--keep` flag, which
  195. tells python-build to keep the downloaded source after installation. This can be
  196. useful if you need to use `gdb` and `memprof` with Python.
  197. Source code will be kept in a parallel directory tree `~/.pyenv/sources` when
  198. using `--keep` with the `pyenv install` command. You should specify the
  199. location of the source code with the `PYTHON_BUILD_BUILD_PATH` environment
  200. variable when using `--keep` with `python-build`.
  201. ## Getting Help
  202. Please see the [pyenv wiki](https://github.com/pyenv/pyenv/wiki) for solutions to common problems.
  203. [wiki]: https://github.com/pyenv/pyenv/wiki
  204. If you can't find an answer on the wiki, open an issue on the [issue
  205. tracker](https://github.com/pyenv/pyenv/issues). Be sure to include
  206. the full build log for build failures.
  207. ## Contributing
  208. ### Testing new python versions
  209. If you are contributing a new python version for python-build,
  210. you can test the build in a [docker](https://www.docker.com/) container based on Ubuntu 18.04.
  211. With docker installed:
  212. ```sh
  213. docker build -t my_container .
  214. docker run my_container pyenv install <my_version>
  215. ```
  216. To enter a shell which will allow you to build and then test a python version,
  217. replace the second line with
  218. ```sh
  219. docker run -it my_container
  220. ```
  221. The container will need to be rebuilt whenever you change the repo,
  222. but after the first build, this will be very fast,
  223. as the layer including the build dependencies will be cached.
  224. Changes made inside the container will not be persisted.
  225. To test *all* new versions since a particular revision (e.g. `master`), `cd` to the root of your `pyenv` repo, and run this script:
  226. ```sh
  227. set -e
  228. set -x
  229. docker build -t pyenv-test-container .
  230. git diff --name-only master \
  231. | grep '^plugins/python-build/share/python-build/' \
  232. | awk -F '/' '{print $NF}' \
  233. | xargs -I _ docker run pyenv-test-container pyenv install _
  234. ```
  235. - Build the docker image with the **t**ag pyenv-test-container
  236. - Look for the names files changed since revision `master`
  237. - Filter out any which don't live where python-build keeps its build scripts
  238. - Look only at the file name (i.e. the python version name)
  239. - Run a new docker container for each, building that version