Minimal Python setup.py with prerequisites

1 minute read

Related: Upload user Python module to PyPi


python -m pip install -e . 

easily and quickly installs complicated lists of prerequisites. The best place to list prerequisites is setup.cfg, which is human- and machine-parseable without first installing the package. This is important for reasons including:

  • reproducible results
  • security risk mitigation
  • getting package prerequisite tree list

Example

This is an example of best practices (since 2016) of minimal setup.py using setup.cfg. It does NOT use requirements.txt or .coveragerc.

setup.py in modern Python packages can be a one-liner:

from setuptools import setup; setup()

setup.cfg holds the machine-readable configuration, easy for humans too:

[metadata]
name = mycoolpkg
author = Joe Smith
description = My awesome program prints cool messages
version = 0.8.1
url = https://github.com/joe/mycoolpkg
keywords = 
  cool printing
  networking
classifiers = 
  Development Status :: 4 - Beta
  Intended Audience :: Science/Research
  Programming Language :: Python :: 3.6
  Programming Language :: Python :: 3.7
  Topic :: Scientific/Engineering
long_description = file: README.md
long_description_content_type = text/markdown
license_file = LICENSE

[options]
python_requires = >= 3.6
setup_requires = 
  setuptools >= 38.6
  pip >= 10
  twine >= 1.11
include_package_data = True
packages = find:
install_requires =
#  colorama
  
[options.extras_require]
tests = 
  pytest
  pytest-cov
  coveralls
  flake8
  mypy
  
[options.entry_points]
console_scripts =
#  joesprint = joesprint:main
  
[flake8]
max-line-length = 132
exclude = .git,__pycache__,doc/,docs/,build/,dist/,archive/

[coverage:run]
cover_pylib = false
omit =
    /home/travis/virtualenv/*
    */site-packages/*
    
[coverage:report]
exclude_lines =
    pragma: no cover
    def __repr__
    RuntimeError
    NotImplementedError
    FileNotFoundError
    ImportError

Notes

  • install_requires canNOT read requirements.txt as file: is not in the setup.cfg install_requires input types
  • console_scripts expects a file joesprint.py with the function main designed to accept command line input, perhaps using argparse.ArgumentParser
  • python_requires parameter avoids user confusion if they have an old Python version. Instead of them opening a GitHub issue or just not using your program at all, they’ll get a useful error message.

Classifiers

Classifiers are optional, but help your project be indexed better in PyPi (and hence search engines). Classifiers must be from this official classifiers list or they will fail when uploading your package to PyPi.

setup.py Fortran f2py

Python can easily import Fortran code using f2py. See this f2py example setup.py.

Notes

Leave a Comment