Skip to content

Commit 3518185

Browse files
committed
Add discussion "Distribution package vs. import package"
Resolves #1425
1 parent 0c6459b commit 3518185

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

source/discussions/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ specific topic. If you're just trying to get stuff done, see
1212
pip-vs-easy-install
1313
install-requires-vs-requirements
1414
wheel-vs-egg
15+
distribution-package-vs-import-package
1516
src-layout-vs-flat-layout
1617
setup-py-deprecated

source/glossary.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Glossary
6767
:term:`Import Package` (which is also commonly called a "package") or
6868
another kind of distribution (e.g. a Linux distribution or the Python
6969
language distribution), which are often referred to with the single term
70-
"distribution".
70+
"distribution". See :ref:`distribution-package-vs-import-package`
71+
for a breakdown of the differences.
7172

7273
Egg
7374

@@ -103,7 +104,8 @@ Glossary
103104
An import package is more commonly referred to with the single word
104105
"package", but this guide will use the expanded term when more clarity
105106
is needed to prevent confusion with a :term:`Distribution Package` which
106-
is also commonly called a "package".
107+
is also commonly called a "package". See :ref:`distribution-package-vs-import-package`
108+
for a breakdown of the differences.
107109

108110
Module
109111

source/guides/packaging-namespace-packages.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _packaging-namespace-packages:
2+
13
============================
24
Packaging namespace packages
35
============================

0 commit comments

Comments
 (0)