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

Python Modules

This document discusses Python modules and packages. It explains that a module is a file containing functions and other objects that can be imported and used in other Python programs. Modules can be created by saving a .py file. Built-in and third-party modules can be imported using the import statement. Packages are directories that contain modules and have an __init__.py file to identify them as packages. Modules within packages can be imported using dot notation. The pip package manager is used to install third-party packages.

Uploaded by

Sudesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Python Modules

This document discusses Python modules and packages. It explains that a module is a file containing functions and other objects that can be imported and used in other Python programs. Modules can be created by saving a .py file. Built-in and third-party modules can be imported using the import statement. Packages are directories that contain modules and have an __init__.py file to identify them as packages. Modules within packages can be imported using dot notation. The pip package manager is used to install third-party packages.

Uploaded by

Sudesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Python Modules

Python 3.6.5
What is a Module?
• Consider a module to be the same as a code library.
• A file containing a set of functions you want to include in your
application.
Create a Module
• To create a module just save the code you want in a file with
the file extension .py:
Example
Save this code in a file named mymodule.py
def greeting(name):
print("Hello, " + name)
Use a Module
• Now we can use the module we just created, by using
the import statement:
Example
Import the module named mymodule, and call the greeting
function:
import mymodule
mymodule.greeting("Jonathan")
Variables in Module
Example
Save this code in the file mymodule.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import the module named mymodule, and access the person1
dictionary:
import mymodule

a = mymodule.person1["age"]
print(a)
Re-naming a Module
• You can create an alias when you import a module, by using
the as keyword:

Example
Create an alias for mymodule called mx:
import mymodule as mx

a = mx.person1["age"]
print(a)
Built-in Modules
• There are several built-in modules in Python, which you can
import whenever you like.
Example
Import and use the platform module:
import platform

x = platform.system()
print(x)
Using the dir() Function
• There is a built-in function to list all the function names (or
variable names) in a module. The dir() function:
Example
List all the defined names belonging to the platform module:
import platform

x = dir(platform)
print(x)

• Note: The dir() function can be used on all modules, also the
ones you create yourself.
Import From Module
• You can choose to import only parts from a module, by using
the from keyword.
Example
The module named mymodule has one function and one dictionary:
def greeting(name):
print("Hello, " + name)

person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
• Import only the person1 dictionary from the module:
from mymodule import person1

print (person1["age"])
What is package?
• Python has packages for directories and modules for files.
• A directory must contain a file named __init__.py in order for
Python to consider it as a package.
Importing module from a package
• We can import modules from packages using the dot (.)
operator.
• For example, if want to import the start module in the above
example, it is done as follows.
import Game.Level.start

• Now if this module contains a function named


select_difficulty(), we must use the full name to reference it.
Game.Level.start.select_difficulty(2)
Importing module from a package
• If this construct seems lengthy, we can import the module
without the package prefix as follows.
from Game.Level import start

• We can now call the function simply as follows.


start.select_difficulty(2)
Importing module from a package
• Yet another way of importing just the required function (or
class or variable) form a module within a package would be as
follows.
from Game.Level.start import select_difficulty

• Now we can directly call this function.


select_difficulty(2)
Python PIP
What is PIP?
• PIP is a package manager for Python packages, or modules if
you like.
• Note: If you have Python version 3.4 or later, PIP is included by
default.

What is a Package?
• A package contains all the files you need for a module.
• Modules are Python code libraries you can include in your
project.
Check if PIP is Installed
• Navigate your command line to the location of Python's script
directory, and type the following:

C:\Python65\Scripts>pip --version

• If you get version as output means PIP is installed.

Install PIP
• If you do not have PIP installed, you can download and install
it from this page: https://pypi.org/project/pip/
Download a Package
• Downloading a package is very easy.
• Open the command line interface and tell PIP to download the
package you want.
• Navigate your command line to the location of Python's script
directory, and type the following to install package camelcase:

C:\Python36\Scripts>pip install camelcase

• Now you have downloaded and installed your first package!


Using a Package
• Once the package is installed, it is ready to use.
• Import the "camelcase" package into your project.

import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))

Find Packages
• Find more packages at https://pypi.org/.
Thanks

You might also like