Skip to content

Commit 227a942

Browse files
authored
Merge pull request #859 from matthewfeickert/fix/use-src-structure
2 parents 8dfdf5c + 11bdf83 commit 227a942

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

source/tutorials/packaging-projects.rst

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ To create this project locally, create the following file structure:
2121
.. code-block:: text
2222
2323
packaging_tutorial
24-
└── example_pkg
25-
   └── __init__.py
26-
24+
└── src
25+
└── example_pkg
26+
└── __init__.py
2727
2828
Once you create this structure, you'll want to run all of the commands in this
2929
tutorial within the top-level folder - so be sure to ``cd packaging_tutorial``.
3030

31-
:file:`example_pkg/__init__.py` is required to import the directory as a package,
31+
:file:`src/example_pkg/__init__.py` is required to import the directory as a package,
3232
and can simply be an empty file.
3333

3434
.. _Python documentation for packages and modules:
@@ -46,12 +46,13 @@ project's root directory - you will add content to them in the following steps.
4646
4747
packaging_tutorial
4848
├── LICENSE
49-
├── README.md
50-
├── example_pkg
51-
│   └── __init__.py
5249
├── pyproject.toml
50+
├── README.md
5351
├── setup.cfg
5452
├── setup.py # optional, needed to make editable pip installs work
53+
├── src
54+
│   └── example_pkg
55+
│   └── __init__.py
5556
└── tests
5657
5758
@@ -142,9 +143,13 @@ an escape hatch when absolutely necessary.
142143
Operating System :: OS Independent
143144
144145
[options]
146+
package_dir =
147+
= src
145148
packages = find:
146149
python_requires = >=3.6
147150
151+
[options.packages.find]
152+
where = src
148153
149154
There are a `variety of metadata and options
150155
<https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html>`_
@@ -183,11 +188,16 @@ an escape hatch when absolutely necessary.
183188

184189
In the options category, we have controls for setuptools itself:
185190

191+
- ``package_dir`` is a collection of package names and directories.
192+
An empty package name represents the "root package", so in this
193+
case the root package is in the ``src`` directory.
186194
- ``packages`` is a list of all Python :term:`import packages <Import
187195
Package>` that should be included in the :term:`Distribution Package`.
188196
Instead of listing each package manually, we can use the ``find:`` directive
189-
to automatically discover all packages and subpackages. In this case, the
190-
list of packages will be ``example_pkg`` as that's the only package present.
197+
to automatically discover all packages and subpackages and
198+
``options.packages.find`` to specify the ``package_dir`` to use. In this
199+
case, the list of packages will be ``example_pkg`` as that's the only
200+
package present.
191201
- ``python_requires`` gives the versions of Python supported by your
192202
project. Installers like pip will look back though older versions of
193203
packages until it finds one that has a matching Python version.
@@ -252,7 +262,8 @@ an escape hatch when absolutely necessary.
252262
"License :: OSI Approved :: MIT License",
253263
"Operating System :: OS Independent",
254264
],
255-
packages=setuptools.find_packages(),
265+
package_dir={"": "src"},
266+
packages=setuptools.find_packages(where="src"),
256267
python_requires=">=3.6",
257268
)
258269
@@ -288,11 +299,15 @@ an escape hatch when absolutely necessary.
288299
which license your package is available under, and which operating systems
289300
your package will work on. For a complete list of classifiers, see
290301
https://pypi.org/classifiers/.
302+
- ``package_dir`` is a dictionary with package names for keys and directories
303+
for values. An empty package name represents the "root package", so in this
304+
case the root package is in the ``src`` directory.
291305
- ``packages`` is a list of all Python :term:`import packages <Import
292306
Package>` that should be included in the :term:`Distribution Package`.
293307
Instead of listing each package manually, we can use :func:`find_packages`
294-
to automatically discover all packages and subpackages. In this case, the
295-
list of packages will be ``example_pkg`` as that's the only package present.
308+
to automatically discover all packages and subpackages under ``package_dir``.
309+
In this case, the list of packages will be ``example_pkg`` as that's the
310+
only package present.
296311
- ``python_requires`` gives the versions of Python supported by your
297312
project. Installers like pip will look back though older versions of
298313
packages until it finds one that has a matching Python version.

0 commit comments

Comments
 (0)