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.
import math
area = math.pi * (2 ** 2)
from math import sqrt, factorial
r = sqrt(49)
import numpy as np
arr = np.array([1, 2, 3])
Using from module import * pollutes the namespace and makes code harder to read and debug.
Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →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.
# Unix/macOS
python3 -m venv .venv
# Windows
py -m venv .venv
# Unix/macOS
source .venv/bin/activate
# Windows
.\.venv\Scripts\activate
python -m pip install requests
import requests
print(requests.__version__)
Method: Control the Module Search Path
Python looks for modules in directories listed in sys.path.
import sys, pprint
pprint.pprint(sys.path)
import sys
sys.path.append("/path/to/my_modules") # temporary during this process
# 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.
import package.subpackage.module as mod
mod.run()
from package.subpackage.module import run
run()
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().
import mymodule
Edits don’t apply automatically to already imported modules.
import importlib
importlib.reload(mymodule)
Troubleshooting Import Errors
# Activate the right venv, then:
python -m pip install <package>
Confirm the item exists in that module, check for typos and case, and import from the correct submodule.
A local file like math.py will hide the standard library math module.
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 modulewith 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.






