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