Creating and Deploying a Python Package to PyPI
Introduction
Developing a Python package and sharing it with the world can be a rewarding experience. In this tutorial, we will guide you through the process of creating a simple Python package and deploying it to the Python Package Index (PyPI) using GitHub Actions. This automated approach ensures that your package is consistently built and deployed whenever you create a new release on GitHub.
By the end of this article, you will learn how to:
- Create a simple Python package with the proper structure and metadata.
- Set up a GitHub repository for your package.
- Configure GitHub Actions to automatically build and deploy your package to PyPI upon creating a new release.
By following these steps, you will be able to easily create, manage, and share your Python packages with the community, ensuring a smooth development and deployment process. So, let’s dive in and start sharing your Python creations with the world!
To create a simple Python package, follow these steps:
Step 1: Create a new directory for your package
1.1: Create a new folder with the desired name for your package. In this example, we will use my_package
:
mkdir my_package
cd my_package
Step 2: Create the package structure
2.1: Create an __init__.py
file inside the package folder. This file is required for Python to treat the directory as a package:
touch __init__.py
2.2: Create a Python file (module) inside the package folder. In this example, we’ll create a file called hello.py
with a simple function:
# hello.py
def greet(name):
return f"Hello, {name}!"
Step 3: Create a setup.py
file
3.1: Create a setup.py
file in the root directory of your package. This file contains metadata about your package and is required for packaging and distribution:
# hello.py
from setuptools import setup, find_packages
setup(
name="my-package",
version="0.1",
packages=find_packages(),
install_requires=[],
author="Your Name",
author_email="your.email@example.com",
description="A simple example Python package",
url="https://github.com/yourusername/my_package",
)
Replace the placeholders with your actual information, such as your name, email, and GitHub URL.
Step 4: Package your code
4.1: Install the required packaging tools:
pip install setuptools wheel
4.2: Build your package using the setup.py
script:
python setup.py sdist bdist_wheel
This command will generate the source distribution and the wheel distribution of your package inside the dist
folder.
Step 5: Publish your package (Optional)
If you want to publish your package to PyPI, follow the steps in the previous answer to set up a GitHub Actions workflow for publishing your package whenever a new release is created. Alternatively, you can manually upload your package using twine
:
5.1: Install twine
:
pip install twine
5.2: Upload your package to PyPI:
twine upload dist/*
You will be prompted to enter your PyPI username and password. If you want to use an API token, use __token__
as the username and your actual API token as the password.
Now your Python package has been created and is ready for use. To install and use your package from PyPI, run:
pip install my-package
Replace my-package
with the actual name of your package.
Conclusion
In this tutorial, we have successfully walked through the process of creating a simple Python package and deploying it to PyPI using GitHub Actions. By leveraging the power of automation, you can now ensure a consistent and efficient development and deployment process for your Python packages. This setup not only helps you manage your codebase efficiently but also allows you to share your work with the community with ease.
As you continue to develop and improve your packages, remember that best practices in code organization, documentation, and testing are crucial for maintaining a high-quality project. By adhering to these principles and utilizing the automation provided by GitHub Actions, you can focus on delivering valuable and reliable Python packages that others will appreciate and benefit from. Happy coding!
About the Author
Author Name: Saurabh Pandey
Saurabh Pandey is a passionate Software Engineer from India. With years of experience in the industry, Saurabh has honed his skills in various programming languages and technologies, allowing him to effectively lead and contribute to complex projects.
To connect with Saurabh and learn more about his work, you can visit his LinkedIn profile at https://www.linkedin.com/in/dextrop/.
If you’re interested in diving deeper into the topics covered in this article, here are some advanced resources that you can explore:
- Python Packaging User Guide: A comprehensive guide to packaging Python projects that covers various tools, packaging formats, and best practices. This is an invaluable resource for anyone looking to deepen their understanding of Python packaging. https://packaging.python.org/
- GitHub Actions Documentation: The official GitHub Actions documentation provides detailed information on how to create custom workflows, configure jobs, and integrate with various third-party services. This resource will help you master GitHub Actions for automating your development process. https://docs.github.com/en/actions
- Setuptools Documentation: The official documentation for setuptools, a library that provides tools for building and packaging Python projects. This resource covers advanced topics and customization options for setup.py files. https://setuptools.pypa.io/
- PyPI and TestPyPI: Learn about PyPI (Python Package Index) and TestPyPI, which are package repositories for distributing Python projects. TestPyPI is a separate instance of the package index that allows you to try the distribution tools and process without affecting the real index. PyPI: https://pypi.org/, TestPyPI: https://test.pypi.org/
- Deploying Python Applications with Docker: This tutorial explains how to containerize Python applications using Docker, which can help you manage dependencies and streamline deployment across various environments. https://runnable.com/docker/python/dockerize-your-python-application
By exploring these advanced resources, you’ll be well-equipped to enhance your Python packaging and deployment skills, as well as further automate your development processes using tools like GitHub Actions. Happy learning!