python-infer

Options controlling which dependencies will be inferred for Python targets.

Backend: pants.backend.python
Config section: [python-infer]

Basic options

imports

--[no-]python-infer-imports

PANTS_PYTHON_INFER_IMPORTS

default: True

Infer a target's imported dependencies by parsing import statements from sources.

To ignore a false positive, you can either put # pants: no-infer-dep on the line of the import or put !{bad_address} in the dependencies field of your target.


string_imports

--[no-]python-infer-string-imports

PANTS_PYTHON_INFER_STRING_IMPORTS

default: False

Infer a target's dependencies based on strings that look like dynamic dependencies, such as Django settings files expressing dependencies as strings.

To ignore a false positive, you can either put # pants: no-infer-dep on the line of the string or put !{bad_address} in the dependencies field of your target.


string_imports_min_dots

--python-infer-string-imports-min-dots=<int>

PANTS_PYTHON_INFER_STRING_IMPORTS_MIN_DOTS

default: 2

If --string-imports is True, treat valid-looking strings with at least this many dots in them as potential dynamic dependencies. E.g., 'foo.bar.Baz' will be treated as a potential dependency if this option is set to 2 but not if set to 3.


assets

--[no-]python-infer-assets

PANTS_PYTHON_INFER_ASSETS

default: False

Infer a target's asset dependencies based on strings that look like Posix filepaths, such as those given to open or pkgutil.get_data.

To ignore a false positive, you can either put # pants: no-infer-dep on the line of the string or put !{bad_address} in the dependencies field of your target.


assets_min_slashes

--python-infer-assets-min-slashes=<int>

PANTS_PYTHON_INFER_ASSETS_MIN_SLASHES

default: 1

If --assets is True, treat valid-looking strings with at least this many forward slash characters as potential assets. E.g. 'data/databases/prod.db' will be treated as a potential candidate if this option is set to 2 but not to 3.


init_files

--python-infer-init-files=<InitFilesInference>

PANTS_PYTHON_INFER_INIT_FILES

one of: always, content_only, never
default: content_only

Infer a target's dependencies on any __init__.py files in the packages it is located in (recursively upward in the directory structure).

Even if this is set to never or content_only, Pants will still always include any ancestor __init__.py files in the sandbox. Only, they will not be "proper" dependencies, e.g. they will not show up in ./pants dependencies and their own dependencies will not be used.

By default, Pants only adds a "proper" dependency if there is content in the __init__.py file. This makes sure that dependencies are added when likely necessary to build, while also avoiding adding unnecessary dependencies. While accurate, those unnecessary dependencies can complicate setting metadata like the interpreter_constraints and resolve fields.


conftests

--[no-]python-infer-conftests

PANTS_PYTHON_INFER_CONFTESTS

default: True

Infer a test target's dependencies on any conftest.py files in the current directory and ancestor directories.


entry_points

--[no-]python-infer-entry-points

PANTS_PYTHON_INFER_ENTRY_POINTS

default: True

Infer dependencies on targets' entry points, e.g. pex_binary's entry_point field, python_awslambda's handler field and python_distribution's entry_points field.


unowned_dependency_behavior

--python-infer-unowned-dependency-behavior=<UnownedDependencyUsage>

PANTS_PYTHON_INFER_UNOWNED_DEPENDENCY_BEHAVIOR

one of: error, warning, ignore
default: ignore

How to handle imports that don't have an inferrable owner.

Usually when an import cannot be inferred, it represents an issue like Pants not being properly configured, e.g. targets not set up. Often, missing dependencies will result in confusing runtime errors like ModuleNotFoundError, so this option can be helpful to error more eagerly.

To ignore any false positives, either add # pants: no-infer-dep to the line of the import or put the import inside a try: except ImportError: block.


Advanced options

None

Deprecated options

inits

--[no-]python-infer-inits

PANTS_PYTHON_INFER_INITS

default: False

Deprecated, is scheduled to be removed in version: 2.14.0.dev1.
Use the more powerful option `[python-infer].init_files`. For identical behavior, set to 'always'. Otherwise, we recommend the default of `content_only` (simply delete the option `[python-infer].inits` to trigger the default).


Infer a target's dependencies on any __init__.py files in the packages it is located in (recursively upward in the directory structure).

Even if this is disabled, Pants will still include any ancestor __init__.py files, only they will not be 'proper' dependencies, e.g. they will not show up in ./pants dependencies and their own dependencies will not be used.

If you have empty __init__.py files, it's safe to leave this option off; otherwise, you should enable this option.



Did this page help you?