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

Problems in Python Packages

The document discusses problems with importing packages and modules in Python. It provides examples of importing from the matplotlib package, creating custom packages and modules, and creating sub-packages. The examples show how to import specific functions and modules from packages and sub-packages and call functions from the imported modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Problems in Python Packages

The document discusses problems with importing packages and modules in Python. It provides examples of importing from the matplotlib package, creating custom packages and modules, and creating sub-packages. The examples show how to import specific functions and modules from packages and sub-packages and call functions from the imported modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Problems in Packages

1. To Write a program to import the


package named matplotlib.

import matplotlib
print(dir(matplotlib)[:3])

Output:
[‘AitoffAxes’, ‘Axes3D’, ‘HammerAxes’]
2. To write a program to import the function plot
from the module pyplot in the matplotlib package.

from matplotlib.pyplot import plot


print(plot)

Output:
<function plot at 0x113167c10>
3. To write a program to import the sub-package projections
from the matplotlib package and alias it as mat.

import matplotlib.projections as mat


print(dir(mat)[:3])

Output:
[‘AitoffAxes’, ‘Axes3D’, ‘HammerAxes’]
4. To create a package mypackage with modules moduleA and
moduleB. Both modules must contain a function that prints a
string of that module name. Also create another python file to
run the two functions.
File: mypackage/moduleA.py File: main.py
def getName1(): from mypackage.moduleA import getName1
print("ModuleA") from mypackage.moduleB import getName2
File: mypackage/moduleB.py getName1()
def getName2(): getName2()
print("ModuleB")
Output
ModuleA
moduleB
5. To create a sub-package road with modules cars and bikes in the
package transport. Both modules must contain a function that prints a
string of that module name. Also create another python file to run the
two functions.
File: transport/roads/cars.py

File: main.py
def getName1():
print("Cars.py") from transport.road.cars import getName1
File: transport/roads/bikes.py from transport.road.bikes import getName2
getName1()
getName2()
def getName2():
print("Bikes.py")
Thank you

You might also like