Today we’re going to talk about one of the most important aspects of sharing your code with others: packaging. But before we dive in, let’s take a moment to appreciate how far we’ve come as programmers. Remember when you had to email someone a .zip file and hope they could figure out what was inside? Those days are over thanks to Python’s built-in distribution tools!
So, what exactly is packaging in Python land? It’s the process of creating an archive (usually a .tar.gz or .zip) that contains your code along with instructions on how to install it. There are two main types of distributions: source distributions (sdists) and built distributions (bdists).
Source Distributions (sdist): The Simpler Option
An sdist is like sharing just the source code it doesn’t include any platform-specific binaries, so it works for all platforms. When you create an sdist, your client will have to build it themselves once they download it. This can be a bit of a hassle, but it ensures that everyone has access to the same code and instructions on how to install it.
Here’s what creating an sdist looks like:
bash
# This script creates a source distribution (sdist) of a Python package using the setup.py file.
# An sdist is a compressed archive of the package's source code, which can be easily distributed and installed on different platforms.
# To create an sdist, we use the "sdist" command in the setup.py file, which is a Python script that contains instructions for building and distributing the package.
# The "python" command is used to run the setup.py file, followed by the "sdist" command to specify the type of distribution we want to create.
bash
python setup.py sdist
This will create a .tar.gz file in your project directory with all of your source code, as well as some metadata (like the version number) and instructions on how to build it.
Built Distributions (bdist): The More Complex Option
A bdist is like sharing pre-built binaries for specific platforms this can be a huge time saver for clients who don’t want to spend hours building your code themselves. However, creating a bdist requires more work on the part of the package author because you have to build it for multiple platforms and versions.
Here’s what creating a bdist looks like:
bash
# This script is used to create a bdist (built distribution) for a Python package.
# A bdist is a pre-built package that can be easily installed on different platforms.
# This saves time for clients who do not want to build the code themselves.
# To create a bdist, the package author needs to run the following command:
python setup.py bdist_wheel
# This command uses the setup.py file to build a wheel distribution of the package.
# The bdist_wheel command is used to create a wheel distribution, which is a built package format for Python.
# This format is compatible with multiple platforms and versions, making it easier for clients to install the package.
# The setup.py file contains instructions for building the package, including dependencies and installation requirements.
# This file is essential for creating a bdist and should be included in the package repository.
# To run this script, the package author needs to have Python installed on their system.
# They can then navigate to the package directory and run the script using the following command:
bash create_bdist.sh
# This will execute the script and create a bdist for the package.
This will create a .whl file in your project directory that contains pre-built binaries for specific platforms (like Windows, Linux, or Mac) and versions of Python (like 3.9). This is great because it ensures maximum compatibility with different systems, but it can be more time consuming to maintain all those different builds.
So, which one should you choose? It really depends on your audience if they’re mostly developers who are comfortable building code themselves, then an sdist might be the way to go. But if you have a wider range of clients with varying technical abilities, then creating both sdists and bdists is probably best.