python.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. .. _python:
  2. ===========================
  3. Python and the Build System
  4. ===========================
  5. The Python programming language is used significantly in the build
  6. system. If we need to write code for the build system or for a tool
  7. related to the build system, Python is typically the first choice.
  8. Python Requirements
  9. ===================
  10. The tree requires Python 2.7.3 or greater but not Python 3 to build.
  11. All Python packages not in the Python distribution are included in the
  12. source tree. So all you should need is a vanilla Python install and you
  13. should be good to go.
  14. Only CPython (the Python distribution available from www.python.org) is
  15. supported.
  16. We require Python 2.7.3 (and not say 2.7.2) to build because Python
  17. 2.7.3 contains numerous bug fixes, especially around the area of Unicode
  18. handling. These bug fixes are extremely annoying and have to be worked
  19. around. The build maintainers were tired of doing this, so the minimum
  20. version requirement was upped (bug 870420).
  21. We intend to eventually support Python 3. This will come by way of dual
  22. 2.7/3.x compatibility because a single flag day conversion to 3.x will
  23. be too cumbersome given the amount of Python that would need converted.
  24. We will not know which 3.x minor release we are targeting until this
  25. effort is underway. This is tracked in bug 636155.
  26. Compiled Python Packages
  27. ========================
  28. There are some features of the build that rely on compiled Python packages
  29. (packages containing C source). These features are currently all
  30. optional because not every system contains the Python development
  31. headers required to build these extensions.
  32. We recommend you have the Python development headers installed (``mach
  33. bootstrap`` should do this for you) so you can take advantage of these
  34. features.
  35. Issues with OS X System Python
  36. ==============================
  37. The Python that ships with OS X has historically been littered with
  38. subtle bugs and suboptimalities. Furthermore, OS X up through 10.8 don't
  39. ship with Python 2.7.3 (10.8 ships with 2.7.2).
  40. OS X 10.8 and below users will be required to install a new Python
  41. distribution. This may not be necessary for OS X 10.9+. However, we
  42. still recommend installing a separate Python because of the history with
  43. OS X's system Python issues.
  44. We recommend installing Python through Homebrew or MacPorts. If you run
  45. ``mach bootstrap``, this should be done for you.
  46. Virtualenvs
  47. ===========
  48. The build system relies heavily on
  49. `virtualenvs <http://www.virtualenv.org/en/latest/>`_. Virtualenvs are
  50. standalone and isolated Python environments. The problem a virtualenv
  51. solves is that of dependencies across multiple Python components. If two
  52. components on a system relied on different versions of a package, there
  53. could be a conflict. Instead of managing multiple versions of a package
  54. simultaneously, Python and virtualenvs take the route that it is easier
  55. to just keep them separate so there is no potential for conflicts.
  56. Very early in the build process, a virtualenv is created inside the
  57. :term:`object directory`. The virtualenv is configured such that it can
  58. find all the Python packages in the source tree. The code for this lives
  59. in :py:mod:`mozbuild.virtualenv`.
  60. Deficiencies
  61. ------------
  62. There are numerous deficiencies with the way virtualenvs are handled in
  63. the build system.
  64. * mach reinvents the virtualenv.
  65. There is code in ``build/mach_bootstrap.py`` that configures ``sys.path``
  66. much the same way the virtualenv does. There are various bugs tracking
  67. this. However, no clear solution has yet been devised. It's not a huge
  68. problem and thus not a huge priority.
  69. * They aren't preserved across copies and packaging.
  70. If you attempt to copy an entire tree from one machine to another or
  71. from one directory to another, chances are the virtualenv will fall
  72. apart. It would be nice if we could preserve it somehow. Instead of
  73. actually solving portable virtualenvs, all we really need to solve is
  74. encapsulating the logic for populating the virtualenv along with all
  75. dependent files in the appropriate place.
  76. * .pyc files written to source directory.
  77. We rely heavily on ``.pth`` files in our virtualenv. A ``.pth`` file
  78. is a special file that contains a list of paths. Python will take the
  79. set of listed paths encountered in ``.pth`` files and add them to
  80. ``sys.path``.
  81. When Python compiles a ``.py`` file to bytecode, it writes out a
  82. ``.pyc`` file so it doesn't have to perform this compilation again.
  83. It puts these ``.pyc`` files alongside the ``.pyc`` file. Python
  84. provides very little control for determing where these ``.pyc`` files
  85. go, even in Python 3 (which offers customer importers).
  86. With ``.pth`` files pointing back to directories in the source tree
  87. and not the object directory, ``.pyc`` files are created in the source
  88. tree. This is bad because when Python imports a module, it first looks
  89. for a ``.pyc`` file before the ``.py`` file. If there is a ``.pyc``
  90. file but no ``.py`` file, it will happily import the module. This
  91. wreaks havoc during file moves, refactoring, etc.
  92. There are various proposals for fixing this. See bug 795995.
  93. Installing Python Manually
  94. ==========================
  95. We highly recommend you use your system's package manager or a
  96. well-supported 3rd party package manager to install Python for you. If
  97. these are not available to you, we recommend the following tools for
  98. installing Python:
  99. * `buildout.python <https://github.com/collective/buildout.python>`_
  100. * `pyenv <https://github.com/yyuu/pyenv>`_
  101. * An official installer from http://www.python.org.
  102. If all else fails, consider compiling Python from source manually. But this
  103. should be viewed as the least desirable option.
  104. Common Issues with Python
  105. =========================
  106. Upgrading your Python distribution breaks the virtualenv
  107. --------------------------------------------------------
  108. If you upgrade the Python distribution (e.g. install Python 2.7.5
  109. from 2.7.3, chances are parts of the virtualenv will break.
  110. This commonly manifests as a cryptic ``Cannot import XXX`` exception.
  111. More often than not, the module being imported contains binary/compiled
  112. components.
  113. If you upgrade or reinstall your Python distribution, we recommend
  114. clobbering your build.
  115. Packages installed at the system level conflict with build system's
  116. -------------------------------------------------------------------
  117. It is common for people to install Python packages using ``sudo`` (e.g.
  118. ``sudo pip install psutil``) or with the system's package manager
  119. (e.g. ``apt-get install python-mysql``.
  120. A problem with this is that packages installed at the system level may
  121. conflict with the package provided by the source tree. As of bug 907902
  122. and changeset f18eae7c3b27 (September 16, 2013), this should no longer
  123. be an issue since the virtualenv created as part of the build doesn't
  124. add the system's ``site-packages`` directory to ``sys.path``. However,
  125. poorly installed packages may still find a way to creep into the mix and
  126. interfere with our virtualenv.
  127. As a general principle, we recommend against using your system's package
  128. manager or using ``sudo`` to install Python packages. Instead, create
  129. virtualenvs and isolated Python environments for all of your Python
  130. projects.
  131. Python on $PATH is not appropriate
  132. ----------------------------------
  133. Tools like ``mach`` will look for Python by performing ``/usr/bin/env
  134. python`` or equivalent. Please be sure the appropriate Python 2.7.3+
  135. path is on $PATH. On OS X, this likely means you'll need to modify your
  136. shell's init script to put something ahead of ``/usr/bin``.