Creating and publishing a Python package involves several steps. Below is the full cycle from starting to create a Python package to publishing it on PyPI (Python Package Index).
1. Prepare Your Package Directory Structure
Create a project directory with the necessary structure:
my_package/
├── my_package/ # The package folder
│ ├── __init__.py # Mark as a Python package
│ └── my_module.py # Your module code
├── tests/ # Unit tests
│ └── test_my_module.py
├── setup.py # Package setup configuration
├── README.md # Documentation for your package
├── LICENSE # License file
└── requirements.txt # List of dependencies (optional)
2. Create Your Package Code
In the my_package
directory, create your Python code.
For example, in my_package/my_module.py
:
def greet(name):
return f"Hello, {name}!"
Then, in my_package/__init__.py
, make the package accessible by importing your module:
from .my_module import greet
3. Write Unit Tests
In the tests
directory, create a test file test_my_module.py
to test your package:
import unittest
from my_package import greet
class TestGreet(unittest.TestCase):
def test_greet(self):
self.assertEqual(greet("World"), "Hello, World!")
if __name__ == '__main__':
unittest.main()
4. Write the setup.py
File
The setup.py
file is used to define the package metadata and installation details.
Create setup.py
at the root level of your project:
from setuptools import setup, find_packages
setup(
name="my_package", # Name of your package
version="0.1", # Version number
packages=find_packages(), # Find all packages in the directory
install_requires=[ # External dependencies (optional)
# "requests", # Uncomment to add external packages
],
test_suite='tests', # Define the location of your tests
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
author="Your Name",
author_email="your.email@example.com",
description="A simple package for greeting",
long_description=open('README.md').read(),
long_description_content_type="text/markdown",
url="https://github.com/yourusername/my_package", # Replace with your repo URL
)
5. Write the README.md
File
Create a README.md
file with some basic information about your package. This file will be displayed on your PyPI page.
# My Package
This is a simple Python package that greets users.
## Installation
To install the package, run:
pip install my_package
Usage
from my_package import greet
print(greet("World")) # Output: Hello, World!
6. Create a LICENSE
File
Choose a license for your package. A common choice is the MIT license. You can create a LICENSE
file with the following content:
MIT License
Copyright (c) 2025 Your Name
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
...
7. Create requirements.txt
(Optional)
If your package has any external dependencies, list them in the requirements.txt
file:
requests==2.25.1
8. Test Your Package Locally
Before publishing, it’s a good idea to test your package locally.
- Install your package locally by running:
pip install -e .
This installs the package in "editable" mode, meaning changes to the code will be reflected immediately.
- Run your tests to ensure everything is working:
python -m unittest discover
9. Create a Source Distribution and Wheel
To prepare your package for publication, you need to create distribution archives (source tarball and wheel).
- Install
setuptools
andwheel
if you haven't already:
pip install setuptools wheel
- Run the following command to create the distributions:
python setup.py sdist bdist_wheel
This will create a dist/
folder containing .tar.gz
and .whl
files, which are the files that can be uploaded to PyPI.
10. Publish Your Package to PyPI
To upload your package to PyPI, you need to create an account on PyPI.
- Install
twine
to upload your package:
pip install twine
- Upload the package to PyPI using
twine
:
twine upload dist/*
This will prompt you for your PyPI username and password. After authentication, your package will be uploaded.
11. Install Your Package from PyPI
Once your package is published, you can install it using pip
:
pip install my_package
12. Maintain Your Package
After publishing, you can update your package by:
- Making changes to your code.
- Incrementing the version number in
setup.py
. - Running the
python setup.py sdist bdist_wheel
command again. - Uploading the new version to PyPI using
twine upload dist/*
.
Additional Tools and Tips:
- Tox: For testing your package on multiple Python versions.
- Sphinx: For generating documentation.
-
Pytest: As an alternative to
unittest
. - Pre-commit hooks: To ensure code quality and consistency before commits.
Top comments (0)