Modules and Packages in Python
Modules and Packages in Python
Modules and Packages in Python
and
Packages
1
Index
1. Python Modules
2. Loading the module in our python code
➢ The import statement
➢ The from-import statement
3. Renaming a module
4. The reload() function
5. Scope of variables
6. Python packages
2
1. Python Modules
3
Example:
def displayMsg(name)
print("Hi "+name);
4
2. Loading the module in our python code
5
We can import multiple modules with a single import
statement, but a module is loaded once regardless of the
number of times, it has been imported into our file.
6
Example:
import file;
name = input("Enter the name?")
file.displayMsg(name)
Output:
Enter the name?John
Hi John
7
The from-import statement
8
calculation.py:
9
Main.py:
10
Output:
11
The from...import statement is always better to use if we
know the attributes to be imported from the module in
advance. It doesn't let our code to be heavier. We can also
import all the attributes from a module by using *.
12
3. Renaming a module
13
Example:
Output:
Enter a?10
Enter b?20
Sum = 30
14
4. The reload() function
As we have already stated that, a module is loaded once
regardless of the number of times it is imported into the
python source file. However, if you want to reload the already
imported module to re-execute the top-level code, python
provides us the reload() function. The syntax to use the
reload() function is given below.
reload(<module-name>)
15
5. Scope of variables
16
Example:
name = "john"
def print_name(name):
print("Hi",name) #prints the name that is local to this
function only.
name = input("Enter the name?")
print_name(name)
Output:
Hi David
17
6. Python packages
18
2. Create a python source file with name ITEmployees.py on the
path /home/Employees.
ITEmployees.py
def getITNames():
List = ["John", "David", "Nick", "Martin"]
return List;
19
3. Similarly, create one more python file with name
BPOEmployees.py and create a function getBPONames().
20
4. Now, the directory Employees which we have created in the
first step contains two python modules. To make this
directory a package, we need to include one more file here,
that is __init__.py which contains the import statements of
the modules defined in this directory.
__init__.py
21
5. Now, the directory Employees has become the package
containing two python modules. Here we must notice that we
must have to create __init__.py inside a directory to convert this
directory to a package.
22
6. To use the modules defined inside the package Employees,
we must have to import this in our python source file. Let's
create a simple python source file at our home directory
(/home) which uses the modules defined in this package.
Test.py
import Employees
print(Employees.getNames())
23
6. To use the modules defined inside the package Employees,
we must have to import this in our python source file. Let's
create a simple python source file at our home directory
(/home) which uses the modules defined in this package.
Test.py
import Employees
print(Employees.getNames())
24