Beyond Local Scripts: How to Package and Publish Python Code

ArjanCodes////3 min read

Overview

Packaging Python code transforms a collection of scripts into a professional, reusable library. This process allows developers to share binary files and source code via a package manager, ensuring dependencies remain managed and installation stays consistent across different environments. By publishing to the PyPI repository, you enable others to install your work with a simple command, moving beyond manual file sharing to a scalable distribution model.

Beyond Local Scripts: How to Package and Publish Python Code
How to Package and Publish Python Code the Right Way

Prerequisites

To follow this guide, you should have a solid grasp of Python fundamentals and familiarity with the command line. You must have Python installed (version 3.10 or newer is recommended). Additionally, you will need a PyPI account for the final publishing step and a basic understanding of directory structures, specifically how __init__.py files designate packages.

Key Libraries & Tools

  • setuptools: The standard library for building and distributing Python packages.
  • wheel: A tool that creates a binary distribution format, making installation faster than source distributions.
  • twine: A utility used to securely upload your packaged distributions to PyPI.
  • PyPI: The official third-party software repository for Python.

Code Walkthrough

The heart of the process lies in the setup.py file. This script uses setuptools to define your package's metadata and dependencies.

from setuptools import setup, find_packages

with open("README.md", "r") as f:
    long_description = f.read()

setup(
    name="id_generator",
    version="0.1.0",
    description="A library for generating various IDs",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=find_packages(where="app"),
    package_dir={"": "app"},
    install_requires=["bson"],
    extras_require={"dev": ["pytest", "twine"]},
    python_requires=">=3.10",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3.10",
    ],
)

In this snippet, find_packages automatically locates the source code in the app directory. The install_requires list ensures that bson installs automatically whenever a user downloads your package. We use long_description to pull in the README, ensuring the project page on PyPI looks professional.

To build the package, run the following commands in your terminal:

# Create the binary (wheel) and source distribution (sdist)
python setup.py bdist_wheel sdist

# Check the build for errors
twine check dist/*

Syntax Notes

Python packaging uses specific conventions for folder structures. Your source code should reside in a subfolder (like app/ or src/) to prevent the build tools from accidentally including test files or configuration scripts in the final distribution. Always include a LICENSE file and an __init__.py in your package directory to signify that the folder is an importable Python package.

Practical Examples

Packaging is indispensable for internal company tools or open-source utilities. For instance, if you build a custom ID generator used by both a front-end Django app and a back-end data processing script, packaging the generator allows both projects to track it as a versioned dependency. This prevents "code rot" where different parts of a system use different versions of the same logic.

Tips & Gotchas

Avoid the trap of publishing directly to the live PyPI server immediately. Use the TestPyPI repository first. It acts as a sandbox, allowing you to verify that your README renders correctly and the installation works without cluttering the official registry. Also, ensure you bump your version number in setup.py before every upload; PyPI will reject any file that reuses an existing version string.

Topic DensityMention share of the most discussed topics · 13 mentions across 8 distinct topics
PyPI
38%· products
setuptools
15%· products
Arjan
8%· people
bson
8%· products
Django
8%· products
Other topics
23%
End of Article
Source video
Beyond Local Scripts: How to Package and Publish Python Code

How to Package and Publish Python Code the Right Way

Watch

ArjanCodes // 20:28

On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!

What they talk about
AI and Agentic Coding News
Who and what they mention most
Python
27.3%3
Python
18.2%2
Python
18.2%2
3 min read0%
3 min read