Python Import Functionality

Published: 30 August 2018
on channel: Deep Hobbying
160
3

When the Python interpreter reads a source file, it executes all of the code found in it. This is called the direct execution as we are running codes directly from the source file. However, we could also run a python file from a different module or file by using the import functionality of python.

Let us assume we have two different modules: module1 & module2. In case we import module1 into module2, it just execute all the code present in the module1 and then move to the next line of code in module2, which does not have any code present in module1. Hence, no class or function, which we have defined in the module1, will be available for module2. If we wish to access them from module2, we need import them separately using below line of code.

from module import class_name, function_name

We could also assign a different name to all the classes and functions that we have imported from a different module, using 'as' keyword, as shown below.

from module import class_name as name1, function_name as name2
Every module in python has a special attribute called _name__. The value of __name_ attribute is set to '__main__' when module run as main program. Otherwise, the value of _name_ is set to contain the name of the module.


You could find this tutorial’s code in the following links:

https://github.com/DeepHobbying/Basic...

https://github.com/DeepHobbying/Basic...


On this page of the site you can watch the video online Python Import Functionality with a duration of hours minute second in good quality, which was uploaded by the user Deep Hobbying 30 August 2018, share the link with friends and acquaintances, this video has already been watched 160 times on youtube and it was liked by 3 viewers. Enjoy your viewing!