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