GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing packaging.python.org — Packaging Python Projects - Python Packaging User Guide Packaging Python Projects¶This tutorial walks you through how to package a simple Python project. It will show you how to add the necessary files and structure to create the package, how to build the package, and how to upload it to the Python Package Index (PyPI). Tip If you have trouble running the commands in this tutorial, please copy the command and its output, then open an issue on the packaging-problems repository on GitHub. We’ll do our best to help you! Some of the commands require a newer version of pip, so start by making sure you have the latest version installed:
Unix/macOS
python3 -m pip install --upgrade pip py -m pip install --upgrade pip A simple project¶This tutorial uses a simple project named
Create the following file structure locally: packaging_tutorial/
└── src/
└── example_package_YOUR_USERNAME_HERE/
├── __init__.py
└── example.py
The directory containing the Python files should match the project name. This simplifies the configuration and is more obvious to users who install the package. Creating the file
def add_one(number): return number + 1 If you are unfamiliar with Python’s modules and import packages, take a few minutes to read over the Python documentation for packages and modules. Once you create this structure, you’ll want to run all of the commands in this
tutorial within the Creating the package files¶You will now add files that are used to prepare the project for distribution. When you’re done, the project structure will look like this: packaging_tutorial/ ├── LICENSE ├── pyproject.toml ├── README.md ├── src/ │ └── example_package_YOUR_USERNAME_HERE/ │ ├── __init__.py │ └── example.py └── tests/ Creating a test directory¶
Choosing a build backend¶Tools like pip and build do not actually convert your sources into a distribution package (like a wheel); that job is performed by a build backend. The build backend determines how your project will specify its configuration, including metadata (information about the project, for example, the name and tags that are displayed on PyPI) and input files. Build backends have different levels of functionality, such as whether they support building extension modules, and you should choose one that suits your needs and preferences. You can choose from a number of backends; this tutorial uses Hatchling by default, but it will work identically with Setuptools,
Flit, PDM, and others that support the Note Some build backends are part of larger tools that provide a command-line interface with additional features like project initialization and version management, as well as building, uploading, and installing packages. This tutorial uses single-purpose tools that work independently. The
Hatchling
[build-system] requires = ["hatchling >= 1.26"] build-backend = "hatchling.build" [build-system] requires = ["setuptools >= 77.0.3"] build-backend = "setuptools.build_meta" [build-system] requires = ["flit_core >= 3.12.0, <5"] build-backend = "flit_core.buildapi" [build-system] requires = ["pdm-backend >= 2.4.0"] build-backend = "pdm.backend" [build-system] requires = ["uv_build >= 0.13.6, <0.14.0"] build-backend = "uv_build" The The Both of these values will be provided by the documentation for your build backend, or generated by its command line interface. There should be no need for you to customize these settings. Additional configuration of the build tool will either be in a Configuring metadata¶Open [project] name = "example_package_YOUR_USERNAME_HERE" version = "0.0.1" authors = [ { name="Example Author", email="[email protected]" }, ] description = "A small example package" readme = "README.md" requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", "Operating System :: OS Independent", ] license = "MIT" license-files = ["LICEN[CS]E*"] [project.urls] Homepage = "https://github.com/pypa/sampleproject" Issues = "https://github.com/pypa/sampleproject/issues"
See the pyproject.toml guide for details
on these and other fields that can be defined in the Creating README.md¶Open # Example Package This is a simple example package. You can use [GitHub-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content. Creating a LICENSE¶It’s important for every Distribution Archive uploaded to the Python
Package Index to include a license. This tells users who install your
Distribution Archive the terms under which they can use it. For help
picking a license, see https://choosealicense.com/. Once you have chosen a
license, open Copyright (c) 2018 The Python Packaging Authority Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Most build backends automatically include license files in packages. See your
backend’s documentation for more details.
If you include the path to license in the Including other files¶The files listed above will be included automatically in your source distribution. If you want to include additional files, see the documentation for your build backend. Generating distribution archives¶The next step is to generate distribution packages for the package. These are archives that are uploaded to the Python Package Index and can be installed by pip. Make sure you have the latest version of PyPA’s build installed:
Unix/macOS
python3 -m pip install --upgrade build py -m pip install --upgrade build Tip If you have trouble installing these, see the Installing Packages tutorial. Now run this command from the same directory where
Unix/macOS
python3 -m build py -m build This command should output a lot of text and once completed should generate two
files in the dist/ ├── example_package_YOUR_USERNAME_HERE-0.0.1-py3-none-any.whl └── example_package_YOUR_USERNAME_HERE-0.0.1.tar.gz The Uploading the distribution archives¶Finally, it’s time to upload your package to the Python Package Index! The first thing you’ll need to do is register an account on TestPyPI, which is a separate instance of the package index intended for testing and experimentation. It’s great for things like this tutorial where we don’t necessarily want to upload to the real index. To register an account, go to https://test.pypi.org/account/register/ and complete the steps on that page. You will also need to verify your email address before you’re able to upload any packages. For more details, see Using TestPyPI. To securely upload your project, you’ll need a PyPI API token. Create one at https://test.pypi.org/manage/account/#api-tokens, setting the “Scope” to “Entire account”. Don’t close the page until you have copied and saved the token — you won’t see that token again. Now that you are registered, you can use twine to upload the distribution packages. You’ll need to install Twine:
Unix/macOS
python3 -m pip install --upgrade twine py -m pip install --upgrade twine Once installed, run Twine to upload all of the archives under
Unix/macOS
python3 -m twine upload --repository testpypi dist/* py -m twine upload --repository testpypi dist/* You will be prompted for an API token. Use the token value, including the After the command completes, you should see output similar to this: Uploading distributions to https://test.pypi.org/legacy/ Enter your API token: Uploading example_package_YOUR_USERNAME_HERE-0.0.1-py3-none-any.whl 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.2/8.2 kB • 00:01 • ? Uploading example_package_YOUR_USERNAME_HERE-0.0.1.tar.gz 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.8/6.8 kB • 00:00 • ? Tip If Twine reports an invalid username or password, see PyPI’s help on invalid authentication errors. Once uploaded, your package should be viewable on TestPyPI; for example:
Installing your newly uploaded package¶You can use pip to install your package and verify that it works. Create a virtual environment and install your package from TestPyPI:
Unix/macOS
python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-package-YOUR-USERNAME-HERE py -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-package-YOUR-USERNAME-HERE Make sure to specify your username in the package name! pip should install the package from TestPyPI and the output should look something like this: Collecting example-package-YOUR-USERNAME-HERE Downloading https://test-files.pythonhosted.org/packages/.../example_package_YOUR_USERNAME_HERE_0.0.1-py3-none-any.whl Installing collected packages: example_package_YOUR_USERNAME_HERE Successfully installed example_package_YOUR_USERNAME_HERE-0.0.1 Note This example uses Using your newly installed package¶You can test that it was installed correctly by importing the package. Make sure you’re still in your virtual environment, then run Python:
Unix/macOS
python3 py and import the package: >>> from example_package_YOUR_USERNAME_HERE import example >>> example.add_one(2) 3 Next steps¶Congratulations, you’ve packaged and distributed a Python project! ✨ 🍰 ✨ Keep in mind that this tutorial showed you how to upload your package to Test PyPI, which isn’t a permanent storage. The Test system occasionally deletes packages and accounts. It is best to use TestPyPI for testing and experiments like this tutorial. Tip For projects published through a supported CI/CD platform, we recommend Trusted Publishing instead of storing a long-lived API token. The linked guide shows how to set this up with GitHub Actions. When you are ready to upload a real package to the Python Package Index you can do much the same as you did in this tutorial, but with these important differences:
At this point if you want to read more on packaging Python libraries here are some things you can do:
Notes |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |