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