SlideShare a Scribd company logo
2
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

PPTX
Java Data Types
PDF
Constants, Variables and Data Types in Java
PPTX
Constructor in java
PPT
9. Input Output in java
PPTX
Control Statements in Java
PPTX
Thread priorities
PPTX
Packages In Python Tutorial
PDF
Python Modules
Java Data Types
Constants, Variables and Data Types in Java
Constructor in java
9. Input Output in java
Control Statements in Java
Thread priorities
Packages In Python Tutorial
Python Modules

What's hot (20)

PPTX
Java constructors
PPTX
Type casting in java
PDF
Python Programming
PPTX
Data types in java
PPTX
Chapter 07 inheritance
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Functions in python slide share
PPTX
OOPS In JAVA.pptx
PPTX
Python Functions
PPTX
Java packages
PPT
Thread model in java
PPTX
Sql queries presentation
PDF
Python programming : Strings
PPTX
6-Python-Recursion PPT.pptx
PPTX
Python Exception Handling
PDF
Datatypes in python
PDF
Syntax directed translation
PPTX
constructors in java ppt
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
PDF
Java constructors
Type casting in java
Python Programming
Data types in java
Chapter 07 inheritance
Basic Concepts of OOPs (Object Oriented Programming in Java)
Functions in python slide share
OOPS In JAVA.pptx
Python Functions
Java packages
Thread model in java
Sql queries presentation
Python programming : Strings
6-Python-Recursion PPT.pptx
Python Exception Handling
Datatypes in python
Syntax directed translation
constructors in java ppt
CLASS OBJECT AND INHERITANCE IN PYTHON
Ad

Similar to Python modules (20)

PPTX
Python module 3, b.tech 5th semester ppt
PDF
Using Python Libraries.pdf
PPT
Python modules
ODP
Python Modules
PPTX
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
PDF
Unit-2 Introduction of Modules and Packages.pdf
PPTX
Interesting Presentation on Python Modules and packages
PPTX
Python for Beginners
PPTX
Chapter 03 python libraries
PPTX
Modules and its usage in python for beginners
PPT
jb_Modules_in_Python.ppt
PPT
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
PDF
Python Imports
PDF
Python libraries
PPTX
Modules and Packages in Python Programming Language.pptx
PDF
Python. libraries. modules. and. all.pdf
PPT
python_models_import_main_init_presentation.ppt
PPTX
Session-1_Introduction to Python.pptx
PDF
Modules and Packages in Python_Basics.pdf
Python module 3, b.tech 5th semester ppt
Using Python Libraries.pdf
Python modules
Python Modules
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
Unit-2 Introduction of Modules and Packages.pdf
Interesting Presentation on Python Modules and packages
Python for Beginners
Chapter 03 python libraries
Modules and its usage in python for beginners
jb_Modules_in_Python.ppt
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
Python Imports
Python libraries
Modules and Packages in Python Programming Language.pptx
Python. libraries. modules. and. all.pdf
python_models_import_main_init_presentation.ppt
Session-1_Introduction to Python.pptx
Modules and Packages in Python_Basics.pdf
Ad

More from Learnbay Datascience (20)

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

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
English Language Teaching from Post-.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Module 3: Health Systems Tutorial Slides S2 2025
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O7-L3 Supply Chain Operations - ICLT Program
How to Manage Starshipit in Odoo 18 - Odoo Slides
English Language Teaching from Post-.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Open folder Downloads.pdf yes yes ges yes
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Open Quiz Monsoon Mind Game Prelims.pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Module 3: Health Systems Tutorial Slides S2 2025
UPPER GASTRO INTESTINAL DISORDER.docx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

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