Wednesday, February 23, 2022

Using the import statement

The import statement is a common way to import a module. The next code snippet is an example of using an import statement:

import math

The import statement is responsible for two operations: first, it searches for the module given after the import keyword, and then it binds the results of that search to a variable name (which is the same as the module name) in the local scope of the execution. In the next two subsections, we will discuss how import works and also how to import specific elements from a module or a package.

Learning how import works

Next, we need to understand how the import statement works. First, we need to remind ourselves that all global variables and functions are added to the global namespace by the Python interpreter at the start of an execution. To illustrate the concept, we can write a small Python program to spit out of the contents of the globals namespace, as shown next:

# globalmain.py with globals() function

def print_globals():

print (globals())

def hello():

print ("Hello")

if __name__ == "__main__":

print_globals()

This program has two functions: print_globals and hello. The print_globals function will spit out the contents of the global namespace. The hello function will not be executed and is added here to show its reference in the console output of the global namespace. The console output after executing this Python code will be similar to the following:

{

"__name__":"__main__",

"__doc__":"None",

"__package__":"None",

"__loader__":"<_frozen_importlib_external.\

SourceFileLoader object at 0x101670208>",

"__spec__":"None",

"__annotations__":{

},

"__builtins__":"<module 'builtins' (built-in)>",

"__file__":"/ PythonForGeeks/source_code/chapter2/\

modules/globalmain.py",

"__cached__":"None",

"print_globals":"<function print_globals at \

0x1016c4378>",

"hello":"<function hello at 0x1016c4400>"

}

The key points to be noticed in this console output are as follows:

• The __name__ variable is set to the __main__ value. This will be discussed in more detail in the Loading and initializing a module post.

• The __file__ variable is set to the file path of the main module here.

• A reference to each function is added at the end.

If we add print(globals()) to our calcmain1.py script, the console output after adding this statement will be similar to the following:

{

"__name__":"__main__",

"__doc__":"None",

"__package__":"None",

"__loader__":"<_frozen_importlib_external.\

SourceFileLoader object at 0x100de1208>",

"__spec__":"None",

"__annotations__":{},

"__builtins__":"<module 'builtins' (built-in)>",

"__file__":"/PythonForGeeks/source_code/chapter2/module1/

main.py",

"__cached__":"None",

"mycalculator":"<module 'mycalculator' from \

'/PythonForGeeks/source_code/chapter2/modules/\

mycalculator.py'>",

"myrandom":"<module 'myrandom' from '/PythonForGeeks/source_

code/chapter2/modules/myrandom.py'>",

"my_main":"<function my_main at 0x100e351e0>"

}

An important point to note is that there are two additional variables (mycalculator and myrandom) added to the global namespace corresponding to each import statement used to import these modules. Every time we import a library, a variable with the same name is created, which holds a reference to the module just like a variable for the global functions (my_main in this case).

We will see, in other approaches of importing modules, that we can explicitly define some of these variables for each module. The import statement does this automatically for us.

Share:

0 comments:

Post a Comment