Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sys.modules contains a module which is not yet imported

I'm trying to understand the difference between a loaded module vs. an imported module, if there is any.

I'm working in Python 2.7.3, and am just running Python from the command line.

If I execute:

import sys
sys.modules

I get a list which includes os, for example. The documentation says that sys.modules is a list of "loaded" modules. However, if I try to run something like os.environ, I get a NameError which tells me that os is not defined. However, if I then run import os, this resolves the issue. Can anyone explain why os exists in sys.modules prior to me actually importing the module?

like image 246
Justin Avatar asked Jul 28 '12 02:07

Justin


People also ask

How do you check if a module is already imported in Python?

Use: if "sys" not in dir(): print("sys not imported!")

What is sys module in Python?

The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.

Do you need to import sys in Python?

Like all the other modules, the sys module has to be imported with the import statement, i.e. The sys module provides information about constants, functions and methods of the Python interpreter.

Is sys a built in Python library?

One particular module deserves some attention: sys, which is built into every Python interpreter. However, if I start the python interpreter and type, for example, sys. path , I get a NameError: name sys is not defined .


1 Answers

The difference between a module being imported and being loaded is what is placed into your current module's namespace. A module will only get loaded once (in ordinary situations), but can be imported many times, from many different places. A loaded module may not be accessible in a given namespace, if it hasn't been imported there. For instance, you can load a module without importing it under its name using the from module import name syntax (you'll be able to access the specified name, but not the module itself).

You're seeing the os module in the sys.modules dictionary because it's used internally by the python interpreter, and so it is always loaded at startup. You can't access it using the name "os" though, because it isn't automatically imported into your namespace.

However, you can bypass the normal import mechanisms in a few ways. For instance, try this:

import sys
os = sys.modules["os"]

You'll now be able to access the os module just as if you had done import os.

That's because that code is exactly what an import statement does when you request a module that has already been loaded. However, if you try the code above with a module that isn't loaded yet, it won't work (you'll get a key error from the sys.modules dictionary). The import statement loads new modules in addition to adding them to the current namespace. While you can manually load modules and further work around the regular import system, there's rarely a good reason to do so.

like image 190
Blckknght Avatar answered Oct 07 '22 12:10

Blckknght