Hello welcome to this tutorial on packages and modules
left to understand packages and modules using some examples.
Say for example have created a directory in my system.
Called basic math and this directory has four files and each of these files have an extension.
Dot B white indicating these files contain python script or Python code and each of these files is used
to perform a specific task.
So for example the first file addition dot B Y is used to perform additions on some input variables
or input parameters.
Similarly we have the other file subtraction multiplication and area performing different tasks so instead
of having the code the entire code for different tasks in a single file.
We have split the code into small parts and each of these but is called a module
and a module in Python is a file containing Python definitions and statements in a module and defined
functions classes and variables in order to define a module.
We need to simply use a text editor to type some Python code and save it with the dot B Y extension
and any such file is automatically considered a Python module.
And the module name is molded out of a file name by removing the dot B Y suffix.
So the file name here is addition dot B Y and the module name is Ed. So if you remove the extension
part b y it becomes the module name so a module in Python is a file containing Python definitions and
statements and the modules are built separately and more or less independently and any file which has
the extension dot B Y and counters of proper Python code is considered a Python module next we will
talk about packages in the previous tutorials.
We were introduced to Python packages.
We have downloaded packages from the Python packaging index and a python package is a collection of
modules and we can build applications by putting all of these modules together and a package is basically
a directory but Python modules and a file with the name underscored underscored in it underscored underscore
dot be white.
This means that every directory inside of the Python part which contains a file name double underscored
in it.
Dot be y will be treated as a package by Python.
A package is basically a directory but Python modules and a file with the name double underscore in
a dot B white.
This means that every directory inside of the python but which contains a file named w underscored in
it will be treated as a package by Python so basic underscore might.
In our example is a package and here we have modules in this package.
And what does this inert file contain.
This the inert file can be left empty but we generally place the initialization code for that package
in this file.
We will talk about the initialization code in the upcoming topics but for now we must remember that
a directory must contain a file named double underscore in it.
In order for Python to consider it as a package
Python package can have sub packages and modules in order to further organize the files and packages
can contain nested sub packages to arbitrary length.
So in this example here we have two sub packages.
And in order for Python to treat these two folders or directories as sub packages we have placed the
double underscored in it Phi in these sub packages as well.
So Python packages a collection of modules and these modules co-operate or work together in order to
implement a task next.
Python packages can have sub packages to arbitrary depth.
Next a directory must contain the doublet underscored in it file in order for Python to consider it
as a package.
So we have understood what modules and packages are.
Now let's go to the next example.
So here I have defined two files a dot B Y and b dot B Y and in this module B I have declared the variable
Z which is a global variable and then a function can and as you can see we have not put any function
calls to this particular function.
In the module B next I want to access the variables and the functions that are defined in module B from
the module a in the global scope or the global namespace of the module b we have the attribute Z which
is pointing to the object one and then we also have the function can which is pointing to the function
object and these two attributes in the global namespace are accessible from anywhere in the in the file.
B Not coming to the local scope we have two variables that are defined in the local scope and because
they are defined within the function body of the function.
Carl they are only accessible within this particular function.
Now I want to access the variables and functions that are defined in the module B from module 8 and
Python provide the import.
And from statements in order to access the content the content of a module.
So in order to access the content defined in the module B we will have to put an import statement in
Module 8 so we will have the import statement and we are going to import the module be the python import
statement gives the module a access to everything that is defined in the global scope or namespace of
module B.
So then we import module B in the file a dot B why this import statement gives access to the attribute
in the global scope of module B.
Remember we do not have a function call to the function that is defined in module B can so so we have
access to the function call that is defined in the global namespace of module b and this function can
be called from the module e using the dot notation so using the dot notation B dark Cal we can make
a function call parse the arguments when Python encounters an import statement in a file it performs
a sequence of steps.
Now let's see the steps involved when importing a module.