SlideShare a Scribd company logo
1 of 8
Python Modules
        Python 3

  Katkam Nitin Reddy
    k.nitin.r [at] gmail.com
    www.nitinkatkam.com
Python Modules

●   When you “import” a function, for instance,
    you are essentially using a module
●   A module is essentially a Python file with a .py
    extension
Using Modules

●   You can import a module using
    import <module-name>
    and access the contents using
    <module-name>.<entity-name>
●   You can also access entities directly
    from <module-name> import <entity-name>
    OR
    import <module-name>
    <alias>=<module-name>.<entity-name>
Creating Modules

●   Example: Create the following and save it as
    example.py
    def add(x,y):
     return x+y
●   Now, in another Python file, call the add()
    function using the following:
    import example
    print(example.add(1,2))
Creating Modules (Contd)

●   Modules have the __name__ variable set to the
    module name
●   When a Python file is called as a script, the
    __name__ is set to “__main__”. This lets you
    create modules that can also be executed as
    scripts using the following:
    def add(x,y):
      return x+y
    if __name__ == “__main__”:
      print(add(1,2))
Packages

●   Python modules can be categorized into
    packages by placing them within folders. The
    folder name becomes the package name and
    is used as a prefix with a period (dot) with the
    module name.
●   Packages can be initialized with a module
    named __init__.py
Python Module Search Path

●   PYTHONPATH is an environment variable set
    with the locations where the Python interpreter
    searches for modules
●   Typically, the module search path is defined
    as:
    PYTHONPATH=./usr/local/lib/pythonX.X
    which is the current directory and
    /usr/local/lib/pythonX.X
Questions?
         Ask at:
k.nitin.r [at] gmail.com

More Related Content

What's hot

What's hot (20)

Python libraries
Python librariesPython libraries
Python libraries
 
List in Python
List in PythonList in Python
List in Python
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python tuple
Python   tuplePython   tuple
Python tuple
 

Viewers also liked

Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
4. python functions
4. python   functions4. python   functions
4. python functions
in4400
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
Ranel Padon
 

Viewers also liked (20)

Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
CodeIgniter
CodeIgniterCodeIgniter
CodeIgniter
 
Python exceptions
Python exceptionsPython exceptions
Python exceptions
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Day2
Day2Day2
Day2
 
Functions
FunctionsFunctions
Functions
 
Functions in python
Functions in python Functions in python
Functions in python
 
python Function
python Function python Function
python Function
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Input and Output
Input and OutputInput and Output
Input and Output
 
Python
Python Python
Python
 
Python datatype
Python datatypePython datatype
Python datatype
 
Hot Spot Columbia
Hot Spot ColumbiaHot Spot Columbia
Hot Spot Columbia
 
4. python functions
4. python   functions4. python   functions
4. python functions
 
The Awesome Python Class Part-4
The Awesome Python Class Part-4The Awesome Python Class Part-4
The Awesome Python Class Part-4
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
 

Similar to Python Modules

Similar to Python Modules (20)

CLTL python course: Object Oriented Programming (2/3)
CLTL python course: Object Oriented Programming (2/3)CLTL python course: Object Oriented Programming (2/3)
CLTL python course: Object Oriented Programming (2/3)
 
Python import mechanism
Python import mechanismPython import mechanism
Python import mechanism
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
 
package module in the python environement.pptx
package module in the python environement.pptxpackage module in the python environement.pptx
package module in the python environement.pptx
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptx
 
Python Session - 5
Python Session - 5Python Session - 5
Python Session - 5
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
packages.pptx
packages.pptxpackages.pptx
packages.pptx
 
CLTL python course: Object Oriented Programming (3/3)
CLTL python course: Object Oriented Programming (3/3)CLTL python course: Object Oriented Programming (3/3)
CLTL python course: Object Oriented Programming (3/3)
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
CBSE-Class 12-Ch4 -Using Python Libraries - SasikalaJayaprakash
CBSE-Class 12-Ch4 -Using Python Libraries - SasikalaJayaprakashCBSE-Class 12-Ch4 -Using Python Libraries - SasikalaJayaprakash
CBSE-Class 12-Ch4 -Using Python Libraries - SasikalaJayaprakash
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
 
Modules 101
Modules 101Modules 101
Modules 101
 
Interesting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesInteresting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packages
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptx
 
Python modules
Python modulesPython modules
Python modules
 

More from Nitin Reddy Katkam (13)

FuelPHP
FuelPHPFuelPHP
FuelPHP
 
Sales Lead Pro
Sales Lead ProSales Lead Pro
Sales Lead Pro
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Program activation records
Program activation recordsProgram activation records
Program activation records
 
C Standards: main()
C Standards: main()C Standards: main()
C Standards: main()
 
Computer hardware
Computer hardwareComputer hardware
Computer hardware
 
8 Simple Rules to Keep Burglars Away
8 Simple Rules to Keep Burglars Away8 Simple Rules to Keep Burglars Away
8 Simple Rules to Keep Burglars Away
 
Software evaluation
Software evaluationSoftware evaluation
Software evaluation
 
Creating WordPress themes
Creating WordPress themesCreating WordPress themes
Creating WordPress themes
 
Code Igniter 2
Code Igniter 2Code Igniter 2
Code Igniter 2
 
Types of knowledge management systems
Types of knowledge management systemsTypes of knowledge management systems
Types of knowledge management systems
 
jQuery Date Picker
jQuery Date PickerjQuery Date Picker
jQuery Date Picker
 
Organizational Uncertainty
Organizational UncertaintyOrganizational Uncertainty
Organizational Uncertainty
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 

Python Modules

  • 1. Python Modules Python 3 Katkam Nitin Reddy k.nitin.r [at] gmail.com www.nitinkatkam.com
  • 2. Python Modules ● When you “import” a function, for instance, you are essentially using a module ● A module is essentially a Python file with a .py extension
  • 3. Using Modules ● You can import a module using import <module-name> and access the contents using <module-name>.<entity-name> ● You can also access entities directly from <module-name> import <entity-name> OR import <module-name> <alias>=<module-name>.<entity-name>
  • 4. Creating Modules ● Example: Create the following and save it as example.py def add(x,y): return x+y ● Now, in another Python file, call the add() function using the following: import example print(example.add(1,2))
  • 5. Creating Modules (Contd) ● Modules have the __name__ variable set to the module name ● When a Python file is called as a script, the __name__ is set to “__main__”. This lets you create modules that can also be executed as scripts using the following: def add(x,y): return x+y if __name__ == “__main__”: print(add(1,2))
  • 6. Packages ● Python modules can be categorized into packages by placing them within folders. The folder name becomes the package name and is used as a prefix with a period (dot) with the module name. ● Packages can be initialized with a module named __init__.py
  • 7. Python Module Search Path ● PYTHONPATH is an environment variable set with the locations where the Python interpreter searches for modules ● Typically, the module search path is defined as: PYTHONPATH=./usr/local/lib/pythonX.X which is the current directory and /usr/local/lib/pythonX.X
  • 8. Questions? Ask at: k.nitin.r [at] gmail.com