Python 3 - Modules
Python 3 - Modules
Python 3 - Modules
A module allows you to logically organize your Python code. Grouping related code into a module
makes the code easier to understand and use. A module is a Python object with arbitrarily named
attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and
variables. A module can also include runnable code.
Example
The Python code for a module named aname normally resides in a file namedaname.py. Here is an
example of a simple module, support.py −
When the interpreter encounters an import statement, it imports the module if the module is present
in the search path. A search path is a list of directories that the interpreter searches before importing
a module. For example, to import the module hello.py, you need to put the following command at the
top of the script −
#!/usr/bin/python3
Hello : Zara
https://www.tutorialspoint.com/python3/python_modules.htm 1/7
17.01.2023 17:15 Python 3 - Modules
A module is loaded only once, regardless of the number of times it is imported. This prevents the
module execution from happening repeatedly, if multiple imports occur.
AD
In-store shopping
The from...import Statement
Python's from statement lets you import specific attributes from a module into the current
namespace. The from...import has the following syntax −
For example, to import the function fibonacci from the module fib, use the following statement −
#!/usr/bin/python3
This statement does not import the entire module fib into the current namespace; it just introduces
the item fibonacci from the module fib into the global symbol table of the importing module.
This provides an easy way to import all the items from a module into the current namespace;
however, this statement should be used sparingly.
https://www.tutorialspoint.com/python3/python_modules.htm 2/7
17.01.2023 17:15 Python 3 - Modules
Mağazada İncele
AD
Live Demo
#!/usr/bin/python3
When you run the above code, the following output will be displayed.
Locating Modules
When you import a module, the Python interpreter searches for the module in the following
sequences −
If the module is not found, Python then searches each directory in the shell variable
PYTHONPATH.
If all else fails, Python checks the default path. On UNIX, this default path is normally
/usr/local/lib/python3/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path
variable contains the current directory, PYTHONPATH, and the installation-dependent default.
https://www.tutorialspoint.com/python3/python_modules.htm 3/7
17.01.2023 17:15 Python 3 - Modules
A Python statement can access variables in a local namespace and in the global namespace. If
a local and a global variable have the same name, the local variable shadows the global
variable.
Each function has its own local namespace. Class methods follow the same scoping rule as
ordinary functions.
Python makes educated guesses on whether variables are local or global. It assumes that any
variable assigned a value in a function is local.
Therefore, in order to assign a value to a global variable within a function, you must first use the
global statement.
The statement global VarName tells Python that VarName is a global variable. Python stops
searching the local namespace for the variable.
For example, we define a variable Money in the global namespace. Within the function Money, we
assign Money a value, therefore Python assumes Money as a local variable.
However, we accessed the value of the local variable Money before setting it, so an
UnboundLocalError is the result. Uncommenting the global statement fixes the problem.
Live Demo
#!/usr/bin/python3
Money = 2000
def AddMoney():
# Uncomment the following line to fix the code:
# global Money
Money = Money + 1
print (Money)
https://www.tutorialspoint.com/python3/python_modules.htm 4/7
17.01.2023 17:15 Python 3 - Modules
AddMoney()
print (Money)
The list contains the names of all the modules, variables and functions that are defined in a module.
Following is a simple example −
Live Demo
#!/usr/bin/python3
content = dir(math)
print (content)
Here, the special string variable __name__ is the module's name, and __file__ is the filename from
which the module was loaded.
If locals() is called from within a function, it will return all the names that can be accessed
locally from that function.
If globals() is called from within a function, it will return all the names that can be accessed
globally from that function.
The return type of both these functions is dictionary. Therefore, names can be extracted using the
keys() function.
https://www.tutorialspoint.com/python3/python_modules.htm 5/7
17.01.2023 17:15 Python 3 - Modules
Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function.
The reload() function imports a previously imported module again. The syntax of the reload() function
is this −
reload(module_name)
Here, module_name is the name of the module you want to reload and not the string containing the
module name. For example, to reload hello module, do the following −
reload(hello)
Packages in Python
A package is a hierarchical file directory structure that defines a single Python application
environment that consists of modules and subpackages and sub-subpackages, and so on.
Consider a file Pots.py available in Phone directory. This file has the following line of source code −
#!/usr/bin/python3
def Pots():
print ("I'm Pots Phone")
Similar, we have other two files having different functions with the same name as above. They are −
Phone/__init__.py
To make all of your functions available when you have imported Phone, you need to put explicit
import statements in __init__.py as follows −
After you add these lines to __init__.py, you have all of these classes available when you import the
Phone package.
https://www.tutorialspoint.com/python3/python_modules.htm 6/7
17.01.2023 17:15 Python 3 - Modules
#!/usr/bin/python3
Phone.Pots()
Phone.Isdn()
Phone.G3()
In the above example, we have taken example of a single function in each file, but you can keep
multiple functions in your files. You can also define different Python classes in those files and then
you can create your packages out of those classes.
https://www.tutorialspoint.com/python3/python_modules.htm 7/7