|
| 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 PyPI_, the most widely known centralized source for |
| 20 | +installing Python software, 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, "distribution package" refers to a package |
| 25 | +provided by the system package manager, which is a different meaning. |
| 26 | + |
| 27 | + |
| 28 | +What's an import package? |
| 29 | +========================= |
| 30 | + |
| 31 | +An import package is a Python module. Thus, when you write ``import |
| 32 | +pkg`` or ``from pkg import func`` in your Python code, ``pkg`` is the |
| 33 | +name of an import package. More precisely, import packages are special |
| 34 | +Python modules that can contain submodules. For example, the ``numpy`` |
| 35 | +package contains modules like ``numpy.linalg`` and |
| 36 | +``numpy.fft``. Usually, an import package is a directory on the file |
| 37 | +system, containing modules as ``.py`` files and subpackages as |
| 38 | +subdirectories. |
| 39 | + |
| 40 | +You can use an import package as soon as you have installed a distribution |
| 41 | +package that provides it. |
| 42 | + |
| 43 | + |
| 44 | +What are the links between distribution packages and import packages? |
| 45 | +===================================================================== |
| 46 | + |
| 47 | +By convention, a distribution package usually provides one single import |
| 48 | +package (or non-package module), with a matching name. For example, |
| 49 | +``pip install numpy`` lets you ``import numpy``. |
| 50 | + |
| 51 | +However, this is only a convention. PyPI and other package indices do |
| 52 | +not enforce any relationship between the name of a distribution package |
| 53 | +and the import packages it provides. |
| 54 | + |
| 55 | +A distribution package could provide an import package with a different |
| 56 | +name. An example of this is the popular Pillow_ library for image |
| 57 | +processing. Its distribution package name is ``Pillow``, but it provides |
| 58 | +the import package ``PIL``. This is for historical reasons: Pillow |
| 59 | +started as a fork of the PIL library, thus it kept the import name |
| 60 | +``PIL`` so that existing PIL users could switch to Pillow with little |
| 61 | +effort. More generally, a fork of an existing library is a common reason |
| 62 | +for differing names between the distribution package and the import |
| 63 | +package. |
| 64 | + |
| 65 | +On a given package index (like PyPI), distribution package names must be |
| 66 | +unique. On the other hand, import packages have no such requirement. |
| 67 | +Import packages with the same name can be provided by several |
| 68 | +distribution packages. Again, forks are a common reason for this. |
| 69 | + |
| 70 | +Conversely, a distribution package can provide several import packages, |
| 71 | +although this is less common. |
| 72 | + |
| 73 | + |
| 74 | +How do distribution package names and import package names compare? |
| 75 | +=================================================================== |
| 76 | + |
| 77 | +Import packages should have valid Python identifiers as their name. In |
| 78 | +particular, they use underscores ``_`` as word separator and they are |
| 79 | +case-sensitive. |
| 80 | + |
| 81 | +On the other hand, distribution packages can use hyphens ``-`` or |
| 82 | +underscores ``.``. They can also contain dots ``.``, which is sometimes |
| 83 | +used for packaging a subpackage of a :ref:`namespace package |
| 84 | +<packaging-namespace-packages>`. For most purposes, they are insensitive |
| 85 | +to case and to ``-`` vs. ``_`` differences, e.g., ``pip install |
| 86 | +Awesome_Package`` is the same as ``pip install awesome-package``. |
| 87 | + |
| 88 | + |
| 89 | +.. _PyPI: https://pypi.org |
| 90 | +.. _Pillow: https://pypi.org/project/Pillow |
0 commit comments