Packaging Your Python Module
Understanding the Setup.py File
The setup.py
file is the heart of a Python package distribution. It contains metadata about your package, such as the name, version, author, description, and dependencies. Here's an example setup.py
file:
from setuptools import setup, find_packages
setup(
name='my_package',
version='1.0.0',
description='A sample Python package',
author='LabEx',
packages=find_packages(),
install_requires=[
'numpy>=1.19.2',
'pandas>=1.1.3',
],
)
Creating a Source Distribution
To create a source distribution of your Python package, you can use the setuptools
library. Follow these steps:
-
Create the setup.py
file in the root directory of your package.
-
Open a terminal and navigate to the root directory of your package.
-
Run the following command to create the source distribution:
python setup.py sdist
This will create a dist
directory in your package's root directory, containing a .tar.gz
file, which is the source distribution of your package.
Anatomy of a Source Distribution
A source distribution of a Python package typically contains the following files:
my_package/
: The directory containing your package's modules and subpackages.
my_package.egg-info/
: Metadata about your package, including the PKG-INFO
file.
setup.py
: The file used to build and distribute your package.
MANIFEST.in
: An optional file that specifies additional files to include in the distribution.
graph TD
dist[dist/] --> tarball[my_package-1.0.0.tar.gz]
root[my_package/] --> init[__init__.py]
root --> module1[module1.py]
root --> module2[module2.py]
root --> setup[setup.py]
root --> manifest[MANIFEST.in]
root --> egginfo[my_package.egg-info/]
egginfo --> pkginfo[PKG-INFO]
By creating a source distribution of your Python package, you can easily share your code with others and make it available for installation.