8

Python 3.6.5

Using this answer as a guide, I attempted to see whether some modules, such as math were imported.

But Python tells me they are all imported when they are not.

>>> import sys
>>> 'math' in sys.modules
True
>>> 'math' not in sys.modules
False
>>> math.pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> import math
>>> 'math' in sys.modules
True
>>> math.pi
3.141592653589793
EmmaV
  • 183
  • 3
  • Does this answer your question? [Where are math.py and sys.py?](https://stackoverflow.com/questions/18857355/where-are-math-py-and-sys-py) – CreepyRaccoon Nov 26 '22 at 16:05
  • If `math` is a builtin, why won't it work unless I import it? – EmmaV Nov 26 '22 at 16:09
  • 1
    Because even builtin modules need to be imported. That's how the language works – Pranav Hosangadi Nov 26 '22 at 16:11
  • @PranavHosangadi Not necessarily. I don't have Python 3.6 available to test, but in Python 3.9 at least, `math` is *not* a built-in module like (say)`os`. – chepner Nov 26 '22 at 16:19
  • "_But Python tells me they are all imported_" No, Python does not tell you that. `sys.modules` is a dictionary that maps module names to modules which have already been **loaded** (not imported in the current scope). Also note this is only true for the interactive interpreter. If you run `python -c "import sys; print('math' in sys.modules)"`, you will see that it prints `False`. – wovano Nov 26 '22 at 16:36
  • PS: `math` is not a built-in, it's just part of the [Standard Library](https://docs.python.org/3/library/index.html) – wovano Nov 26 '22 at 16:39
  • @chepner, even `os` is not a built-in module. AFAIK, there are no built-in modules at all. There all built-in [Functions](https://docs.python.org/3/library/functions.html), [Constants](https://docs.python.org/3/library/constants.html), [Types](https://docs.python.org/3/library/stdtypes.html) and [Exceptions](https://docs.python.org/3/library/exceptions.html). – wovano Nov 26 '22 at 16:41
  • @wovano There *are* built-in modules. These modules are compiled into the interpreter. Try this: `import sys; print(sys.builtin_module_names)`. You'll see `math` there. – S.B Nov 28 '22 at 18:04
  • @S.B, interesting, didn't know that. Although the concept seems a bit different though. At least the built-in Functions, Constants, Types and Exceptions are part of the Python language and are always available. The built-in modules are interpreter-specific and are not readily available (they must be imported). – wovano Nov 28 '22 at 19:03

1 Answers1

9

to explain this, let's define this function:

def import_math():
    import math

import_math()

the above function will import the module math, but only in its local scope, anyone that tries to reference math outside of it will get a name error, because math is not defined in the global scope.

any module that is imported is saved into sys.modules so a call to check

import_math()
print("math" in sys.modules)

will print True, because sys.modules caches any module that is loaded anywhere, whether or not it was available in the global scope, a very simple way to define math in the global scope would then to

import_math()
math = sys.modules["math"]

which will convert it from being only in sys.modules to being in the global scope, this is just equivalent to

import math

which defines a variable math in the global scope that points to the module math.

now if you want to see whether "math" exists in the global scope is to check if it is in the global scope directly.

print("math" in globals())
print("math" in locals())

which will print false if "math" wasn't imported into the global or local scope and is therefore inaccessable.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23