It includes all the modules that you have ever imported, so if you are del module , the module will still be displayed in the dict, but that does not mean that you can access it:
In [16]: import BeautifulSoup In [17]: sys.modules["BeautifulSoup"] Out[17]: <module 'BeautifulSoup' from '/usr/local/lib/python2.7/dist-packages/BeautifulSoup.pyc'> In [18]: BeautifulSoup.re Out[18]: <module 're' from '/usr/lib/python2.7/re.pyc'> In [19]: del BeautifulSoup In [20]: sys.modules["BeautifulSoup"] Out[20]: <module 'BeautifulSoup' from '/usr/local/lib/python2.7/dist-packages/BeautifulSoup.pyc'> In [21]: BeautifulSoup.re --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-21-db67e3f66def> in <module>() ----> 1 BeautifulSoup.re NameError: name 'BeautifulSoup' is not defined
Python preloads some module at startup, and any imported modules that also import other modules will mean that other modules may appear in sys.modules, but are not available in your namespace:
In [1]: sys.modules["numpy"] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-1-88fb63c48e78> in <module>() ----> 1 sys.modules["numpy"] KeyError: 'numpy' In [2]: cat test.py import numpy In [3]: import test In [4]: sys.modules["numpy"] Out[4]: <module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'> In [5]: numpy.array --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-132169fc46d3> in <module>() ----> 1 numpy.array
source share