|
| 1 | +.. _distribution-package-vs-import-package: |
| 2 | + |
| 3 | +======================================= |
| 4 | +Distribution package vs. import package |
| 5 | +======================================= |
| 6 | + |
| 7 | +A number of different concepts are commonly referred to by the word |
| 8 | +"package". This page clarifies the differences between two distinct but |
| 9 | +related meanings in Python packaging, "distribution package" and "import |
| 10 | +package". |
| 11 | + |
| 12 | +What's a distribution package? |
| 13 | +============================== |
| 14 | + |
| 15 | +A distribution package is a piece of software that you can install. |
| 16 | +Most of the time, this is synonymous with "project". When you type ``pip |
| 17 | +install pkg``, or when you write ``dependencies = ["pkg"]`` in your |
| 18 | +``pyproject.toml``, ``pkg`` is the name of a distribution package. When |
| 19 | +you search or browse the PyPI_, the most widely known centralized source for |
| 20 | +installing Python libraries and tools, what you see is a list of distribution |
| 21 | +packages. Alternatively, the term "distribution package" can be used to |
| 22 | +refer to a specific file that contains a certain version of a project. |
| 23 | + |
| 24 | +Note that in the Linux world, a "distribution package", |
| 25 | +most commonly abbreviated as "distro package" or just "package", |
| 26 | +is something provided by the system package manager of the `Linux distribution <distro_>`_, |
| 27 | +which is a different meaning. |
| 28 | + |
| 29 | + |
| 30 | +What's an import package? |
| 31 | +========================= |
| 32 | + |
| 33 | +An import package is a Python module. Thus, when you write ``import |
| 34 | +pkg`` or ``from pkg import func`` in your Python code, ``pkg`` is the |
| 35 | +name of an import package. More precisely, import packages are special |
| 36 | +Python modules that can contain submodules. For example, the ``numpy`` |
| 37 | +package contains modules like ``numpy.linalg`` and |
| 38 | +``numpy.fft``. Usually, an import package is a directory on the file |
| 39 | +system, containing modules as ``.py`` files and subpackages as |
| 40 | +subdirectories. |
| 41 | + |
| 42 | +You can use an import package as soon as you have installed a distribution |
| 43 | +package that provides it. |
| 44 | + |
| 45 | + |
| 46 | +What are the links between distribution packages and import packages? |
| 47 | +===================================================================== |
| 48 | + |
| 49 | +Most of the time, a distribution package provides one single import |
| 50 | +package (or non-package module), with a matching name. For example, |
| 51 | +``pip install numpy`` lets you ``import numpy``. |
| 52 | + |
| 53 | +However, this is only a convention. PyPI and other package indices *do not |
| 54 | +enforce any relationship* between the name of a distribution package and the |
| 55 | +import packages it provides. (A consequence of this is that you cannot blindly |
| 56 | +install the PyPI package ``foo`` if you see ``import foo``; this may install an |
| 57 | +unintended, and potentially even malicious package.) |
| 58 | + |
| 59 | +A distribution package could provide an import package with a different |
| 60 | +name. An example of this is the popular Pillow_ library for image |
| 61 | +processing. Its distribution package name is ``Pillow``, but it provides |
| 62 | +the import package ``PIL``. This is for historical reasons: Pillow |
| 63 | +started as a fork of the PIL library, thus it kept the import name |
| 64 | +``PIL`` so that existing PIL users could switch to Pillow with little |
| 65 | +effort. More generally, a fork of an existing library is a common reason |
| 66 | +for differing names between the distribution package and the import |
| 67 | +package. |
| 68 | + |
| 69 | +On a given package index (like PyPI), distribution package names must be |
| 70 | +unique. On the other hand, import packages have no such requirement. |
| 71 | +Import packages with the same name can be provided by several |
| 72 | +distribution packages. Again, forks are a common reason for this. |
| 73 | + |
| 74 | +Conversely, a distribution package can provide several import packages, |
| 75 | +although this is less common. An example is the attrs_ distribution |
| 76 | +package, which provides both an ``attrs`` import package with a newer |
| 77 | +API, and an ``attr`` import package with an older but supported API. |
| 78 | + |
| 79 | + |
| 80 | +How do distribution package names and import package names compare? |
| 81 | +=================================================================== |
| 82 | + |
| 83 | +Import packages should have valid Python identifiers as their name (the |
| 84 | +:ref:`exact rules <python:identifiers>` are found in the Python |
| 85 | +documentation) [#non-identifier-mod-name]_. In particular, they use underscores ``_`` as word |
| 86 | +separator and they are case-sensitive. |
| 87 | + |
| 88 | +On the other hand, distribution packages can use hyphens ``-`` or |
| 89 | +underscores ``_``. They can also contain dots ``.``, which is sometimes |
| 90 | +used for packaging a subpackage of a :ref:`namespace package |
| 91 | +<packaging-namespace-packages>`. For most purposes, they are insensitive |
| 92 | +to case and to ``-`` vs. ``_`` differences, e.g., ``pip install |
| 93 | +Awesome_Package`` is the same as ``pip install awesome-package`` (the |
| 94 | +precise rules are given in the :ref:`name normalization specification |
| 95 | +<name-normalization>`). |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +--------------------------- |
| 100 | + |
| 101 | +.. [#non-identifier-mod-name] Although it is technically possible |
| 102 | + to import packages/modules that do not have a valid Python identifier as |
| 103 | + their name, using :doc:`importlib <python:library/importlib>`, |
| 104 | + this is vanishingly rare and strongly discouraged. |
| 105 | +
|
| 106 | +
|
| 107 | +.. _distro: https://en.wikipedia.org/wiki/Linux_distribution |
| 108 | +.. _PyPI: https://pypi.org |
| 109 | +.. _Pillow: https://pypi.org/project/Pillow |
| 110 | +.. _attrs: https://pypi.org/project/attrs |
0 commit comments