| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 1 | .. highlightlang:: none | 
 | 2 |  | 
 | 3 | .. _installing-index: | 
 | 4 |  | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 5 | ************************* | 
 | 6 | Installing Python Modules | 
 | 7 | ************************* | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 8 |  | 
 | 9 | :Email: distutils-sig@python.org | 
 | 10 |  | 
 | 11 | As a popular open source development project, Python has an active | 
 | 12 | supporting community of contributors and users that also make their software | 
 | 13 | available for other Python developers to use under open source license terms. | 
 | 14 |  | 
 | 15 | This allows Python users to share and collaborate effectively, benefiting | 
 | 16 | from the solutions others have already created to common (and sometimes | 
 | 17 | even rare!) problems, as well as potentially contributing their own | 
 | 18 | solutions to the common pool. | 
 | 19 |  | 
 | 20 | This guide covers the installation part of the process. For a guide to | 
 | 21 | creating and sharing your own Python projects, refer to the | 
 | 22 | :ref:`distribution guide <distributing-index>`. | 
 | 23 |  | 
 | 24 | .. note:: | 
 | 25 |  | 
 | 26 |    For corporate and other institutional users, be aware that many | 
 | 27 |    organisations have their own policies around using and contributing to | 
 | 28 |    open source software. Please take such policies into account when making | 
 | 29 |    use of the distribution and installation tools provided with Python. | 
 | 30 |  | 
 | 31 |  | 
 | 32 | Key terms | 
 | 33 | ========= | 
 | 34 |  | 
 | 35 | * ``pip`` is the preferred installer program. Starting with Python 3.4, it | 
 | 36 |   is included by default with the Python binary installers. | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 37 | * A *virtual environment* is a semi-isolated Python environment that allows | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 38 |   packages to be installed for use by a particular application, rather than | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 39 |   being installed system wide. | 
 | 40 | * ``venv`` is the standard tool for creating virtual environments, and has | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 41 |   been part of Python since Python 3.3. Starting with Python 3.4, it | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 42 |   defaults to installing ``pip`` into all created virtual environments. | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 43 | * ``virtualenv`` is a third party alternative (and predecessor) to | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 44 |   ``venv``. It allows virtual environments to be used on versions of | 
 | 45 |   Python prior to 3.4, which either don't provide ``venv`` at all, or | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 46 |   aren't able to automatically install ``pip`` into created environments. | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 47 | * The `Python Packaging Index <https://pypi.python.org/pypi>`__ is a public | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 48 |   repository of open source licensed packages made available for use by | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 49 |   other Python users. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 50 | * the `Python Packaging Authority | 
| Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 51 |   <https://www.pypa.io/en/latest/>`__ are the group of | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 52 |   developers and documentation authors responsible for the maintenance and | 
 | 53 |   evolution of the standard packaging tools and the associated metadata and | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 54 |   file format standards. They maintain a variety of tools, documentation, | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 55 |   and issue trackers on both `GitHub <https://github.com/pypa>`__ and | 
 | 56 |   `BitBucket <https://bitbucket.org/pypa/>`__. | 
 | 57 | * ``distutils`` is the original build and distribution system first added to | 
 | 58 |   the Python standard library in 1998. While direct use of ``distutils`` is | 
 | 59 |   being phased out, it still laid the foundation for the current packaging | 
 | 60 |   and distribution infrastructure, and it not only remains part of the | 
 | 61 |   standard library, but its name lives on in other ways (such as the name | 
 | 62 |   of the mailing list used to coordinate Python packaging standards | 
 | 63 |   development). | 
 | 64 |  | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 65 | .. deprecated:: 3.6 | 
 | 66 |    ``pyvenv`` was the recommended tool for creating virtual environments for | 
 | 67 |    Python 3.3 and 3.4, and is `deprecated in Python 3.6 | 
 | 68 |    <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_. | 
 | 69 |  | 
 | 70 | .. versionchanged:: 3.5 | 
 | 71 |    The use of ``venv`` is now recommended for creating virtual environments. | 
 | 72 |  | 
 | 73 | .. seealso:: | 
 | 74 |  | 
 | 75 |    `Python Packaging User Guide: Creating and using virtual environments | 
 | 76 |    <https://packaging.python.org/installing/#creating-virtual-environments>`__ | 
 | 77 |  | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 78 |  | 
 | 79 | Basic usage | 
 | 80 | =========== | 
 | 81 |  | 
 | 82 | The standard packaging tools are all designed to be used from the command | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 83 | line. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 84 |  | 
 | 85 | The following command will install the latest version of a module and its | 
| Nick Coghlan | 5c4fbd5 | 2014-10-04 21:11:25 +1000 | [diff] [blame] | 86 | dependencies from the Python Packaging Index:: | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 87 |  | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 88 |     python -m pip install SomePackage | 
 | 89 |  | 
 | 90 | .. note:: | 
 | 91 |  | 
 | 92 |    For POSIX users (including Mac OS X and Linux users), the examples in | 
 | 93 |    this guide assume the use of a :term:`virtual environment`. | 
 | 94 |  | 
 | 95 |    For Windows users, the examples in this guide assume that the option to | 
 | 96 |    adjust the system PATH environment variable was selected when installing | 
 | 97 |    Python. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 98 |  | 
 | 99 | It's also possible to specify an exact or minimum version directly on the | 
| Senthil Kumaran | 76d9a6b | 2016-01-17 18:42:13 -0800 | [diff] [blame] | 100 | command line. When using comparator operators such as ``>``, ``<`` or some other | 
 | 101 | special character which get interpreted by shell, the package name and the | 
 | 102 | version should be enclosed within double quotes:: | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 103 |  | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 104 |     python -m pip install SomePackage==1.0.4    # specific version | 
| Senthil Kumaran | 76d9a6b | 2016-01-17 18:42:13 -0800 | [diff] [blame] | 105 |     python -m pip install "SomePackage>=1.0.4"  # minimum version | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 106 |  | 
 | 107 | Normally, if a suitable module is already installed, attempting to install | 
 | 108 | it again will have no effect. Upgrading existing modules must be requested | 
 | 109 | explicitly:: | 
 | 110 |  | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 111 |     python -m pip install --upgrade SomePackage | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 112 |  | 
 | 113 | More information and resources regarding ``pip`` and its capabilities can be | 
| Georg Brandl | e73778c | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 114 | found in the `Python Packaging User Guide <https://packaging.python.org>`__. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 115 |  | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 116 | Creation of virtual environments is done through the :mod:`venv` module. | 
 | 117 | Installing packages into an active virtual environment uses the commands shown | 
 | 118 | above. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 119 |  | 
 | 120 | .. seealso:: | 
 | 121 |  | 
| Nick Coghlan | 5c4fbd5 | 2014-10-04 21:11:25 +1000 | [diff] [blame] | 122 |     `Python Packaging User Guide: Installing Python Distribution Packages | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 123 |     <https://packaging.python.org/installing/>`__ | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 124 |  | 
 | 125 |  | 
 | 126 | How do I ...? | 
 | 127 | ============= | 
 | 128 |  | 
 | 129 | These are quick answers or links for some common tasks. | 
 | 130 |  | 
 | 131 | ... install ``pip`` in versions of Python prior to Python 3.4? | 
 | 132 | -------------------------------------------------------------- | 
 | 133 |  | 
 | 134 | Python only started bundling ``pip`` with Python 3.4. For earlier versions, | 
 | 135 | ``pip`` needs to be "bootstrapped" as described in the Python Packaging | 
 | 136 | User Guide. | 
 | 137 |  | 
 | 138 | .. seealso:: | 
 | 139 |  | 
| Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 140 |    `Python Packaging User Guide: Requirements for Installing Packages | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 141 |    <https://packaging.python.org/installing/#requirements-for-installing-packages>`__ | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 142 |  | 
 | 143 |  | 
 | 144 | .. installing-per-user-installation: | 
 | 145 |  | 
 | 146 | ... install packages just for the current user? | 
 | 147 | ----------------------------------------------- | 
 | 148 |  | 
| Nick Coghlan | 1d52096 | 2014-09-06 20:38:23 +1000 | [diff] [blame] | 149 | Passing the ``--user`` option to ``python -m pip install`` will install a | 
 | 150 | package just for the current user, rather than for all users of the system. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 151 |  | 
 | 152 |  | 
 | 153 | ... install scientific Python packages? | 
 | 154 | --------------------------------------- | 
 | 155 |  | 
 | 156 | A number of scientific Python packages have complex binary dependencies, and | 
 | 157 | aren't currently easy to install using ``pip`` directly. At this point in | 
 | 158 | time, it will often be easier for users to install these packages by | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 159 | `other means <https://packaging.python.org/science/>`__ | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 160 | rather than attempting to install them with ``pip``. | 
 | 161 |  | 
 | 162 | .. seealso:: | 
 | 163 |  | 
 | 164 |    `Python Packaging User Guide: Installing Scientific Packages | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 165 |    <https://packaging.python.org/science/>`__ | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 166 |  | 
 | 167 |  | 
 | 168 | ... work with multiple versions of Python installed in parallel? | 
 | 169 | ---------------------------------------------------------------- | 
 | 170 |  | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 171 | On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 172 | in combination with the ``-m`` switch to run the appropriate copy of | 
 | 173 | ``pip``:: | 
 | 174 |  | 
 | 175 |    python2   -m pip install SomePackage  # default Python 2 | 
 | 176 |    python2.7 -m pip install SomePackage  # specifically Python 2.7 | 
 | 177 |    python3   -m pip install SomePackage  # default Python 3 | 
 | 178 |    python3.4 -m pip install SomePackage  # specifically Python 3.4 | 
 | 179 |  | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 180 | Appropriately versioned ``pip`` commands may also be available. | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 181 |  | 
 | 182 | On Windows, use the ``py`` Python launcher in combination with the ``-m`` | 
 | 183 | switch:: | 
 | 184 |  | 
 | 185 |    py -2   -m pip install SomePackage  # default Python 2 | 
 | 186 |    py -2.7 -m pip install SomePackage  # specifically Python 2.7 | 
 | 187 |    py -3   -m pip install SomePackage  # default Python 3 | 
 | 188 |    py -3.4 -m pip install SomePackage  # specifically Python 3.4 | 
 | 189 |  | 
 | 190 | .. other questions: | 
 | 191 |  | 
 | 192 |    Once the Development & Deployment part of PPUG is fleshed out, some of | 
 | 193 |    those sections should be linked from new questions here (most notably, | 
 | 194 |    we should have a question about avoiding depending on PyPI that links to | 
| Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 195 |    https://packaging.python.org/en/latest/mirrors/) | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 196 |  | 
 | 197 |  | 
 | 198 | Common installation issues | 
 | 199 | ========================== | 
 | 200 |  | 
 | 201 | Installing into the system Python on Linux | 
 | 202 | ------------------------------------------ | 
 | 203 |  | 
 | 204 | On Linux systems, a Python installation will typically be included as part | 
 | 205 | of the distribution. Installing into this Python installation requires | 
 | 206 | root access to the system, and may interfere with the operation of the | 
 | 207 | system package manager and other components of the system if a component | 
 | 208 | is unexpectedly upgraded using ``pip``. | 
 | 209 |  | 
 | 210 | On such systems, it is often better to use a virtual environment or a | 
 | 211 | per-user installation when installing packages with ``pip``. | 
 | 212 |  | 
 | 213 |  | 
 | 214 | Installing binary extensions | 
 | 215 | ---------------------------- | 
 | 216 |  | 
 | 217 | Python has typically relied heavily on source based distribution, with end | 
 | 218 | users being expected to compile extension modules from source as part of | 
 | 219 | the installation process. | 
 | 220 |  | 
 | 221 | With the introduction of support for the binary ``wheel`` format, and the | 
 | 222 | ability to publish wheels for at least Windows and Mac OS X through the | 
| Nick Coghlan | 5c4fbd5 | 2014-10-04 21:11:25 +1000 | [diff] [blame] | 223 | Python Packaging Index, this problem is expected to diminish over time, | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 224 | as users are more regularly able to install pre-built extensions rather | 
 | 225 | than needing to build them themselves. | 
 | 226 |  | 
 | 227 | Some of the solutions for installing `scientific software | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 228 | <https://packaging.python.org/science/>`__ | 
 | 229 | that are not yet available as pre-built ``wheel`` files may also help with | 
| Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 230 | obtaining other binary extensions without needing to build them locally. | 
 | 231 |  | 
 | 232 | .. seealso:: | 
 | 233 |  | 
 | 234 |    `Python Packaging User Guide: Binary Extensions | 
| Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 235 |    <https://packaging.python.org/extensions/>`__ |