17db96d56Sopenharmony_ci.. highlight:: none
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _installing-index:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ci*************************
67db96d56Sopenharmony_ciInstalling Python Modules
77db96d56Sopenharmony_ci*************************
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci:Email: distutils-sig@python.org
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ciAs a popular open source development project, Python has an active
127db96d56Sopenharmony_cisupporting community of contributors and users that also make their software
137db96d56Sopenharmony_ciavailable for other Python developers to use under open source license terms.
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ciThis allows Python users to share and collaborate effectively, benefiting
167db96d56Sopenharmony_cifrom the solutions others have already created to common (and sometimes
177db96d56Sopenharmony_cieven rare!) problems, as well as potentially contributing their own
187db96d56Sopenharmony_cisolutions to the common pool.
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ciThis guide covers the installation part of the process. For a guide to
217db96d56Sopenharmony_cicreating and sharing your own Python projects, refer to the
227db96d56Sopenharmony_ci:ref:`distribution guide <distributing-index>`.
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci.. note::
257db96d56Sopenharmony_ci
267db96d56Sopenharmony_ci   For corporate and other institutional users, be aware that many
277db96d56Sopenharmony_ci   organisations have their own policies around using and contributing to
287db96d56Sopenharmony_ci   open source software. Please take such policies into account when making
297db96d56Sopenharmony_ci   use of the distribution and installation tools provided with Python.
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ciKey terms
337db96d56Sopenharmony_ci=========
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci* ``pip`` is the preferred installer program. Starting with Python 3.4, it
367db96d56Sopenharmony_ci  is included by default with the Python binary installers.
377db96d56Sopenharmony_ci* A *virtual environment* is a semi-isolated Python environment that allows
387db96d56Sopenharmony_ci  packages to be installed for use by a particular application, rather than
397db96d56Sopenharmony_ci  being installed system wide.
407db96d56Sopenharmony_ci* ``venv`` is the standard tool for creating virtual environments, and has
417db96d56Sopenharmony_ci  been part of Python since Python 3.3. Starting with Python 3.4, it
427db96d56Sopenharmony_ci  defaults to installing ``pip`` into all created virtual environments.
437db96d56Sopenharmony_ci* ``virtualenv`` is a third party alternative (and predecessor) to
447db96d56Sopenharmony_ci  ``venv``. It allows virtual environments to be used on versions of
457db96d56Sopenharmony_ci  Python prior to 3.4, which either don't provide ``venv`` at all, or
467db96d56Sopenharmony_ci  aren't able to automatically install ``pip`` into created environments.
477db96d56Sopenharmony_ci* The `Python Package Index <https://pypi.org>`__ is a public
487db96d56Sopenharmony_ci  repository of open source licensed packages made available for use by
497db96d56Sopenharmony_ci  other Python users.
507db96d56Sopenharmony_ci* the `Python Packaging Authority
517db96d56Sopenharmony_ci  <https://www.pypa.io/>`__ is the group of
527db96d56Sopenharmony_ci  developers and documentation authors responsible for the maintenance and
537db96d56Sopenharmony_ci  evolution of the standard packaging tools and the associated metadata and
547db96d56Sopenharmony_ci  file format standards. They maintain a variety of tools, documentation,
557db96d56Sopenharmony_ci  and issue trackers on both `GitHub <https://github.com/pypa>`__ and
567db96d56Sopenharmony_ci  `Bitbucket <https://bitbucket.org/pypa/>`__.
577db96d56Sopenharmony_ci* ``distutils`` is the original build and distribution system first added to
587db96d56Sopenharmony_ci  the Python standard library in 1998. While direct use of ``distutils`` is
597db96d56Sopenharmony_ci  being phased out, it still laid the foundation for the current packaging
607db96d56Sopenharmony_ci  and distribution infrastructure, and it not only remains part of the
617db96d56Sopenharmony_ci  standard library, but its name lives on in other ways (such as the name
627db96d56Sopenharmony_ci  of the mailing list used to coordinate Python packaging standards
637db96d56Sopenharmony_ci  development).
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ci.. versionchanged:: 3.5
667db96d56Sopenharmony_ci   The use of ``venv`` is now recommended for creating virtual environments.
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci.. seealso::
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci   `Python Packaging User Guide: Creating and using virtual environments
717db96d56Sopenharmony_ci   <https://packaging.python.org/installing/#creating-virtual-environments>`__
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci
747db96d56Sopenharmony_ciBasic usage
757db96d56Sopenharmony_ci===========
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ciThe standard packaging tools are all designed to be used from the command
787db96d56Sopenharmony_ciline.
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ciThe following command will install the latest version of a module and its
817db96d56Sopenharmony_cidependencies from the Python Package Index::
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ci    python -m pip install SomePackage
847db96d56Sopenharmony_ci
857db96d56Sopenharmony_ci.. note::
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ci   For POSIX users (including macOS and Linux users), the examples in
887db96d56Sopenharmony_ci   this guide assume the use of a :term:`virtual environment`.
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci   For Windows users, the examples in this guide assume that the option to
917db96d56Sopenharmony_ci   adjust the system PATH environment variable was selected when installing
927db96d56Sopenharmony_ci   Python.
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ciIt's also possible to specify an exact or minimum version directly on the
957db96d56Sopenharmony_cicommand line. When using comparator operators such as ``>``, ``<`` or some other
967db96d56Sopenharmony_cispecial character which get interpreted by shell, the package name and the
977db96d56Sopenharmony_civersion should be enclosed within double quotes::
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci    python -m pip install SomePackage==1.0.4    # specific version
1007db96d56Sopenharmony_ci    python -m pip install "SomePackage>=1.0.4"  # minimum version
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ciNormally, if a suitable module is already installed, attempting to install
1037db96d56Sopenharmony_ciit again will have no effect. Upgrading existing modules must be requested
1047db96d56Sopenharmony_ciexplicitly::
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci    python -m pip install --upgrade SomePackage
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ciMore information and resources regarding ``pip`` and its capabilities can be
1097db96d56Sopenharmony_cifound in the `Python Packaging User Guide <https://packaging.python.org>`__.
1107db96d56Sopenharmony_ci
1117db96d56Sopenharmony_ciCreation of virtual environments is done through the :mod:`venv` module.
1127db96d56Sopenharmony_ciInstalling packages into an active virtual environment uses the commands shown
1137db96d56Sopenharmony_ciabove.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ci.. seealso::
1167db96d56Sopenharmony_ci
1177db96d56Sopenharmony_ci    `Python Packaging User Guide: Installing Python Distribution Packages
1187db96d56Sopenharmony_ci    <https://packaging.python.org/installing/>`__
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ciHow do I ...?
1227db96d56Sopenharmony_ci=============
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ciThese are quick answers or links for some common tasks.
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci... install ``pip`` in versions of Python prior to Python 3.4?
1277db96d56Sopenharmony_ci--------------------------------------------------------------
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ciPython only started bundling ``pip`` with Python 3.4. For earlier versions,
1307db96d56Sopenharmony_ci``pip`` needs to be "bootstrapped" as described in the Python Packaging
1317db96d56Sopenharmony_ciUser Guide.
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci.. seealso::
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ci   `Python Packaging User Guide: Requirements for Installing Packages
1367db96d56Sopenharmony_ci   <https://packaging.python.org/installing/#requirements-for-installing-packages>`__
1377db96d56Sopenharmony_ci
1387db96d56Sopenharmony_ci
1397db96d56Sopenharmony_ci.. installing-per-user-installation:
1407db96d56Sopenharmony_ci
1417db96d56Sopenharmony_ci... install packages just for the current user?
1427db96d56Sopenharmony_ci-----------------------------------------------
1437db96d56Sopenharmony_ci
1447db96d56Sopenharmony_ciPassing the ``--user`` option to ``python -m pip install`` will install a
1457db96d56Sopenharmony_cipackage just for the current user, rather than for all users of the system.
1467db96d56Sopenharmony_ci
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci... install scientific Python packages?
1497db96d56Sopenharmony_ci---------------------------------------
1507db96d56Sopenharmony_ci
1517db96d56Sopenharmony_ciA number of scientific Python packages have complex binary dependencies, and
1527db96d56Sopenharmony_ciaren't currently easy to install using ``pip`` directly. At this point in
1537db96d56Sopenharmony_citime, it will often be easier for users to install these packages by
1547db96d56Sopenharmony_ci`other means <https://packaging.python.org/science/>`__
1557db96d56Sopenharmony_cirather than attempting to install them with ``pip``.
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ci.. seealso::
1587db96d56Sopenharmony_ci
1597db96d56Sopenharmony_ci   `Python Packaging User Guide: Installing Scientific Packages
1607db96d56Sopenharmony_ci   <https://packaging.python.org/science/>`__
1617db96d56Sopenharmony_ci
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ci... work with multiple versions of Python installed in parallel?
1647db96d56Sopenharmony_ci----------------------------------------------------------------
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ciOn Linux, macOS, and other POSIX systems, use the versioned Python commands
1677db96d56Sopenharmony_ciin combination with the ``-m`` switch to run the appropriate copy of
1687db96d56Sopenharmony_ci``pip``::
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci   python2   -m pip install SomePackage  # default Python 2
1717db96d56Sopenharmony_ci   python2.7 -m pip install SomePackage  # specifically Python 2.7
1727db96d56Sopenharmony_ci   python3   -m pip install SomePackage  # default Python 3
1737db96d56Sopenharmony_ci   python3.4 -m pip install SomePackage  # specifically Python 3.4
1747db96d56Sopenharmony_ci
1757db96d56Sopenharmony_ciAppropriately versioned ``pip`` commands may also be available.
1767db96d56Sopenharmony_ci
1777db96d56Sopenharmony_ciOn Windows, use the ``py`` Python launcher in combination with the ``-m``
1787db96d56Sopenharmony_ciswitch::
1797db96d56Sopenharmony_ci
1807db96d56Sopenharmony_ci   py -2   -m pip install SomePackage  # default Python 2
1817db96d56Sopenharmony_ci   py -2.7 -m pip install SomePackage  # specifically Python 2.7
1827db96d56Sopenharmony_ci   py -3   -m pip install SomePackage  # default Python 3
1837db96d56Sopenharmony_ci   py -3.4 -m pip install SomePackage  # specifically Python 3.4
1847db96d56Sopenharmony_ci
1857db96d56Sopenharmony_ci.. other questions:
1867db96d56Sopenharmony_ci
1877db96d56Sopenharmony_ci   Once the Development & Deployment part of PPUG is fleshed out, some of
1887db96d56Sopenharmony_ci   those sections should be linked from new questions here (most notably,
1897db96d56Sopenharmony_ci   we should have a question about avoiding depending on PyPI that links to
1907db96d56Sopenharmony_ci   https://packaging.python.org/en/latest/mirrors/)
1917db96d56Sopenharmony_ci
1927db96d56Sopenharmony_ci
1937db96d56Sopenharmony_ciCommon installation issues
1947db96d56Sopenharmony_ci==========================
1957db96d56Sopenharmony_ci
1967db96d56Sopenharmony_ciInstalling into the system Python on Linux
1977db96d56Sopenharmony_ci------------------------------------------
1987db96d56Sopenharmony_ci
1997db96d56Sopenharmony_ciOn Linux systems, a Python installation will typically be included as part
2007db96d56Sopenharmony_ciof the distribution. Installing into this Python installation requires
2017db96d56Sopenharmony_ciroot access to the system, and may interfere with the operation of the
2027db96d56Sopenharmony_cisystem package manager and other components of the system if a component
2037db96d56Sopenharmony_ciis unexpectedly upgraded using ``pip``.
2047db96d56Sopenharmony_ci
2057db96d56Sopenharmony_ciOn such systems, it is often better to use a virtual environment or a
2067db96d56Sopenharmony_ciper-user installation when installing packages with ``pip``.
2077db96d56Sopenharmony_ci
2087db96d56Sopenharmony_ci
2097db96d56Sopenharmony_ciPip not installed
2107db96d56Sopenharmony_ci-----------------
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ciIt is possible that ``pip`` does not get installed by default. One potential fix is::
2137db96d56Sopenharmony_ci
2147db96d56Sopenharmony_ci    python -m ensurepip --default-pip
2157db96d56Sopenharmony_ci
2167db96d56Sopenharmony_ciThere are also additional resources for `installing pip.
2177db96d56Sopenharmony_ci<https://packaging.python.org/en/latest/tutorials/installing-packages/#ensure-pip-setuptools-and-wheel-are-up-to-date>`__
2187db96d56Sopenharmony_ci
2197db96d56Sopenharmony_ci
2207db96d56Sopenharmony_ciInstalling binary extensions
2217db96d56Sopenharmony_ci----------------------------
2227db96d56Sopenharmony_ci
2237db96d56Sopenharmony_ciPython has typically relied heavily on source based distribution, with end
2247db96d56Sopenharmony_ciusers being expected to compile extension modules from source as part of
2257db96d56Sopenharmony_cithe installation process.
2267db96d56Sopenharmony_ci
2277db96d56Sopenharmony_ciWith the introduction of support for the binary ``wheel`` format, and the
2287db96d56Sopenharmony_ciability to publish wheels for at least Windows and macOS through the
2297db96d56Sopenharmony_ciPython Package Index, this problem is expected to diminish over time,
2307db96d56Sopenharmony_cias users are more regularly able to install pre-built extensions rather
2317db96d56Sopenharmony_cithan needing to build them themselves.
2327db96d56Sopenharmony_ci
2337db96d56Sopenharmony_ciSome of the solutions for installing `scientific software
2347db96d56Sopenharmony_ci<https://packaging.python.org/science/>`__
2357db96d56Sopenharmony_cithat are not yet available as pre-built ``wheel`` files may also help with
2367db96d56Sopenharmony_ciobtaining other binary extensions without needing to build them locally.
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci.. seealso::
2397db96d56Sopenharmony_ci
2407db96d56Sopenharmony_ci   `Python Packaging User Guide: Binary Extensions
2417db96d56Sopenharmony_ci   <https://packaging.python.org/extensions/>`__
242