Skip to content

Commit c3d4b22

Browse files
authored
Merge pull request #817 from henryiii/chore/modern
chore: use modern package setup for example
2 parents b6bf584 + 88fcb23 commit c3d4b22

1 file changed

Lines changed: 63 additions & 16 deletions

File tree

source/tutorials/packaging-projects.rst

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ A simple project
1111

1212
This tutorial uses a simple project named ``example_pkg``. If you are unfamiliar
1313
with Python's modules and :term:`import packages <Import Package>`, take a few
14-
minutes to read over the `Python documentation for packages and modules`_. Even if you already have a project that you want to package up, we recommend following this tutorial as-is using this example package and then trying with your own package.
14+
minutes to read over the `Python documentation for packages and modules`_. Even
15+
if you already have a project that you want to package up, we recommend
16+
following this tutorial as-is using this example package and then trying with
17+
your own package.
1518

1619
To create this project locally, create the following file structure:
1720

@@ -36,8 +39,8 @@ Creating the package files
3639
--------------------------
3740

3841
You will now create a handful of files to package up this project and prepare it
39-
for distribution. Create the new files listed below and place them in the project's root directory
40-
- you will add content to them in the following steps.
42+
for distribution. Create the new files listed below and place them in the
43+
project's root directory - you will add content to them in the following steps.
4144

4245
.. code-block:: text
4346
@@ -46,6 +49,7 @@ for distribution. Create the new files listed below and place them in the projec
4649
├── README.md
4750
├── example_pkg
4851
│   └── __init__.py
52+
├── pyproject.toml
4953
├── setup.py
5054
└── tests
5155
@@ -56,14 +60,50 @@ Creating a test folder
5660
:file:`tests/` is a placeholder for unit test files. Leave it empty for now.
5761

5862

63+
Creating pyproject.toml
64+
-----------------------
65+
66+
:file:`pyproject.toml` is the file that tells build tools (like ``pip`` 10+ and
67+
``build``) what system you are using and what it required for building. The
68+
default if this file is missing is to assume a classic setuptools build system,
69+
but it is better to be explicit; if you have a :file:`pyproject.toml` file, you
70+
will be able to rely on ``wheel`` and other packages being present.
71+
72+
This file should be ideal for most setuptools projects:
73+
74+
75+
.. code-block:: toml
76+
77+
[build-system]
78+
requires = [
79+
"setuptools>=42",
80+
"wheel"
81+
]
82+
build-backend = "setuptools.build_meta"
83+
84+
85+
``build-system.requires`` gives a list of packages that are needed to build your
86+
package. Listing something here will *only* make it available during the build,
87+
not after it is installed.
88+
89+
``build-system.build-backend`` is technically optional, but you will get
90+
``setuptools.build_meta:__legacy__`` instead if you forget to include it, so
91+
always include it. If you were to use a different build system, such as
92+
:ref:`flit` or `poetry`_, those would go here, and the configuration details
93+
would be completely different than the setuptools configuration described
94+
below. See :pep:`517` and :pep:`518` for background and details.
95+
5996
Creating setup.py
6097
-----------------
6198

6299
:file:`setup.py` is the build script for :ref:`setuptools`. It tells setuptools
63100
about your package (such as the name and version) as well as which code files
64101
to include.
65102

66-
Open :file:`setup.py` and enter the following content. Update the package name to include your username (for example, ``example-pkg-theacodes``), this ensures that you have a unique package name and that your package doesn't conflict with packages uploaded by other people following this tutorial.
103+
Open :file:`setup.py` and enter the following content. Update the package name
104+
to include your username (for example, ``example-pkg-theacodes``), this ensures
105+
that you have a unique package name and that your package doesn't conflict with
106+
packages uploaded by other people following this tutorial.
67107

68108
.. code-block:: python
69109
@@ -94,9 +134,11 @@ Open :file:`setup.py` and enter the following content. Update the package name t
94134
:func:`setup` takes several arguments. This example package uses a relatively
95135
minimal set:
96136

97-
- ``name`` is the *distribution name* of your package. This can be any name as long as only
98-
contains letters, numbers, ``_`` , and ``-``. It also must not already be
99-
taken on pypi.org. **Be sure to update this with your username,** as this ensures you won't try to upload a package with the same name as one which already exists when you upload the package.
137+
- ``name`` is the *distribution name* of your package. This can be any name as
138+
long as only contains letters, numbers, ``_`` , and ``-``. It also must not
139+
already be taken on pypi.org. **Be sure to update this with your username,**
140+
as this ensures you won't try to upload a package with the same name as one
141+
which already exists when you upload the package.
100142
- ``version`` is the package version see :pep:`440` for more details on
101143
versions.
102144
- ``author`` and ``author_email`` are used to identify the author of the
@@ -185,21 +227,20 @@ The next step is to generate :term:`distribution packages <Distribution
185227
Package>` for the package. These are archives that are uploaded to the Package
186228
Index and can be installed by :ref:`pip`.
187229

188-
Make sure you have the latest versions of ``setuptools`` and ``wheel``
189-
installed:
230+
Make sure you have the latest versions of PyPA's ``build`` installed:
190231

191232
.. code-block:: bash
192233
193-
python3 -m pip install --user --upgrade setuptools wheel
234+
python3 -m pip install --upgrade build
194235
195-
.. tip:: IF you have trouble installing these, see the
236+
.. tip:: If you have trouble installing these, see the
196237
:doc:`installing-packages` tutorial.
197238

198-
Now run this command from the same directory where :file:`setup.py` is located:
239+
Now run this command from the same directory where :file:`pyproject.toml` is located:
199240

200241
.. code-block:: bash
201242
202-
python3 setup.py sdist bdist_wheel
243+
python3 -m build
203244
204245
This command should output a lot of text and once completed should generate two
205246
files in the :file:`dist` directory:
@@ -307,7 +348,12 @@ something like this:
307348
Installing collected packages: example-pkg-YOUR-USERNAME-HERE
308349
Successfully installed example-pkg-YOUR-USERNAME-HERE-0.0.1
309350
310-
.. note:: This example uses ``--index-url`` flag to specify TestPyPI instead of live PyPI. Additionally, it specifies ``--no-deps``. Since TestPyPI doesn't have the same packages as the live PyPI, it's possible that attempting to install dependencies may fail or install something unexpected. While our example package doesn't have any dependencies, it's a good practice to avoid installing dependencies when using TestPyPI.
351+
.. note:: This example uses ``--index-url`` flag to specify TestPyPI instead of
352+
live PyPI. Additionally, it specifies ``--no-deps``. Since TestPyPI doesn't
353+
have the same packages as the live PyPI, it's possible that attempting to
354+
install dependencies may fail or install something unexpected. While our
355+
example package doesn't have any dependencies, it's a good practice to avoid
356+
installing dependencies when using TestPyPI.
311357

312358
You can test that it was installed correctly by importing the package.
313359
Run the Python interpreter (make sure you're still in your virtualenv):
@@ -334,7 +380,8 @@ Next steps
334380

335381
Keep in mind that this tutorial showed you how to upload your package to Test
336382
PyPI, which isn't a permanent storage. The Test system occasionally deletes
337-
packages and accounts. It is best to use Test PyPI for testing and experiments like this tutorial.
383+
packages and accounts. It is best to use Test PyPI for testing and experiments
384+
like this tutorial.
338385

339386
When you are ready to upload a real package to the Python Package Index you can
340387
do much the same as you did in this tutorial, but with these important
@@ -361,4 +408,4 @@ some things you can do:
361408
and `poetry`_.
362409

363410
.. _hatch: https://github.com/ofek/hatch
364-
.. _poetry: https://github.com/python-poetry/poetry
411+
.. _poetry: https://python-poetry.org

0 commit comments

Comments
 (0)