Python Forum

Full Version: Why is mpl_toolkits loaded?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
There is a strange behavior of Python on my machine:
~/P/S/2021-01 python3.8
Python 3.8.7 (default, Dec 21 2020, 21:23:03) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys                                                                               
>>> sys.modules['mpl_toolkits']                                                              
<module 'mpl_toolkits' (namespace)>
>>> import pkg_resources
>>> sys.modules['mpl_toolkits']
<module 'mpl_toolkits' from '/usr/lib/python3/dist-packages/mpl_toolkits/'>
When I start Python 3.8, there is already an 'mpl_toolkits' entry in sys.modules, but this module is related to matplotlib, so how comes it appears in sys.modules without any attempt to use matplotlib?

Worse, if I import pkg_resources, which is not related to matplotlib, this time the mpl_toolkits module is loaded from the dist-packages directory.

Can someone give me a clue about this? I'm puzzled Bonkself
I think that you just have a reference to it. It is not imported. Find out how with
python -v
(beware of massive output).
You will probably find a line like
# possible namespace for /<some path...>/site-packages/mpl_toolkits
Also see what happens with
python -S # Don't load the "site" module
>>> import sys
>>> sys.modules
It should not appear in the output. If it does I'll be very puzzled. Not loading the site module means, among other things, that the site-packages directories aren't added to the module path at initialization.