unbox.missing_install_names¶
Finding missing install names
For doing things like:
>>> from unbox import print_missing_names
>>> import unbox
>>> print_missing_names(unbox)
bs4
requests
Note
These missing dependencies are real, unpurpose, but don’t affect you.
You can get the set of missing install names as well as the set of install names that are not used.
What does this mean?
Install names are those you have declared you needed to install the packages: the
[project] dependencies of your pyproject.toml, or the [options]
install_requires of your setup.cfg if that’s what you use. Both are read
(pyproject.toml first) – see find_install_names.
Why are the missing or unused? It has to do with imports. If you declared you needed a package to be installed (a dependency) but never import it, it’s unused. More importantly, if you use (that is import) a package, but haven’t listed it in the names to be installed, that’s a problem for your users.
There’s several problems to solve here (and you have function here to solve them).
One problem: finding the names that are imported. That’s solved by import_for and
the many subfunctions like import_for.third_party.
Another problem is that the names that are imported are not necessarily the names that
should be installed.
For example, you import sklearn but you pip install scikit-learn.
That’s annoying. So we need a mapping between import names and install names.
So we provide a default mapping (which we will expand as users notify us, or we encounter more of those aggravating cases.
That said, we provide ways for users to specify their own map.
You can specify an explicit mapping from import name to install name in the form of a
dict or any other valid Mapping instance. You can also add a
IMPORT_TO_INSTALL_NAME_MAP_FILE environmental variable pointing to a JSON file path
that will be used if this explicit mapping is not given.
If you do none of those, the system will use the default mapping, which is expressed by
the unbox/data/dflt_import_to_install_name_map.json file in the packaged data.
Besides the import vs install name discrepency, there are other reasons you may want
to specify your own map.
You may want to depend on a particular version of a third-party package,
or ensure that the version is greater than a minimum version. So the import names
you extracted need to be mapped to a fuller specification. For example, instead
of just requiring numpy you may want to have numpy >= 1.3 in your
setup.cfg or requirements.txt.
Module Attributes
Sources of declared install names, tried in order by |
Functions
|
Extract the declared dependencies from |
|
Generate all dependencies from a mapping of setup.cfg files. |
Extract dependencies from a single setup.cfg file content. |
|
|
Diff declared install names against the install names the imports call for. |
|
Get the imported names that are not declared to be installed those names declared to be installed that are not actually imported. |
|
Find the install names (dependencies) a package declares. |
|
Get the import names from the given input. |
|
|
|
|
|
|
Flexible search for the |
|
Flexible search for the |
|
|
Get a set of install names, i.e. names that are used in |
|
Get dependencies from a package's |
Get dependencies from setup.cfg file(s). |
|
Get dependencies from setup.cfg file(s). |
|
|
|
Get dependencies from a package's |
|
Get dependencies from setup.cfg file(s). |
|
|
|
|
See |
Exceptions
To raise when a module requested is probably not on the python path |
- unbox.missing_install_names.DFLT_INSTALL_NAMES_FINDERS = (<function module_requirements_according_to_pyproject>, <function module_requirements_according_to_setupcfg>)¶
Sources of declared install names, tried in order by
find_install_names.
- exception unbox.missing_install_names.ProbablyPythonPathError[source]¶
Bases:
ValueErrorTo raise when a module requested is probably not on the python path
- unbox.missing_install_names.dependencies_from_pyproject_content(pyproject_content, *, extras=False)[source]¶
Extract the declared dependencies from
pyproject.tomltext (PEP 621).By default only the required
[project] dependenciesare yielded:>>> content = ''' ... [project] ... name = "proj" ... dependencies = ["requests", "numpy>=1.20"] ... [project.optional-dependencies] ... dev = ["pytest>=7.0"] ... docs = ["sphinx"] ... ''' >>> list(dependencies_from_pyproject_content(content)) ['requests', 'numpy>=1.20']Pass
extras=Trueto also yield every[project.optional-dependencies]group, or an iterable of group names to pick specific ones:>>> list(dependencies_from_pyproject_content(content, extras=True)) ['requests', 'numpy>=1.20', 'pytest>=7.0', 'sphinx'] >>> list(dependencies_from_pyproject_content(content, extras=['dev'])) ['requests', 'numpy>=1.20', 'pytest>=7.0']Absent or empty sections simply yield nothing (note that
[build-system] requiresare build-time requirements, not dependencies, so are ignored):>>> list(dependencies_from_pyproject_content( ... '[build-system]\nrequires = ["hatchling"]' ... )) []
- unbox.missing_install_names.dependencies_from_setup_configs(setup_configs)[source]¶
Generate all dependencies from a mapping of setup.cfg files.
- Parameters:
setup_configs (
Union[str,Path,Mapping[str,str],Iterable[str]]) – Mapping of filepath to setup.cfg file contents- Yields:
str– Individual dependency strings from all setup.cfg files- Return type:
Example
>>> configs = { ... 'proj1/setup.cfg': '[options]\ninstall_requires =\n requests\n numpy>=1.20' ... } >>> list(dependencies_from_setup_configs(configs)) ['requests', 'numpy>=1.20']
- unbox.missing_install_names.dependencies_from_setup_configs_content(setup_cfg_content)[source]¶
Extract dependencies from a single setup.cfg file content.
- unbox.missing_install_names.dependency_diff(install_names, import_names=None, import_to_install_name_map=None, strict=False, install_names_finder=<function find_install_names>, *, imports_finder=functools.partial(<function imports_for>, post=<function _third_party_first_level_names>))[source]¶
Diff declared install names against the install names the imports call for.
See
dependency_diff_for_pkg, which is the same thing for a package.
- unbox.missing_install_names.dependency_diff_for_pkg(pkg, import_to_install_name_map=None, strict=False, install_names_finder=<function find_install_names>, *, exclude_tests=False)[source]¶
Get the imported names that are not declared to be installed those names declared to be installed that are not actually imported.
- Parameters:
Options (you won’t have to deal with most of the time):
- Parameters:
strict – Whether you want to allow only those names that are explicitly declared in
import_to_install_name_mapor not. (Default is False).install_names_finder (
Callable[[Union[str,ModuleType]],Iterable[str]]) – A function that takes the package and finds the declared install names (by default looks inpyproject.toml, thensetup.cfg, but you can make it look forrequirements.txt, or where-ever).exclude_tests (
bool) – Whether to ignore the imports made only by the package’s own tests (an in-packagetests/,test_*.py,conftest.py: seeunbox.DFLT_TEST_MODULE_PATTERNS). Those imports (pytest,hypothesis, …) are needed to develop the package, not to run it, so counting them reports them as missing install requirements. Default isFalse, which keeps the historical (whole-source-tree) behavior.
- Returns:
The {import_names - install_names} and {install_names - import_names} sets.
The typical use would be when you want to add missing dependencies in your install instructions. You would then do:
>>> import unbox >>> missing = dependency_diff_for_pkg(unbox)[0] >>> print(*sorted(missing), sep='\n') bs4 requestsNote
These packages were purposely omitted in the install instructions of
unboxbecause they’re only needed in the internal module_acquire_builtin_namesthat is for development purporses only.
- unbox.missing_install_names.find_install_names(pkg, *, finders=(<function module_requirements_according_to_pyproject>, <function module_requirements_according_to_setupcfg>))[source]¶
Find the install names (dependencies) a package declares.
Tries
pyproject.toml(PEP 621[project] dependencies) first, then falls back tosetup.cfg([options] install_requires) for legacy projects.- Parameters:
pkg – An imported package, or a path to one.
finders – The sources to try, in order. Each is called with
pkgand should returnNonewhen it has nothing to say.
- Raises:
ValueError – if none of the
findersfound any declared names.- Return type:
- unbox.missing_install_names.get_import_names(import_names, imports_finder=functools.partial(<function imports_for>, post=<function _third_party_first_level_names>))[source]¶
Get the import names from the given input.
- unbox.missing_install_names.get_pyproject_path(x)[source]¶
Flexible search for the
pyproject.tomlpath for an object x>>> import unbox >>> get_pyproject_path(unbox).endswith('pyproject.toml') True
- unbox.missing_install_names.get_setupcfg_path(x)[source]¶
Flexible search for the
setup.cfgpath for an object x>>> import unbox >>> get_setupcfg_path(unbox).endswith('setup.cfg') True
- unbox.missing_install_names.install_names_for_imports(import_names, import_to_install_name_map=None, strict=False, *, imports_finder=functools.partial(<function imports_for>, post=<function _third_party_first_level_names>))[source]¶
Get a set of install names, i.e. names that are used in
pip install PKG_NAME.There are a few use cases for this.
First, the install names can be different from the names that are used to import a package. For example, you
import sklearnbut youpip install scikit-learn.Secondly, you may want to depend on a particular version of a third-party package, or ensure that the version is greater than a minimum version. So the import names you extracted need to be mapped to a fuller specification. For example, instead of just requiring
numpyyou may want to havenumpy >= 1.3in yoursetup.cfgorrequirements.txt.- Parameters:
import_names (
Union[str,ModuleType,Iterable[str]]) – An iterable of import names, or a module, package, or filepath/folderpath thereof, to be able to extract themimport_to_install_name_map (
dict|None) – A dict mapping import names (keys) to install namesstrict – Whether to assert that all import_names are in the map
imports_finder (
Callable[[Union[str,ModuleType]],Iterable[str]]) – How to extract import names whenimport_namesis a module/package/path rather than an iterable of names. Defaults toimports_for.third_party(every module under the root); passimports_for.runtimeto leave out imports made only by the package’s tests.
- Return type:
- Returns:
- unbox.missing_install_names.install_names_from_pyproject_file(pkg, *, extras=False)¶
Get dependencies from a package’s
pyproject.toml.- Parameters:
- Return type:
- Returns:
The list of dependency strings, or
Noneif there is nopyproject.tomlto read – so that callers can fall back to another source (seefind_install_names).
- unbox.missing_install_names.install_names_from_setup_cfg_file(pkg)¶
Get dependencies from setup.cfg file(s).
- Parameters:
pkg –
Can be:
A module/package object
A string path to setup.cfg or directory containing it
A Mapping of {filepath: setup.cfg_content} for batch processing
- Return type:
- Returns:
List of dependency strings for single files, Iterator for Mappings, None if not found
Example
>>> # For batch processing multiple setup.cfg contents: >>> configs = { ... 'proj1/setup.cfg': '[options]\ninstall_requires =\n requests\n numpy>=1.20' ... } >>> list(module_requirements_according_to_setupcfg(configs)) ['requests', 'numpy>=1.20']
- unbox.missing_install_names.install_requires_of_module(pkg)¶
Get dependencies from setup.cfg file(s).
- Parameters:
pkg –
Can be:
A module/package object
A string path to setup.cfg or directory containing it
A Mapping of {filepath: setup.cfg_content} for batch processing
- Return type:
- Returns:
List of dependency strings for single files, Iterator for Mappings, None if not found
Example
>>> # For batch processing multiple setup.cfg contents: >>> configs = { ... 'proj1/setup.cfg': '[options]\ninstall_requires =\n requests\n numpy>=1.20' ... } >>> list(module_requirements_according_to_setupcfg(configs)) ['requests', 'numpy>=1.20']
- unbox.missing_install_names.module_requirements_according_to_pyproject(pkg, *, extras=False)[source]¶
Get dependencies from a package’s
pyproject.toml.- Parameters:
- Return type:
- Returns:
The list of dependency strings, or
Noneif there is nopyproject.tomlto read – so that callers can fall back to another source (seefind_install_names).
- unbox.missing_install_names.module_requirements_according_to_setupcfg(pkg)[source]¶
Get dependencies from setup.cfg file(s).
- Parameters:
pkg –
Can be:
A module/package object
A string path to setup.cfg or directory containing it
A Mapping of {filepath: setup.cfg_content} for batch processing
- Return type:
- Returns:
List of dependency strings for single files, Iterator for Mappings, None if not found
Example
>>> # For batch processing multiple setup.cfg contents: >>> configs = { ... 'proj1/setup.cfg': '[options]\ninstall_requires =\n requests\n numpy>=1.20' ... } >>> list(module_requirements_according_to_setupcfg(configs)) ['requests', 'numpy>=1.20']
- unbox.missing_install_names.print_missing_names(pkg, import_to_install_name_map=None, strict=False, install_names_finder=<function find_install_names>, *, exclude_tests=False)[source]¶
See
dependency_diff_for_pkgfor more info (includingexclude_tests).>>> import unbox >>> print_missing_names(unbox) bs4 requests