0% found this document useful (0 votes)
43 views19 pages

Python Modules

The document discusses modules and packages in Python. Modules allow splitting code into multiple files and importing functionality. Packages group related modules and use an __init__.py file to define import behavior. Specific functions or everything can be imported from a module using different import statements.

Uploaded by

justchilltanuj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
43 views19 pages

Python Modules

The document discusses modules and packages in Python. Modules allow splitting code into multiple files and importing functionality. Packages group related modules and use an __init__.py file to define import behavior. Specific functions or everything can be imported from a module using different import statements.

Uploaded by

justchilltanuj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

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:

• Import os, sys, getopt, ftplib


• This is same as
• Import os
• Import sys
• Import getopt
• Import ftplib
• One can also import the ftplib module using an alias as

• Import ftplib as ftp


• Using aliases is a way to test a new version of a module without upsetting
the original
• For example , that you have two modules, where the stable version is
mylib and the development version is newmylib. You could change
references to your test scripts to

• Import newmylib as mylib


Importing specific module entities
• To import specific functions and objects from a given
module,use the from…import statement
• For example to import the objects foo and bar from the module
foobar, use the following statement

• from foobar import foo, bar


• The major difference between the preceding statement and the import
…. Statement is that the objects that you import become available within
the current namespaces, so you no longer have to specify them
explicitly. For example the following is now valid

• From Foobar import foo, bar


• foo()
• bar()
• To import everything from a given module into the current
namespace
• from Foobar import*
Packages
• Packages in python allow a set of modules to be
grouped under a common package
• A package is defined by creating a directory with the same name as the
package and the creating the file _init_.py within that directory
• This file contains the necessary instructions to the python interpreter to
allow importing of modules and module groups within the package.

• 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)

Print add (1,1)

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

You might also like