Learning how to import modules in Python 3 is one of the most essential and frequently used skills in modern Python programming — modules are the building blocks that unlock Python’s true power, letting you reuse code, organise large projects, and leverage thousands of third-party libraries from PyPI for web development, data science, automation, machine learning, and almost every real-world task.
In this up-to-date 2025–2026 guide, you’ll master exactly how to import modules in Python 3: installing packages with pip, basic & advanced import syntax, aliases, wildcard imports (why to avoid), circular imports, __all__ for public API, namespace packages, dynamic imports with importlib, best practices (PEP 8), troubleshooting, and modern tools. All examples are tested on Python 3.10–3.13.
Key Takeaways – How to Import Modules in Python 3
- Import modules in Python 3 with import module_name (recommended) or from module_name import name.
- Install third-party modules with pip install module_name — most come from PyPI.
- Use aliases (import numpy as np) — standard for popular libraries.
- Avoid from module import * (wildcard) — pollutes namespace and hides origins.
- Circular imports → refactor code or use local (in-function) imports.
- __all__ defines public API for wildcard imports (rarely needed).
- Namespace packages (PEP 420) — no __init__.py needed for multi-directory packages.
- importlib.import_module() — dynamic/programmatic imports.
- Follow PEP 8: group imports (standard → third-party → local), sort alphabetically.
Prerequisites
- Python 3.8+ installed (3.12/3.13 recommended)
- Basic Python knowledge (functions, scripts)
- Command line/terminal access
1. What Are Modules, Packages & Libraries?
- Module: Single .py file containing functions/classes/variables.
- Package: Directory with __init__.py (or implicit namespace in 3.3+) containing modules.
- Library: Collection of modules/packages (e.g., requests, pandas, Python Standard Library).
2. How to Install Modules with pip
# Install from PyPI
pip install requests
# Install specific version
pip install requests==2.31.0
# Upgrade
pip install --upgrade requests
# Install from requirements.txt
pip install -r requirements.txt
Verify installation:
import requests
print(requests.__version__) # e.g., 2.31.0
3. Basic Import Syntax – Best Way to Import Modules in Python 3
# Import whole module (recommended)
import math
print(math.sqrt(16)) # 4.0
# Alias (very common)
import numpy as np
import pandas as pd
# Specific items
from datetime import datetime, timedelta
now = datetime.now()
4. Wildcard Imports – Avoid This!
# Bad practice – pollutes namespace
from math import *
print(sqrt(25)) # Works but unclear origin
Why avoid: Name clashes, hard to read, hides source.
5. Circular Imports – How to Fix
Problem:
# module_a.py
from module_b import func_b
# module_b.py
from module_a import func_a
Solution options:
- Refactor: Move shared code to third module.
- Local import (inside function):
def my_func():
from module_b import func_b # delayed import
func_b()
- Use TYPE_CHECKING for type hints only:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from module_a import User
6. Controlling Public API with all
# my_module.py
__all__ = ['public_function', 'PublicClass']
def public_function(): ...
def _private_helper(): ... # not exported with *
7. Namespace Packages (PEP 420) – No init.py Needed
myapp/
├── core/
│ └── plugins/
│ └── base.py
└── plugins_extra/
└── myapp/
└── plugins/
└── extra.py
Both contribute to myapp.plugins namespace.
8. Dynamic Imports with importlib
import importlib
# Import by string name
module = importlib.import_module('math')
print(module.sqrt(16)) # 4.0
# Plugin system example
plugin = importlib.import_module('plugins.feature_a')
plugin.run()
9. PEP 8 Import Best Practices
# Standard library
import os
import sys
# Third-party
import numpy as np
import requests
# Local
from myapp.utils import helper
Sort alphabetically within groups.
10. Troubleshooting Import Errors
- ModuleNotFoundError: Not installed (pip install), typo, wrong environment.
- ImportError: cannot import name: Circular import, typo, wrong submodule.
- Shadowing: Don’t name files after stdlib/third-party modules (e.g., math.py).
How to Import Modules in Python 3 – FAQ (2025–2026)
- How do I import modules in Python 3?
import math (whole module) or from math import sqrt — safest way to import modules in Python 3. - What’s the best way to import in Python 3?
import module_name — clear, explicit, PEP 8 recommended. - How do I fix circular imports in Python 3?
Refactor or use local imports inside functions. - What does all do in Python 3?
Defines what gets imported with from module import * — controls public API. - How do I import dynamically in Python 3?
importlib.import_module(‘module_name’) — powerful for plugins.
Summary
You now know exactly how to import modules in Python 3: installing with pip, basic/advanced syntax, aliases, avoiding wildcards, fixing circular imports, __all__, namespace packages, importlib, best practices, and troubleshooting.
Mastering how to import modules in Python 3 unlocks the entire Python ecosystem — standard library, PyPI libraries, and your own clean, modular code. It’s the foundation of every serious Python project.
Recommended Next Tutorials
- How to Write & Structure Your Own Modules/Packages
- Python Virtual Environments & pipenv/poetry
- Python importlib – Dynamic & Advanced Imports
- Build a Plugin System with Dynamic Imports
- Python Project Structure & Packaging (2025–2026)