Publishing your Python package to PyPI has never been easier! With Cookiecutter PyPackage, you can generate a fully structured Python package with automated testing, documentation, and CI/CD integration in just a few steps.
In this guide, we’ll cover:
✅ Setting up Cookiecutter PyPackage
✅ Generating a Python package structure
✅ Configuring testing, documentation, and PyPI deployment
✅ Releasing your package 🚀
🔹 Step 1: Install Cookiecutter
If you haven’t installed Cookiecutter, do so with:
pip install -U cookiecutter
🔹 Step 2: Generate Your Python Package
Run the following command to generate a new Python package using Cookiecutter PyPackage:
cookiecutter https://github.com/audreyfeldroy/cookiecutter-pypackage.git
You’ll be prompted to enter details for your package, such as:
-
Project name (e.g.,
my_pypi_package
) - Author name
- GitHub username
After completing the prompts, Cookiecutter will create a fully structured Python package.
🔹 Step 3: Set Up Version Control
Initialize a Git repository and push the code to GitHub:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/yourusername/my_pypi_package.git
git push -u origin main
🔹 Step 4: Set Up Your Development Environment
To install development dependencies, run:
pip install -r requirements_dev.txt
🔹 Step 5: Test Your Package
Run the tests using pytest
or unittest
:
pytest
Or if using unittest
:
python -m unittest discover
🔹 Step 6: Register Your Package on PyPI
- Create an account on PyPI.
- Generate an API token in PyPI under Account Settings → API Tokens.
- Save your API token securely.
🔹 Step 7: Configure PyPI Deployment with Travis CI
If you want automatic deployment, run this command to encrypt your PyPI password in Travis CI:
travis encrypt --add deploy.password
Then, add Travis CI integration to your GitHub repo.
🔹 Step 8: Publish Your Package to PyPI
Manual Upload:
First, install Twine:
pip install twine
Then, build and upload your package:
python setup.py sdist bdist_wheel
twine upload dist/*
Enter your PyPI username and password when prompted.
Automatic Upload (via GitHub Actions or Travis)
Push a new tag to the main
branch, and the package will be published automatically:
git tag v0.1.0
git push origin v0.1.0
🔹 Step 9: Install Your Package
Once uploaded to PyPI, your package can be installed with:
pip install my_pypi_package
🎯 Additional Features in Cookiecutter PyPackage
- ✅ Pre-configured Testing with
unittest
orpytest
- ✅ Travis CI/CD Support for automated testing & deployment
- ✅ Tox Integration for multi-version compatibility testing
- ✅ Sphinx Documentation for generating API docs
- ✅ bump2version for automatic versioning
- ✅ Command-Line Interface (CLI) support via Click (optional)
🔹 Conclusion
With Cookiecutter PyPackage, you can set up a production-ready Python package in minutes! 🚀
🔗 Helpful Links:
- GitHub Repo: Cookiecutter PyPackage
- Documentation: Read the Docs
- PyPI Guide: Packaging Python Projects
💬 What’s Next?
- Want GitHub Actions for automatic deployment?
- Need pre-commit hooks for linting with
ruff
? - Looking for Poetry-based packaging instead of
setup.py
?
Drop a comment below!
Top comments (0)