Cookiecutter#
Introduction#
Cookiecutter is a command-line utility that generates projects from templates. It automates the creation of a project structure with predefined files and directories, ensuring consistency and saving time when starting new projects. This tool is particularly useful for developers who want to maintain a standardized setup across multiple projects.
Example Python Package Template: opengeos/cookiecutter-pypackage
Usage#
This guide provides a step-by-step walkthrough for creating a Python package, configuring it for distribution, and managing versioning and releases. Follow these instructions to streamline your workflow.
Prerequisites#
Before proceeding, install the required tools using pip or conda:
Use pip#
pip install cookiecutter bump-my-version
Use conda#
conda install -c conda-forge cookiecutter bump-my-version
Step-by-Step Instructions#
1. Generate the Package Structure#
Use Cookiecutter to create the initial structure of your Python package. This command clones a template repository and sets up the necessary files and directories. Follow the prompts to customize your package:
cookiecutter gh:opengeos/cookiecutter-pypackage
2. Create a Repository on GitHub#
Go to GitHub and create a new empty repository. Makre the repository name the same as your package name.
Do NOT initialize it with a README, license, or .gitignore file.
3. Configure GitHub Actions Permissions#
Enable GitHub Actions to perform read and write operations for your repository:
Go to your repository on GitHub.
Navigate to
Settings
>Actions
>General
>Workflow permissions
.Adjust the settings to allow read and write operations.
4. Clone the Repository#
Clone the newly created GitHub repository to your local machine:
git clone [your-repository-url]
6. Commit and Push Changes#
Commit the changes and push them to your GitHub repository:
git add .
git commit -m "Initial commit"
git push origin main
7. Create a PyPI API Token#
Visit pypi.org and log in or create an account.
Navigate to
Account Settings
>API Tokens
.Create a new API token with the
Entire account
scope. Copy the token for later use.
8. Add PyPI Credentials to GitHub Secrets#
Store your PyPI credentials securely in your GitHub repository:
Go to your repository on GitHub.
Navigate to
Settings
>Secrets
>New repository secret
.Add the following secrets:
PYPI_USERNAME
: Use__token__
as the value.PYPI_PASSWORD
: Use the API token you copied earlier.
9. Create a GitHub Release#
Trigger the deployment of your package by creating a release on GitHub:
Go to the
Releases
section in your GitHub repository.Click
Draft a new release
.Follow the prompts to create the release.
10. Enable GitHub Pages#
To host documentation or other static content, enable GitHub Pages:
Navigate to
Settings
>Pages
.Select the
gh-pages
branch as the source.Click
Save
.
11. Manage Versioning with bump-my-version#
Use bump-my-version
to handle version updates for your package:
Perform a dry run to preview changes:
bump-my-version bump [patch/minor/major] --dry-run --verbose
Apply the version bump:
bump-my-version bump [patch/minor/major]
13. Create a New GitHub Release#
Finally, create a new release on GitHub to reflect the updated version:
Repeat the process described in Step 9.
Conclusion#
By following this guide, you can efficiently set up, configure, and manage a Python package using Cookiecutter and related tools. This workflow ensures consistency, automates repetitive tasks, and simplifies the process of distributing your package.
For more information, refer to the official documentation of Cookiecutter and bump-my-version.