Skip to content
Advertisement

How to find everything imported by an import statement from python documentation?

I was trying to some code in python and noted this peculiar case.

import importlib
print(importlib.abc)

The above code runs fine in python 3.7 but not in python 3.10.2. In python 3.10.2, i get the following error:

AttributeError: module 'importlib' has no attribute 'abc'. Did you mean: '_abc'?

I tried to look at the documentation as to whether the behaviour of the above code got changed somehow. But couldn’t find it. The documentations i tried looking are Python 3.7 importlib documentation and Python 3.10.2 importlib documentation.

So i wish to know how to find things imported by an import statement on a python standard library through documentation.

Note: I do know that i can look at dir(importlib) to see everything importlib has after it is imported. But i want to find it through documentation so i can know them without using that specific python version.

Advertisement

Answer

If you want to import a submodule of a package, you should do so explicitly:

import importlib.abc

Python will not automatically load all submodules of a package when you import the package. Whether submodules will be available without an explicit submodule import depends on whether some code somewhere else in the program has already loaded the module.

As for the question in the title, documentation will usually not have the information you’re asking for. Good documentation will thoroughly describe a module’s entire public API, but you’re asking for information beyond that.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement