How to Package Python Dependencies for Publication
If you’re packaging a Python package for publication, how do you ensure that all of the required dependencies are included? Just like pip is the standard package manager for Python, setup.py is the heart and center of Python projects installed with pip. Simply put, setup.py is a build script template distributed with Python’s setuptools package.
Setuptools is a package development process library and utility for building Python projects based on packages and their dependencies listed in a setup.py script. A Python file that relies only on the standard library can be redistributed and reused without the need to use setuptools. But for projects that consist of multiple files, need additional libraries, or need a specific version of Python, setuptools will be required.
Update Python Tools
The first step is a best practice: make sure your tools are up to date.
$ python -m pip install –upgrade pip setuptools
The following script is an example of how to create a setup.py script for your project. The script spells out the information you need to provide, including:
- Project name – enter a name for your project in quotes
- Packages to include in the distribution – the find_packages(‘,’) default argument will incorporate all packages that include an __init__.py file and are located in the local directory where setup.py is installed
- Project version number – enter a version number for the project in quotes
- List a license for the project – enter the name of the license you are licensing your project for use under in quotes
- Short description of your library – enter a brief description of your project in quotes
- Long description of your library – enter a more detailed description of your project either in text or using markdown
- Your name – enter your name in quotes to denote who the author is
- Your email address – optionally provide an email address in quotes where users can contact you
- Link to your github repository or website – optionally provide a link to your project’s repo
- Download Link from where the project can be downloaded from – provide an URL to your project’s source code in quotes
- List of keywords – provide a list of keywords in square brackets associated with your project to make it easier to search for
- List project dependencies – provide a list of all the dependencies your project requires in square brackets. The easiest way to provide this is to copy and paste from the install_requires section (see below).
- https://pypi.org/classifiers – provide a list of all classifiers that apply to your project in square brackets. You can find a comprehensive list of classifiers at https://pypi.org/classifiers
from distutils.core import setup
from setuptools import find_packages
# Optional project description in README.md:
current_directory = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(current_directory, ‘README.md’), encoding=’utf-8′) as f:
long_description = f.read()
long_description = ”
# Project name:
# Packages to include in the distribution:
# Project version number:
# List a license for the project, eg. MIT License
# Short description of your library:
# Long description of your library:
# Your name:
# Your email address:
# Link to your github repository or website:
# Download Link from where the project can be downloaded from:
# List of keywords:
# List project dependencies:
install_requires is a section within the setup.py file in which you need to input a list of the minimum dependencies needed for a project to run correctly. When pip runs setup.py, it will install all of the dependencies listed in install_requires.
For example, if your project includes matplotlib, you’ll need to list it, as well as its dependency of numpy in install_requires as shown below:
Example of Install_requires with Platform Specific Dependencies
You can modify install_requires by adding in platform-specific and version-specific dependencies, depending on the environment that a project is to be installed on. For instance, if our previous example included matplotlib v3.2.1 and numpy v1.17.4 on Python 3.6.6 for Linux, we could specify:
“<numpy> >= <1.17.4>;platform_system==’<Linux>‘”
To install a setup.py file including dependencies listed in install_inquires:
$ python setup.py install
When the command is run, all of the dependencies not already installed will be downloaded, built (if necessary), and installed. Any scripts that require specific dependencies at runtime will be installed with wrappers that ensure the correct versions are added to sys.path (system path).
Dependency Not Found in PyPI
If your project has a dependency requirement that is not currently in the Python Package Index (PyPI), you can still include them if they can be accessed via http and are packaged as either an egg, .py file, or a VCS (Version Control System) repository, such as Git or Subversion.
In order to download dependencies not found in PyPI, you will need to add URLs to a dependency_links section under setup() in the setup.py file. Assuming that the dependencies are packaged correctly, they will be automatically installed:
To check a dependency not found in PyPI, replace – with _ in the package name and version # in the dependency_links argument: