Python Modules
Python Modules
• As programs get larger and larger, it makes more and more sense to
split a large file into a number of smaller files
• This keeps the code nice and tight and it also makes modification of
the code easier, especially if the project is being developed
simultaneously by a number of different people
• Modules and packages are two constructs used in Python to organize
larger programs
• In Python you have the ability to split up a program into a number of
individual files called modules
• Modules can then be imported into your existing script, providing you
with the access to functions , objects and classes you have defined
within each module
Importing a module
• In python you import the module into script- you are
importing information about how to use the
functions, objects and classes within the module ,
rather than importing the different entities
themselves into the script
Importing an entire Module
• To import, that is , to give yourself access to the functions, objects
and classes within a given module, you use the import statement
• Import ftplib
• The import statement in Python does three things
• 1. It creates a new namespace to hold all the objects defined within
the given module
• 2. It executes the code in the module within the confines of the given
namespace
• It creates a name within the caller that refers to the modules
namespace
• In the preceding example
• 1. A new namespace ftplib was created
• 2.The ftplib.py file in the python library directory was found and
executed within the ftplib namespace
• 3. and then an object called ftplib was created within the namespace of
the current module
• You can also import multiple modules simultaneously by separating
each module name by comma in the import statement:
• Net/
• _init_.py
• Mediaweb/
• _init_.py
• Weather.py
• Weblog.py
• Systemlog.py
• Now, from within python you can import modules from this structure in a
number of ways
• Import Net.MediaWeb.Weather
• If you wish to use the report() function of the module then we must
explicitly specify
• Import Net.MediaWeb.Weather.report()
• Or we can import using the following statement
• from Net.MediaWeb.Weather import report
_init_.py
• When importing modules, the code in _init_.py is executed in order to
perform any package specific initialisations
• For example , importing Net.MediaWeb.Weather would execute
• Net/_init_.py and
• Net/MediaWeb/_init_.py
Creating A Module
• Creating a new module in python is as easy as writing the original
script
• Any file or script that you have created in python is immediately
available as a module without making any modifications to the code .
• In fact apart from copying the file into a standard location to make it
readily available,there is nothing else you need to do
• For example , imagine you have to create a small script called mymath.py
• def add (a,b):
• Return(a+b)
You can import mymath.py and make use of the add function
Import mymath
print mymath.add(2,2)
Or
From mymath.py import add