How to require importlib.resources in a Python setup script

Viewed 1063

I have a Python package that attempts to import the importlib.resources module (added in Python 3.7, as I understand), and then falls back on importing importlib_resources if this fails. importlib_resources is provided by the PyPI package importlib-resources.

So, I want my pip install dependencies to be "nothing if running 3.7 or newer, importlib-resources otherwise".

Is there some way to specify this dependency in the setup script?

1 Answers

This is done using environment markers. The syntax for a setuptools build system looks like this:

from setuptools import setup

setup(
    ...
    install_requires=[
        'importlib_resources; python_version < "3.7"',
    ]
)
Related