Building and Distributing Your Package
Now that we have created a Python package with additional files and confirmed that we can access them, let's learn how to build and distribute this package.
Updating the Setup Script
Before building the package, let's update our setup.py
file to include more metadata and requirements:
cd ~/project
cat > setup.py << 'EOF'
from setuptools import setup, find_packages
setup(
name="mypackage",
version="0.1.0",
packages=find_packages(),
## Include data files
package_data={
"mypackage": ["config.ini", "data/*.txt"],
},
## Dependencies
install_requires=[
"setuptools",
],
## Metadata
author="Your Name",
author_email="[email protected]",
description="A simple Python package with additional files",
keywords="sample, package, data",
url="https://example.com/mypackage",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
python_requires=">=3.6",
)
EOF
Building Source and Wheel Distributions
Python packages can be distributed in several formats, but the most common are:
- Source Distribution (sdist): A tarball containing the source code and additional files
- Wheel Distribution (bdist_wheel): A pre-built package that can be installed without building
Let's create both types of distributions:
## Make sure we have the latest build tools
pip install --upgrade setuptools wheel
## Build the distributions
python setup.py sdist bdist_wheel
You should see output indicating that the distributions were created, and new files should appear in the dist
directory.
Let's check the contents of the dist
directory:
ls -l dist
You should see at least two files:
- A
.tar.gz
file (the source distribution)
- A
.whl
file (the wheel distribution)
Installing the Package from the Distribution Files
Now, let's test installing the package from one of the distribution files. First, let's uninstall our development version:
pip uninstall -y mypackage
Now, let's install the wheel distribution:
pip install dist/mypackage-0.1.0-py3-none-any.whl
You should see output indicating that the package was successfully installed.
Let's verify that the package is installed and that we can still access the additional files:
python -c "import mypackage; print(mypackage.read_config())"
This should output the content of the config.ini file:
[config]
debug = true
log_level = INFO
Publishing Your Package
In a real-world scenario, you would typically publish your package to the Python Package Index (PyPI) so that others can install it using pip install mypackage
. This would involve:
- Creating an account on PyPI (https://pypi.org/)
- Using tools like
twine
to upload your distributions:pip install twine
twine upload dist/*
However, for this lab, we will stop at creating the distributions locally. You now have a complete Python package with additional files that can be distributed and installed by others.
Summary of What You've Created
- A Python package with modules and additional files
- A setup script that includes these files in the distribution
- Functions to access these files from your code
- Source and wheel distribution files ready for distribution
This structure provides a solid foundation for any Python package you might want to create in the future.