Python, a high-level, general-purpose programming language, is renowned for its simplicity, readability, and flexibility. One feature that significantly contributes to Python’s efficiency is the concept of
modules
. A Python module is a standalone file containing Python statements and definitions, usually encapsulating related code for easy organization and code reuse. This detailed article aims to provide an in-depth understanding of Python modules, their installation, and importation methods.
Python Modules Unwrapped
In Python, a module refers to a file encompassing Python code. This file may include functions, variables, classes, constants, and executable code. Modules are self-contained, designed for code reuse in various applications. Python modules, with their names corresponding to the filename with a .py
extension, have their own symbol table which serves as the global symbol table inside the module. Hence, each module is also considered a Python namespace.
A Python namespace is a dictionary mapping object names to the actual objects.
When a Python application imports a module, it gains access to the module’s entire contents. The module’s functions and variables can be utilized like any other Python code without requiring further code processing.
Why Use Python Modules?
Python modules offer several advantages:
- Promotes code reuse, accelerating the development process.
- Facilitates code organization, grouping similar functions and variables within a module.
- Enhances code maintainability.
- Reduces the size of the local symbol table.
- Allows importing individual functions without importing the entire module.
- Minimizes the likelihood of unintentional naming collisions with local or global variables.
Installing Python 3 Modules
Python 3 modules can be installed using the pip
package manager. However, for modules that don’t support pip
, you can still install them locally if they provide a setup.py
file.
Python comes with a plethora of useful standard modules, part of the Library Reference. These modules don’t require installation and can be imported and used as long as Python is installed on your system. A good example of a standard Python module is the math
module.
The following instructions are generally applicable for most Linux distributions, with a focus on Ubuntu users.
Installing Modules with pip
- Make sure the
pip
module is installed. You can installpip
using the APT package manager.sudo apt install python3-pip
- Verify the
pip
version to ensure correct installation.pip3 --version
This should return something like:
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
- Install the new Python module using the command
pip install <module-name>
. For example, to install theffmpeg-python
module, used for media processing tasks, use:pip install ffmpeg-python
Upon successful installation, you should see something similar to:
Successfully installed ffmpeg-python-0.2.0
- You can list all installed Python modules and packages using the
pip list
command.pip list
This will display a list similar to:
Package Version ---------------------- -------------------- attrs 19.3.0 Automat 0.8.0 ... zope.interface 4.7.1
Installing Modules Locally
In situations where a module cannot be installed using pip
, it can be installed locally. To do this, download the module and run the associated setup.py
script.
Let’s consider the Pythonkubernetes-client module as an example. Although this module is available through pip
and can be installed using pip install kubernetes
, we will consider a scenario where the module can only be installed locally.
- Download the package and extract it into a local directory. In this example, we use
git
, but different modules may provide different download instructions.git clone --recursive https://github.com/kubernetes-client/python.git
- Consult the module’s installation directives to determine the
setup.py
file location. In this case, it’s located in thepython
directory.
Note: If the module does not have a
setup.py
file, consult the instructions on the Python site. See the sections on Custom Installation and Modifying Python’s Search Path.cd python
- Install the module using the
python install
command.
Note: This particular module also requires the
requests
module. It’s common for locally-installed modules to require the installation of other modules before they can be used.python setup.py install --user
Importing Python 3 Modules
Whether installed using pip
or part of the Library Reference, all modules must be imported before they can be used in a file. A Python file can import an entire module or individual functions from a file.
Importing a Module Using “import”
The import
directive is used to import an entire Python module. Python can import a built-in module, third-party modules, or a module you have created.
To import a module, add the line import module_name
near the top of the file. When a module is imported, the Python interpreter first searches for a built-in module with the same name. If it does not find one, it then searches through the locations listed in sys.path
.sys.path
always includes the directory of the input script.
The following example shows how to import the Python math
module using the interactive console.
import math
Once a file has imported a module, it can use any of the module’s functions. To use a function from the module, specify the name of the module, followed by the.
symbol, and then the function name. This notation is required because Python considers the new module to be a separate namespace. In this example, math.factorial
references the factorial
function from math
.
math.factorial(9)
This returns:
362880
To see a list of imported modules, use the dir
command. It displays all the currently-defined names inside the current namespace.
dir()
This should display something like:
['__builtins__','__doc__','__name__','__package__','math']
To see what functions are available in a module, use the dir
function and enclose the name of the module in brackets. The module must be imported before this command can be used.
dir(math)
This will display something like:
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
The same approach is used to import and use a module inside a file. Here is a sample file containing the same functionality:
File: factorial.py
import math n = math.factorial(9) print(n)
Here are a few additional points to keep in mind when importing and using modules:
- Functions and variables in submodules are accessed similarly, with a
.
between the module and submodule. Use the syntaxmodule.submodule.function
. - If the module does not exist, Python displays the error
ImportError: No module named modulename
. - An
AttributeError
error is displayed if a function that doesn’t exist within the referenced module is called. - When creating and importing a module, ensure not to give it the same name as another module. This hides the original module, rendering its attributes inaccessible. This is referred to asshadowing and is generally undesirable.
- For readability and maintainability, place all
import
statements at the start of the module. - PEP 8 style guidelines recommend importing each module on a separate line, although it is possible to separate module names using a comma. The guidelines also recommend ordering modules alphabetically within groups. Standard library modules should be listed first, followed by third-party modules, and then local applications.
- Within a module, the global variable
__name__
contains the name of the module. - If a Python module contains executable code, it can run as a script using
python module_name.py
. In such cases,__name__
is set to__main__
instead of the module’s name. - Importing a module adds the module name to the importing module’s symbol table, but it does not add the individual functions or variables.
- Each module is only imported once per session. If the library changes during interactive development, reload it using
importlib.reload(modulename)
, or by exiting and restarting Python.
For a complete list of all built-in Python modules, refer to thePython Module Index.
Importing a Module Using “import from”
Python also enables programmers to import individual functions from a module without importing the module itself. Each function imported this way is added to the importing module’s symbol table, allowing the function to be invoked directly without using “dot notation”. The module name is no longer required in front of the function name. However, this approach does not provide access to other functions in the module or even the module itself.
To import an individual function from a module, add the line from <module_name> import <function_name>
to the file. The following instructions demonstrate how to import and use the factorial
function from the math
module. Python’s interactive mode is used for this example.
from mathimport factorial
To use factorial
, call the function directly the same way as a local function.
print(factorial(8))
This returns:
40320
It is possible to import every single function from a module without importing the actual module using the “wildcard import” technique. The syntax for this command is from module_name import *
. This technique can be useful in interactive mode or during script development to save time. However, the PEP 8 style guide does not consider this to be a good programming practice as it can hide functions that are already defined in the program and bloat the local symbol table.
It is also possible to give a function an easier name to avoid retyping the module name. Import the entire module, and reassign the function name using new_func_name = module_name.old_func_name
. This creates a new entry in the symbol table and overwrites any pre-existing function sharing the same name, so use this technique carefully.
Importing a Module Using “import as”
The import as
command allows programmers to supply a module or a function with an alias when it is imported. This technique is also referred to asaliasing a module. This strategy shortens the name of the module to make it easier to type. It can also reconcile naming conflicts with other modules or local functions and variables.
To import and alias a module, use the syntax import <module_name> as <alias>
. From then on, use the alias to refer to the module. The following example demonstrates how to import the Python time
module and give it the aliast
.
import time as t
To use the functions inside time
, use the alias t
along with “dot notation” and the function name. The following call to t.time
displays the system time in seconds as a floating-point number.
print(t.time())
This returns something like:
1637324618.8210588
Functions can also be aliased in this manner. To apply the alias rint
to random.randint
, use the from ... import ... as
construction. The alias is then used to refer to the imported function whenever it is required.
from random import randint as rint print(rint(1,10))
This returns a random integer between 1 and 10, for example:
2
Importing Variables, Functions, and Classes Using Import
The same methods that apply to modules are also used with variables, functions, and classes. Functions are typically imported as part of an entire module or through the use of the import ... from
directive. Classes are typically imported using the from
command.
The following example explains how to import the config
class from the kubernetes
module. The kubernetes
module can be installed using the command pip install kubernetes
.
from kubernetes import config
The config
class can now be used in the local program as if it were defined locally.
Final Thoughts on Python Modules
Python modules provide a way to organize and structure larger programs. Some modules are built-in and are part of the Python library, while other third-party modules must be installed first. Python’s pip
utility is used to install most modules. If a module is not available via pip
, it can be installed locally.
To utilize the functions in a module, Python must import the module first. The import
command is used to import an entire module. Specific functions can be imported from a module using the from <module> import <function>
command. When a module or function is imported, it becomes part of the local symbol table and can be used as a local object. Modules or functions can be given an alias using the from ... import ... as
directive. The alias can then be used to refer to the imported object. For more information on Python modules, consult thePython documentation on modules.
Additional Resources
For more information on Python modules and related topics, have a look at the following resources:
When working with Python and modules, consider Shape.host’s reliable, scalable, and secure cloud hosting solutions. Whether you’re building a simple script or a complex application, Shape.host offers a range of Linux SSD VPS hosting plans tailored to your needs. Visit Shape.host for more information.