DEV Community

TechWithTy
TechWithTy

Posted on

1

How to Create a PyPI Package Using Cookiecutter PyPackage

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
Enter fullscreen mode Exit fullscreen mode

🔹 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
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted to enter details for your package, such as:

  • Project name (e.g., my_pypi_package)
  • Author name
  • Email
  • 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
Enter fullscreen mode Exit fullscreen mode

🔹 Step 4: Set Up Your Development Environment

To install development dependencies, run:

pip install -r requirements_dev.txt
Enter fullscreen mode Exit fullscreen mode

🔹 Step 5: Test Your Package

Run the tests using pytest or unittest:

pytest
Enter fullscreen mode Exit fullscreen mode

Or if using unittest:

python -m unittest discover
Enter fullscreen mode Exit fullscreen mode

🔹 Step 6: Register Your Package on PyPI

  1. Create an account on PyPI.
  2. Generate an API token in PyPI under Account Settings → API Tokens.
  3. 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
Enter fullscreen mode Exit fullscreen mode

Then, add Travis CI integration to your GitHub repo.


🔹 Step 8: Publish Your Package to PyPI

Manual Upload:

First, install Twine:

pip install twine
Enter fullscreen mode Exit fullscreen mode

Then, build and upload your package:

python setup.py sdist bdist_wheel
twine upload dist/*
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🔹 Step 9: Install Your Package

Once uploaded to PyPI, your package can be installed with:

pip install my_pypi_package
Enter fullscreen mode Exit fullscreen mode

🎯 Additional Features in Cookiecutter PyPackage

  • Pre-configured Testing with unittest or pytest
  • 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:


💬 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!

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️