VMware ESXi and vSphere Cluster Management
How to Import Modules in Python
Learn how Python modules work, create a local .py module, import an entire module, import selected functions, and troubleshoot common import errors.
A module is a Python source file that contains reusable code, such as functions, classes, and variables. A module normally uses the .py extension. Modules let you keep related code in a separate file instead of placing everything in one main application file.
For example, a file named simple_module.py becomes the module named simple_module when imported. The .py suffix is not included in the import statement.
Prerequisites
You should know how to run a basic Python script, define and call functions, pass arguments, work with strings, and create files and folders.
Create a Local Python Module
Create a file named simple_module.py. It contains two reusable functions: welcome displays a greeting, and bye displays a farewell.
# simple_module.py
def welcome(name):
print(f"Welcome, {name}!")
def bye(name):
print(f"Goodbye, {name}!")
Save the module file before trying to import it from another file. Python can only import the saved source file that it can find through its import locations.
Place the files together
For this introductory example, put both files in the same directory:
my_project/
main.py
simple_module.py
Python searches available import locations for a matching module. If the filename is misspelled, includes unexpected characters, is stored in another directory, or is not available on Python's import path, the import may fail.
Import an Entire Module
Use the import module_name syntax to import a complete module:
# main.py
import simple_module
simple_module.welcome("Bob")
Output:
Welcome, Bob!
The import makes the simple_module namespace available in main.py. A namespace is the collection of names currently available in a module or execution context.
Because the entire module was imported, call its functions through the module name: simple_module.welcome() or simple_module.bye(). This form is a qualified name: a name accessed through its module.
import simple_module
simple_module.welcome("Bob")
simple_module.bye("Bob")
The module prefix makes ownership clear. Someone reading the code can immediately see that welcome and bye come from simple_module. It also reduces ambiguity when different modules contain functions with the same name.
Import a Selected Function
Use the from ... import statement to bring a named attribute from a module into the current namespace. An attribute is a named item provided by a module, such as a function, class, or variable.
# main.py
from simple_module import bye
bye("John")
Output:
Goodbye, John!
Because bye was explicitly imported, it can be called directly without the simple_module. prefix. This import does not make every item in the module directly available. In particular, welcome is not available by that name.
Scope and Name Availability
The names introduced by an import depend on the import form:
For example:
from simple_module import bye
bye("John")
welcome("John")
The second call raises NameError because welcome was not imported directly and no local definition created that name. A NameError occurs when Python tries to use a name that has not been defined or imported in the current scope.
NameError: name 'welcome' is not defined
Fix the code by importing welcome too:
from simple_module import bye, welcome
bye("John")
welcome("John")
Alternatively, import the whole module and use qualified calls:
import simple_module
simple_module.bye("John")
simple_module.welcome("John")
Compare Python Import Styles
Choosing an Import Style
- Use a full-module import when clear ownership matters, when a module has several functions you will use, or when names might conflict.
- Use a selective import when you need only a small number of names and their direct use keeps the code easy to read.
- Prefer the style that makes the code understandable. Shorter code is not automatically clearer code.
- Remember that selective imports can hide where a function came from, especially in a large file.
Troubleshooting Import Problems
NameError after a selective import
Symptom: You import bye and then call welcome() directly.
Cause: The desired function was not included in the from ... import statement.
Solution: Import it explicitly with from simple_module import welcome, or import the module and call simple_module.welcome().
ModuleNotFoundError for a local module
Symptom: Python cannot find simple_module.
Possible causes: The module is not in the same directory as the running script, its directory is not on Python's import path, or its filename does not match the import name.
Solution: For this introductory example, place main.py and simple_module.py together. Check the spelling carefully and import the module without the extension.
import simple_module
Using the file extension in an import
Do not write import simple_module.py. The filename becomes the module name without .py:
import simple_module
Calling a function without the module prefix
After import simple_module, this call is incorrect:
welcome("Bob")
Use the qualified name instead:
simple_module.welcome("Bob")
Key Points
- A module is a reusable Python source file, normally ending in
.py. - The filename becomes the module name, without the
.pyextension. - For a simple local example, keep the module and importing script in the same directory.
import simple_modulemakes the module namespace available and requires qualified function calls.from simple_module import byemakesbyedirectly available, but does not directly import other functions.- Calling an unavailable name raises
NameError. - Choose the import style that keeps ownership clear and prevents name conflicts.