You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/tutorials/packaging-projects.rst
+63-16Lines changed: 63 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,10 @@ A simple project
11
11
12
12
This tutorial uses a simple project named ``example_pkg``. If you are unfamiliar
13
13
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.
15
18
16
19
To create this project locally, create the following file structure:
17
20
@@ -36,8 +39,8 @@ Creating the package files
36
39
--------------------------
37
40
38
41
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.
41
44
42
45
.. code-block:: text
43
46
@@ -46,6 +49,7 @@ for distribution. Create the new files listed below and place them in the projec
46
49
├── README.md
47
50
├── example_pkg
48
51
│ └── __init__.py
52
+
├── pyproject.toml
49
53
├── setup.py
50
54
└── tests
51
55
@@ -56,14 +60,50 @@ Creating a test folder
56
60
:file:`tests/` is a placeholder for unit test files. Leave it empty for now.
57
61
58
62
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
+
59
96
Creating setup.py
60
97
-----------------
61
98
62
99
:file:`setup.py` is the build script for :ref:`setuptools`. It tells setuptools
63
100
about your package (such as the name and version) as well as which code files
64
101
to include.
65
102
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.
67
107
68
108
.. code-block:: python
69
109
@@ -94,9 +134,11 @@ Open :file:`setup.py` and enter the following content. Update the package name t
94
134
:func:`setup` takes several arguments. This example package uses a relatively
95
135
minimal set:
96
136
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.
100
142
- ``version`` is the package version see :pep:`440` for more details on
101
143
versions.
102
144
- ``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
185
227
Package>` for the package. These are archives that are uploaded to the Package
186
228
Index and can be installed by :ref:`pip`.
187
229
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:
.. 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.
311
357
312
358
You can test that it was installed correctly by importing the package.
313
359
Run the Python interpreter (make sure you're still in your virtualenv):
@@ -334,7 +380,8 @@ Next steps
334
380
335
381
Keep in mind that this tutorial showed you how to upload your package to Test
336
382
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.
338
385
339
386
When you are ready to upload a real package to the Python Package Index you can
340
387
do much the same as you did in this tutorial, but with these important
0 commit comments