-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Access of top-level matplotlib
modules raises AttributeError
#26016
Comments
This is standard Python behavior, importing a package does not implicitly import all sub-packages. This is good both from an import-time point of view (no need to import things you will never use), modules can do significant work at import time, not all sub-packages are garunteed to be importable (e.g. you do not have an optional dependency installed and we fail at import rather than use time), or the sub-packages are mutually exclusive. If you want to access objects in the import matplotlib.figure
def do_something_with_figure(fig: matplotlib.figure.Figure):
pass
print('doing some actual runtime code...') Due to the way that the Python module import system works, if anything else does There was a PEP to add lazy importing to the language (https://peps.python.org/pep-0690/) however it was rejected. There is on-going work to add lazy loading (so we can make all of the top level modules available without paying the import costs) across the scientific Python ecosystem (https://scientific-python.org/specs/spec-0001/ ) but we have not adopted that yet. We now have a matplotlib/lib/matplotlib/__init__.py Lines 104 to 133 in d464cbf
If you want any other sub-packages you must import them explicitly to be sure. I would not even rely on the three that are there are available as an implementation detail and may change in the future. As a technical note, we need I am going to close this with no-action as it is a duplicate of #22247 . I am contemplating closing both, but lets keep further discussion on that issue which has more discussion already. |
Thanks for the response @tacaswell - I probably should have been more familiar with python's import behaviour! I'll update any usage to explicitly import the required sub-packages. |
Python's import behavior is fiendishly complicated (see https://docs.python.org/3/reference/import.html ) and for most use cases you can get away with not thinking about it too much (the behind the curtain complication makes the user facing side look simple)! |
Bug summary
Direct access to top level modules of
matplotlib
raisesAttributeError
for most of the documented top-level modules. This was discovered when attempting to refer to module contents for type-hints that are evaluated when a function is defined. This occurs reproducibly where no previous matplotlib module imports have occurred prior to evaluation of the code referencing a module namespaced bymatplotlib
Code for reproduction
Actual outcome
Expected outcome
For this example script, we would obviously expect to reach the
print()
statement.Additional information
This appears to be a general issue with most top-level modules. The following attempts to access all top-level modules named in the current
help(matplotlib)
:Note - I think this issue is due to some sort of lazy-loading behaviour, there are various ways by which the
AttributeError
can be avoided. If previous imports such asimport matplotlb.pyplot as plt
have already occurred before references tomatplotlib.xxxx
occur this appears ok, and also, while investigating interactively, it appears that simply showing thehelp()
in the python shell unblocks this access:The above interactive behaviour can be replicated by invoking
pydoc.render_doc()
; the following version of the code example gives no error:All examples in this report are with matplotlib installed in python3.8 bullseye docker image via pip as follows:
Operating system
Observed on Debian buster & bullseye and Ubuntu 18.04, 20.04 & 22.04
Matplotlib Version
Various. Code examples using 3.7.1, with behaviour definitely occurring with 3.5.2, 3.6.2 & 3.6.3
Matplotlib Backend
'agg'
Python version
Various 3.8.x, 3.9.x
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: