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