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
Functions in Python
PDF
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
PPTX
Python OOPs
PPT
Python ppt
DOCX
Seminar report On Python
PPSX
Modules and packages in python
PDF
Intro to Python for Non-Programmers
PPTX
Presentation on python
Functions in Python
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
Python OOPs
Python ppt
Seminar report On Python
Modules and packages in python
Intro to Python for Non-Programmers
Presentation on python

What's hot (20)

PPTX
Python Programming Language
PDF
Overview of python 2019
PDF
PDF
Python libraries
PDF
Python made easy
PPTX
Python
PDF
Python course syllabus
PPTX
Python Functions
PDF
ch 2. Python module
PPTX
Python 3 Programming Language
PPTX
Python Tutorial Part 1
PPTX
Beginning Python Programming
PDF
Python - object oriented
PPTX
Java swing
PPTX
Introduction to Basics of Python
PPTX
Python ppt
PDF
Introduction to python programming
PPTX
Basics of python
PDF
Unix system programming
Python Programming Language
Overview of python 2019
Python libraries
Python made easy
Python
Python course syllabus
Python Functions
ch 2. Python module
Python 3 Programming Language
Python Tutorial Part 1
Beginning Python Programming
Python - object oriented
Java swing
Introduction to Basics of Python
Python ppt
Introduction to python programming
Basics of python
Unix system programming
Ad

Similar to Python modules (20)

PPTX
Chapter 03 python libraries
PPTX
Using python libraries.pptx , easy ppt to study class 12
PPTX
Python Session - 5
PPTX
Python module 3, b.tech 5th semester ppt
PPT
Python modules
PPT
python_models_import_main_init_presentation.ppt
PPTX
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
PDF
Using Python Libraries.pdf
PPT
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
PPT
jb_Modules_in_Python.ppt
PPTX
3-Tut2_Interfacing_Sensors_RPioT.pptx good reference
PDF
Python. libraries. modules. and. all.pdf
DOCX
Python Course.docx
PDF
PYTHON Programming compilation COMMAND LINE TOOLS.pdf
PPTX
Modules,Packages,Librarfrserrrrrrrrrrrries.pptx
PDF
Python_AdvancedUnit - 3.pdf about the python
PPTX
FILE AND OBJECT<,ITS PROPERTIES IN PYTHON
PPTX
Python Tutorial Part 2
PPTX
Python Modules, executing modules as script.pptx
Chapter 03 python libraries
Using python libraries.pptx , easy ppt to study class 12
Python Session - 5
Python module 3, b.tech 5th semester ppt
Python modules
python_models_import_main_init_presentation.ppt
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
Using Python Libraries.pdf
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
jb_Modules_in_Python.ppt
3-Tut2_Interfacing_Sensors_RPioT.pptx good reference
Python. libraries. modules. and. all.pdf
Python Course.docx
PYTHON Programming compilation COMMAND LINE TOOLS.pdf
Modules,Packages,Librarfrserrrrrrrrrrrries.pptx
Python_AdvancedUnit - 3.pdf about the python
FILE AND OBJECT<,ITS PROPERTIES IN PYTHON
Python Tutorial Part 2
Python Modules, executing modules as script.pptx
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
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Presentation on HIE in infants and its manifestations
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O7-L3 Supply Chain Operations - ICLT Program
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
Presentation on HIE in infants and its manifestations

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