Python imports load reusable code from modules and packages. Start with the core import syntax, then install third‑party libraries when needed, control the search path for local code, and apply a few best practices from the Python documentation.
Option: Basic Import Syntax
These patterns cover most day‑to‑day imports.
Step 1: Import a module.
import math
Step 2: Access members with dot notation.
area = math.pi * (2 ** 2)
Step 3: Import specific names when you frequently use a few items.
from math import sqrt, factorial
r = sqrt(49)
Step 4: Use an alias for long names or established conventions.
import numpy as np
arr = np.array([1, 2, 3])
Step 5: Avoid wildcard imports.
Using from module import *
pollutes the namespace and makes code harder to read and debug.
Approach: Install Third‑Party Libraries
When a module isn’t part of the standard library, install it with pip in an isolated environment. The full workflow is outlined in the PyPA tutorial.
Step 1: Create a virtual environment.
# Unix/macOS
python3 -m venv .venv
# Windows
py -m venv .venv
Step 2: Activate the environment.
# Unix/macOS
source .venv/bin/activate
# Windows
.\.venv\Scripts\activate
Step 3: Install the package with pip.
python -m pip install requests
Step 4: Verify the import.
import requests
print(requests.__version__)
Method: Control the Module Search Path
Python looks for modules in directories listed in sys.path
.
Step 1: Inspect the current search path.
import sys, pprint
pprint.pprint(sys.path)
Step 2: Add a local directory at runtime only when necessary.
import sys
sys.path.append("/path/to/my_modules") # temporary during this process
Step 3: Prefer a package structure for your code.
# my_app/
# ├── my_app/
# │ ├── __init__.py
# │ ├── helpers.py
# │ └── mathutils.py
# └── main.py
# In main.py
from my_app.mathutils import hypotenuse
Way: Import from Packages
Use dotted names to reach submodules and items inside packages.
Step 1: Import a submodule with its full path.
import package.subpackage.module as mod
mod.run()
Step 2: Import a symbol directly from a submodule.
from package.subpackage.module import run
run()
Step 3: Keep imports at the top level of files for clarity.
Top‑level imports make dependencies obvious and avoid circular import traps in most cases.
Path: Reload a Module During a Session
In interactive sessions, reload a changed module with importlib.reload()
.
Step 1: Import the module once.
import mymodule
Step 2: Edit and save mymodule.py
in your editor.
Edits don’t apply automatically to already imported modules.
Step 3: Reload the module to pick up changes.
import importlib
importlib.reload(mymodule)
Troubleshooting Import Errors
Step 1: Resolve ModuleNotFoundError
by installing the package or activating the correct environment.
# Activate the right venv, then:
python -m pip install <package>
Step 2: Fix ImportError: cannot import name ...
by checking the symbol and its location.
Confirm the item exists in that module, check for typos and case, and import from the correct submodule.
Step 3: Avoid module shadowing by renaming files that collide with library names.
A local file like math.py
will hide the standard library math
module.
Step 4: Restart the interpreter when imports behave unexpectedly.
Python imports a module once per process; a restart resets state cleanly.
Best practices
- Keep imports at the top of the file in three groups: standard library, third‑party, then local modules.
- Prefer
import module
with dot notation for readability. - Use aliases only when they are conventional (for example,
numpy as np
). - Avoid
from module import *
in production code. - Use virtual environments to isolate dependencies per project.
With these patterns, you can import built‑ins, pull in third‑party libraries, and structure local code so imports stay fast and predictable.
Member discussion