This material on importing packages in the last Victorian we have learned how to create modules and
import these modules into external programmes in this tutorial.
We will talk how to create packages and import the packages.
We have already discussed that a package is basically a directory with Python files and a file with
the name double underscore in it.
This means that every directory inside of the Python part which contains a file named double underscored
in it that b y will be treated as a package by Python and it is possible to put several modules in to
a package.
A package is important like a normal module so we can use the import statement and from statements that
we have discussed in the previous story.
And importing the package is conceptually equivalent to importing the packages double underscored in
a file as a module and when you import or package or a module in a package the double underscored in
nut file is invoked and this file can be left empty.
But we generally place the initialization code for a package.
Say for example we can make sure that some modules are imported or some values are set in the input
file and also only objects that are declared in the import packages in that file are accessible.
In the external programmes or the importer so in the input fi that is created in the basic map package
we are importing three modules.
The first is using the import statement where importing the module addition and then using the from
statement we are importing the function as you be and again using the from statement we are importing
all the available functions in the module multiplication.
So here we have used an asterix in order to import all the functions are all the attributes in the global
namespace of the module multiplication.
So this is the initialization caught in the net fi so I now have my package ready with the different
with the different modules and also the input file.
I'm not going to import the package basic mat from the command prompt so for that I have started the
python interpreted.
You can see the python prompt and before importing the basic math package I'm going to see the attribute
that are available or that are accessible in the current working space.
And for that you can use the d a r function.
So these are the different attributes that are available in the current working space before doing anything
in the Python interpreter.
Now let's go ahead and import the package basic math.
So we have imported the package.
Basic math.
Let us now again see the attributes available or accessible in the current working space.
So as you can see basic math after the import statement has been executed basic math has been added
to this list.
So this package has been added to the list of attributes that add accessible from this space.
So let us try to access the modules and the functions defined in these modules from here.
So we only have the whole package basic math accessible to us and all the modules or the functions need
to be accessed using the dot notation we can't access them directly from here.
So you use the dot notation in order to access these functions are the modules inside the package edition
is one of the modules available in the package and add is a function to pass arguments to this function.
So.
So we were able to access the function and by importing the package basic math by importing the entire
package.
Basic math we were able to access the different functions available in the modules so let's axis the
module multiplication.
Now we will access one of the functions defined in the module multiplication.
So this time we have not used the module but we have directly access the function defined in the module.
That's because we have used the from statement different statement allows individual objects from a
module to be imported directly into the into the calling program.
So the France statement allows individual objects from a module to be imported directly into the culling
programs namespace.
So we have access to this function modeled from the package.
Basic math so that is it is in we have not included the module name multiplication while accessing this
function might we have not used the module name before accessing the function mind that is defined that
is defined in this module multiplication.
That is because the function modeled is added to the namespace of the package.
Basic math.
In order to understand this we use the function D I add and past basic math as an argument to this function.
So as you can see these are the list of list of attributes that are available.
Odd accessible to the.
Package.
Basic math.
And using the import statement we have imported the entire module addition and using the from statement
we have imported the functions mind and sub in dude.
The namespace of the package.
Basic math.
So you can directly access these two functions from the package.
Basic math.
And in my system I have created a directory.
Basic math which contains all these files.
The init file multiplication addition and subtraction.
And these are these modules available in the package.
Basic math.
Apart from the Python script files we also have a folder by cache folder in the package basic math.
Let's go into this folder.
And Python stores the byte code of the imported modules in special cashiered files with the same module
name but using the extension dart.
B why C in the bike cache folder and with this we come to the end of the topic packages and modules.