SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
Swipe
Python - Modules
A module allows you to logically organize your
Python code.
Grouping related code into a module makes the
code easier to understand and use.
A module is a Python object with arbitrarily
named attributes that you can bind and
reference.
Simply, a module is a file consisting of Python
code.
A module can define functions, classes and
variables. A module can also include runnable
code.
Python - Modules
The Python code for a module named aname
normally resides in a file named aname.py.
Here's an example of a simple module, support.py
Example
def print_func( par ):
print "Hello : ", par
return
You can use any Python source file as a module by
executing an import statement in some other
Python source file.
The import has the following syntax:-
import module1[, module2[,... moduleN]
When the interpreter encounters an import
statement, it imports the module if the module is
present in the search path.
A search path is a list of directories that the
interpreter searches before importing a module.
For example, to import the module support.py,
you need to put the following command at the top
of the script
The import Statement
#!/usr/bin/python
# Import module support
import support
# Now you can call defined function that module as
follows
support.print_func("Zara")
When the above code is executed, it produces the
following result
Hello : Zara
A module is loaded only once, regardless of the
number of times it is imported.
This prevents the module execution from
happening over and over again if multiple imports
occur.
Locating Modules
When you import a module, the Python
interpreter searches for the module in the
following sequences
The current directory.
If the module isn't found, Python then
searches each directory in the shell variable
PYTHONPATH.
If all else fails, Python checks the default path.
On UNIX, this default path is normally
/usr/local/lib/python/.
The module search path is stored in the system
module sys as the sys.path variable.
The sys.path variable contains the current
directory, PYTHONPATH, and the installation-
dependent default.
The PYTHONPATH Variable
The PYTHONPATH is an environment variable,
consisting of a list of directories.
The syntax of PYTHONPATH is the same as that of
the shell variable PATH.
Here is a typical PYTHONPATH from a Windows
system
set PYTHONPATH = c:python20lib;
And here is a typical PYTHONPATH from a UNIX
system
set PYTHONPATH = /usr/local/lib/python
Packages in Python
A package is a hierarchical file directory structure
that defines a single Python application
environment that consists of modules and
subpackages and sub-subpackages, and so on.
Consider a file Pots.py available in Phone
directory.
This file has following line of source code
#!/usr/bin/python
def Pots():
print "I'm Pots Phone"
Similar way, we have another two files having
different functions with the same name as above
Phone/Isdn.py file having function Isdn()
Phone/G3.py file having function G3()
Now, create one more file __init__.py in Phone
directory
Phone/__init__.py
To make all of your functions available when
you've imported Phone, you need to put explicit
import statements in __init__.py as follows
from Pots import Pots
from Isdn import Isdn
from G3 import G3
After you add these lines to __init__.py, you have
all of these classes available when you import the
Phone package.
When the above code is executed, it produces the
following result
#!/usr/bin/python
# Now import your Phone Package.
import Phone
Phone.Pots()
Phone.Isdn()
Phone.G3()
I'm Pots Phone
I'm 3G Phone
I'm ISDN Phone
Python - Object Oriented
Python - MySQL Database
Access
Stay Tuned with
Topics for next Post

More Related Content

What's hot (20)

PPT
Expression evaluation
JeeSa Sultana
 
PDF
Python Workshop
Saket Choudhary
 
PPTX
Decomposition in DBMS- check the lossless and lossy concept
sumanac5
 
PPTX
Stream classes in C++
Shyam Gupta
 
PDF
How to Install numpy, scipy, matplotlib, pandas and scikit-learn on Linux
Vinita Silaparasetty
 
PPTX
Iterarators and generators in python
Sarfaraz Ghanta
 
PDF
9 python data structure-2
Prof. Dr. K. Adisesha
 
ODP
Hands on Session on Python
Sumit Raj
 
PPTX
Introduction to numpy
Gaurav Aggarwal
 
PDF
C programming
Sivakumar Palani
 
PDF
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
DOCX
Résumer sur les fichier et les enregistrement
borhen boukthir
 
PPTX
Types of Statements in Python Programming Language
Explore Skilled
 
PPTX
Reduction & Handle Pruning
MdAshikJiddney
 
PDF
Python decision making
Learnbay Datascience
 
PDF
Python Collections Tutorial | Edureka
Edureka!
 
PPT
Python Pandas
Sunil OS
 
PPTX
C and C++ functions
kavitha muneeshwaran
 
DOCX
programming concept
Nehabhy
 
Expression evaluation
JeeSa Sultana
 
Python Workshop
Saket Choudhary
 
Decomposition in DBMS- check the lossless and lossy concept
sumanac5
 
Stream classes in C++
Shyam Gupta
 
How to Install numpy, scipy, matplotlib, pandas and scikit-learn on Linux
Vinita Silaparasetty
 
Iterarators and generators in python
Sarfaraz Ghanta
 
9 python data structure-2
Prof. Dr. K. Adisesha
 
Hands on Session on Python
Sumit Raj
 
Introduction to numpy
Gaurav Aggarwal
 
C programming
Sivakumar Palani
 
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
Résumer sur les fichier et les enregistrement
borhen boukthir
 
Types of Statements in Python Programming Language
Explore Skilled
 
Reduction & Handle Pruning
MdAshikJiddney
 
Python decision making
Learnbay Datascience
 
Python Collections Tutorial | Edureka
Edureka!
 
Python Pandas
Sunil OS
 
C and C++ functions
kavitha muneeshwaran
 
programming concept
Nehabhy
 

Similar to Python modules (20)

PPTX
Python module 3, b.tech 5th semester ppt
course5325
 
PDF
Using Python Libraries.pdf
SoumyadityaDey
 
PPT
Python modules
Shanmugapriya Dineshbabu
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
paurushsinhad
 
PDF
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
PPTX
Interesting Presentation on Python Modules and packages
arunavamukherjee9999
 
PPTX
Python for Beginners
DrRShaliniVISTAS
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPTX
Modules and its usage in python for beginners
Mohammad Usman
 
PPT
jb_Modules_in_Python.ppt
loliktry
 
PPT
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
paurushsinhad
 
PDF
Python Imports
Vladimir Korolev
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PPTX
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
PDF
Python. libraries. modules. and. all.pdf
prasenjitghosh1998
 
PPT
python_models_import_main_init_presentation.ppt
gouthamsaisurya555
 
PPTX
Session-1_Introduction to Python.pptx
WajidAliHashmi2
 
PDF
Modules and Packages in Python_Basics.pdf
RavindraTambe3
 
Python module 3, b.tech 5th semester ppt
course5325
 
Using Python Libraries.pdf
SoumyadityaDey
 
Python modules
Shanmugapriya Dineshbabu
 
Python Modules
Nitin Reddy Katkam
 
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
paurushsinhad
 
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
Interesting Presentation on Python Modules and packages
arunavamukherjee9999
 
Python for Beginners
DrRShaliniVISTAS
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Modules and its usage in python for beginners
Mohammad Usman
 
jb_Modules_in_Python.ppt
loliktry
 
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
paurushsinhad
 
Python Imports
Vladimir Korolev
 
Python libraries
Prof. Dr. K. Adisesha
 
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
Python. libraries. modules. and. all.pdf
prasenjitghosh1998
 
python_models_import_main_init_presentation.ppt
gouthamsaisurya555
 
Session-1_Introduction to Python.pptx
WajidAliHashmi2
 
Modules and Packages in Python_Basics.pdf
RavindraTambe3
 
Ad

More from Learnbay Datascience (20)

PDF
Top data science projects
Learnbay Datascience
 
PDF
Python my SQL - create table
Learnbay Datascience
 
PDF
Python my SQL - create database
Learnbay Datascience
 
PDF
Python my sql database connection
Learnbay Datascience
 
PDF
Python - mySOL
Learnbay Datascience
 
PDF
AI - Issues and Terminology
Learnbay Datascience
 
PDF
AI - Fuzzy Logic Systems
Learnbay Datascience
 
PDF
AI - working of an ns
Learnbay Datascience
 
PDF
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
PDF
AI - Robotics
Learnbay Datascience
 
PDF
Applications of expert system
Learnbay Datascience
 
PDF
Components of expert systems
Learnbay Datascience
 
PDF
Artificial intelligence - expert systems
Learnbay Datascience
 
PDF
AI - natural language processing
Learnbay Datascience
 
PDF
Ai popular search algorithms
Learnbay Datascience
 
PDF
AI - Agents & Environments
Learnbay Datascience
 
PDF
Artificial intelligence - research areas
Learnbay Datascience
 
PDF
Artificial intelligence composed
Learnbay Datascience
 
PDF
Artificial intelligence intelligent systems
Learnbay Datascience
 
PDF
Applications of ai
Learnbay Datascience
 
Top data science projects
Learnbay Datascience
 
Python my SQL - create table
Learnbay Datascience
 
Python my SQL - create database
Learnbay Datascience
 
Python my sql database connection
Learnbay Datascience
 
Python - mySOL
Learnbay Datascience
 
AI - Issues and Terminology
Learnbay Datascience
 
AI - Fuzzy Logic Systems
Learnbay Datascience
 
AI - working of an ns
Learnbay Datascience
 
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
AI - Robotics
Learnbay Datascience
 
Applications of expert system
Learnbay Datascience
 
Components of expert systems
Learnbay Datascience
 
Artificial intelligence - expert systems
Learnbay Datascience
 
AI - natural language processing
Learnbay Datascience
 
Ai popular search algorithms
Learnbay Datascience
 
AI - Agents & Environments
Learnbay Datascience
 
Artificial intelligence - research areas
Learnbay Datascience
 
Artificial intelligence composed
Learnbay Datascience
 
Artificial intelligence intelligent systems
Learnbay Datascience
 
Applications of ai
Learnbay Datascience
 
Ad

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 

Python modules

  • 2. A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. Python - Modules
  • 3. The Python code for a module named aname normally resides in a file named aname.py. Here's an example of a simple module, support.py Example def print_func( par ): print "Hello : ", par return
  • 4. You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax:- import module1[, module2[,... moduleN] When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script The import Statement
  • 5. #!/usr/bin/python # Import module support import support # Now you can call defined function that module as follows support.print_func("Zara") When the above code is executed, it produces the following result Hello : Zara A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur.
  • 6. Locating Modules When you import a module, the Python interpreter searches for the module in the following sequences The current directory. If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH. If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/. The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation- dependent default.
  • 7. The PYTHONPATH Variable The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH. Here is a typical PYTHONPATH from a Windows system set PYTHONPATH = c:python20lib; And here is a typical PYTHONPATH from a UNIX system set PYTHONPATH = /usr/local/lib/python
  • 8. Packages in Python A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on. Consider a file Pots.py available in Phone directory. This file has following line of source code #!/usr/bin/python def Pots(): print "I'm Pots Phone"
  • 9. Similar way, we have another two files having different functions with the same name as above Phone/Isdn.py file having function Isdn() Phone/G3.py file having function G3() Now, create one more file __init__.py in Phone directory Phone/__init__.py To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows from Pots import Pots from Isdn import Isdn from G3 import G3
  • 10. After you add these lines to __init__.py, you have all of these classes available when you import the Phone package. When the above code is executed, it produces the following result #!/usr/bin/python # Now import your Phone Package. import Phone Phone.Pots() Phone.Isdn() Phone.G3() I'm Pots Phone I'm 3G Phone I'm ISDN Phone
  • 11. Python - Object Oriented Python - MySQL Database Access Stay Tuned with Topics for next Post