0% found this document useful (0 votes)
13 views

Python

The document discusses Python modules and how to create and use them. It explains that modules allow importing functions and accessing them using dot notation. Modules can also be organized into packages by placing them in folders. The module search path and how to initialize packages are also covered.

Uploaded by

Cool Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python

The document discusses Python modules and how to create and use them. It explains that modules allow importing functions and accessing them using dot notation. Modules can also be organized into packages by placing them in folders. The module search path and how to initialize packages are also covered.

Uploaded by

Cool Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Modules

Presented By
Harsh Kumar Singh
Rohit
Kartik
ansh
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

You might also like