DEV Community

Md Mahmud Hasan
Md Mahmud Hasan

Posted on

Create a python package and publish.

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

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

Then, in my_package/__init__.py, make the package accessible by importing your module:

from .my_module import greet
Enter fullscreen mode Exit fullscreen mode

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

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

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

Usage

from my_package import greet
print(greet("World"))  # Output: Hello, World!
Enter fullscreen mode Exit fullscreen mode

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

7. Create requirements.txt (Optional)

If your package has any external dependencies, list them in the requirements.txt file:

requests==2.25.1
Enter fullscreen mode Exit fullscreen mode

8. Test Your Package Locally

Before publishing, it’s a good idea to test your package locally.

  1. Install your package locally by running:
   pip install -e .
Enter fullscreen mode Exit fullscreen mode

This installs the package in "editable" mode, meaning changes to the code will be reflected immediately.

  1. Run your tests to ensure everything is working:
   python -m unittest discover
Enter fullscreen mode Exit fullscreen mode

9. Create a Source Distribution and Wheel

To prepare your package for publication, you need to create distribution archives (source tarball and wheel).

  1. Install setuptools and wheel if you haven't already:
   pip install setuptools wheel
Enter fullscreen mode Exit fullscreen mode
  1. Run the following command to create the distributions:
   python setup.py sdist bdist_wheel
Enter fullscreen mode Exit fullscreen mode

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.

  1. Install twine to upload your package:
   pip install twine
Enter fullscreen mode Exit fullscreen mode
  1. Upload the package to PyPI using twine:
   twine upload dist/*
Enter fullscreen mode Exit fullscreen mode

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

12. Maintain Your Package

After publishing, you can update your package by:

  1. Making changes to your code.
  2. Incrementing the version number in setup.py.
  3. Running the python setup.py sdist bdist_wheel command again.
  4. 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)