The contents of a module are made available to the caller using the import and from statements when
a module is access.
Using these two statements Python performs the following steps first the first step is to search for
the module.
When a module is being imported into a file python will go through a search algorithm based on the search
but and the search part is the list of directories available through says Dot.
But and so this is the standard library module and part is the attribute of this module says Dot bar
and says Dot but contained a list the directories that determine the interpreters search box for modules
so it gives the order in which Python searches for modules in order to import the says Dot part variable
is initialized from the following location so the first is the directive from which the input script
was done or the current directly the interpreter is being run interactively so this means when you are
running the program this entry is the directory can containing your program's script file when you're
working interactively.
This entry is the directory in which you're working that is the current working directory next the CIS
dot but variable is initialized from the list of directories contained in the python part environment
variable.
The format for Python parties or is dependent but it should be similar to the part environment variable
in your system and Python part is simply set to a list of user defined and platform specific names of
directories that contain Python code files next.
The easiest but variable is also initialized by installation dependent list of directories configured
at the time.
Python is installed so Python merges the home directory of the script that is being executed and any
python by directories and the standard library directories and the result is a list of directory name
strings that Python searches on each import of a new file.
Now let us inspect the says Dot but variable from the command prompt in order to access the app attribute
part of the source module.
We will have to first import this module.
So let us give the import statement.
Import cease and then let's print the CIS Dot.
But variable.
So here we have the list of directives that Python is going to search when it searches in order to import
a module.
And the first entry as you can see here is an empty string which directs Python to search modules in
the current working directory first say for example say we have found the module B in our example in
one of the directories listed in these says Dot but variable.
So let's go to the next step that Python needs to perform in order to import a module.
So the second step is to translate all these statements in the module into byte code.
So what is a byte code a bite code is a lower level language than Python and Python translate modules
into byte court and then it's what machines or the interpreter.
Execute these modules by interpreting their bite code and Python stores the byte code of the important
modules inspect should captured files with the extension b y c so we have found the module for importing.
Next we have translated All the python code in the module into byte code in the second step.
Now the third step is to create a module object for the module.
And initially it has an empty namespace module object import operations load module files into module
object in memory.
The module object contained the different attributes on variables that are accessible from outside the
module so the module object contained the different attributes the variables that are accessible from
the outside world.
Odd outside of the module when the module object is created in the memory it is empty.
Let us see when the module object get populated shortly and the last step of the import operation is
to execute the bite code of the module.
So all these statements in a module are executed and any assignment statement or any assignment made
to the names during this step generate attribute of the resulting module object.
So your module has been executed and now the module object needs to be populated
so in the last step of the import statement all of the code Python code in the module B gets executed
and the module object for module b get updated with the module name B and also the import statement
when it imports a module it creates a reference to that module in the local namespace from the calling
program.
So the local namespace of module e is updated with a reference to the module B when we import a module
we're going to refer to the whole module object.
And what about the attributes defined in the global scope we can fetch these attributes using the dot
notation like this.
So if you want to call any of the attributes here you can use the dot notation.
So here we are going to say B the module name dot the attribute name B dot Zi or if you want to call
a function.
This is how you can remember the variables or the attribute that are defined or part of the global scope
remain in the modules private table and they're not meaningful in the local context.
That means we cannot directly access the attribute Zi or add the function Carl directly in the module
e in order to access these two variables we have to use d dot notation so the names of the attributes
in the module must be prefixed by the module name we can import several modules using a single import
statement and this is the syntax all of the module names are separated by comma next we can give alternate
names to the modules to be imported and the import statement goes like this say for example import B
as Sam.
So in the calling program whenever you want to refer to module B hereafter you should use sample as
the module name and module name B is not recognizable or recognized in the module e so if you want to
fetch any of the attributes defined in module B in module eight from module e.
This is how you refer sample dot Zi.
So the object can be referenced by pra fixing the new module name or the alternate name.
So in order to fetch the attribute it should be prefixed by the alternate name.
So here I have created two modules a dot B Y and b dot B Y.
So we have discussed before that in order to define a module we can use a text editor to type some Python
code into the text file and save it with a dot B Y extension.
And so I have two files here and b dot B Y is the module that is going to be imported into the module
a.
So in this module B I have to attribute in the global scope one as the attribute C and the other is
the function can we want to call these attributes the attribute Z and the function card from the module.
E here.
So in order to do that we have put an import statement as a first statement in this module and then
we have called the function call and after importing the module I also have a print function here.
We have a print function that prints the content of the directory function the directory function is
is used to find out the names defined in a given namespace after importing the module B we will see
if the module B is included in the local namespace of module a no let's save both these files and then
Randy file a from the command prompt.
Now let execute the file a dot B Y from the command prompt
so diff module B has been accessed from the module a and here are the print functions that got executed
that are present in module B and then we also have placed a print function in module a in order to display
the contents of the directory function and as you can see as you can see the directory function has
return all the properties and the method even building properties which are D4 for all objects such
as double underscore Doc double underscore name and so on and these special names are found in most
or all name spaces and we're interested in this entry in the list the import statement as created a
reference to module B in the local namespace of module 8 and the final print function has printed this
line using the dot notation we have access the attribute Zi defined in module B.
Now we will see different forms of the import statement the first one is the from import statement and
this is the syntax from B that is the module name import the required attribute C Zi comma care and
so on.
So the forearm statement allows individual attributes from the module to be imported directly into the
calling program or body calling programs namespace and also the module object.
Now get updated with the attributes in the namespace Zi and can so we can now use the attributes Zi
and Carl directly in our program without using the dot notation or without referring to the module name.
So when we put these from import statement in this file a dot B Y we can directly access the attribute
Z without using the dot notation or without using the module name as a prefix.
Say for example D from import statement has imported only one attribute that is Z from module B.
In that case we can only refer to this attribute in the file a dot B Y other attribute like function
Cal cannot be accessed in the file a dot B Y because we have not imported this particular attribute
during the import and the function Cal that has been defined in module B cannot be accessed in the file
a dot B why now because this has not been copied to the local namespace of this file a dot B write another
form of the import statement is this from B import start and this statement copies all of the names
assigned a d global scope of the referenced module so after execution of the code in the module B the
module object get updated with all of the attributes in the global scope also the calling programs namespace
gets updated with all the attributes in the module object namespace so the namespace of defined Ada
or b way of the calling program get updated with these attributes and we can directly access these attributes
in the file Ada or b y without using the dot notation.
So using this statement we can directly access the attributes that are part of the global namespace
of module B.