How to Resolve "ModuleNotFoundError: No module named 'pytz'" in Python
Fix the ModuleNotFoundError for pytz in Python by installing the package via pip, troubleshooting environment issues, and using virtual environments for dependency isolation.
The error ModuleNotFoundError: No module named 'pytz' in Python means that the pytz library, which provides timezone definitions, is not installed in your current Python environment.
This guide provides a step-by-step guide to installing pytz and resolving this common error.
Understanding the Error
The ModuleNotFoundError indicates that Python can not find a module named pytz when your code tries to import pytz or from pytz import .... This usually means the package is not installed in the Python environment you're using.
Installing pytz with pip (Recommended)
The standard way to install Python packages is using pip.
Basic Installation
Open your terminal (or command prompt on Windows) and run:
pip install pytz
If you have multiple Python versions installed, or if pip is associated with Python 2, use:
pip3 install pytz # Use pip3 for Python 3 (might be pip3.x)
Using python -m pip (If pip Isn't in Your PATH)
If the pip command doesn't work (e.g., "command not found"), use this instead:
python -m pip install pytz # Use with your system's default Pythonpython3 -m pip install pytz # Use with your system's Python 3
This ensures you're using the pip associated with your Python installation.
Windows-Specific Considerations (py launcher)
On Windows, the py launcher is often helpful:
py -m pip install pytz
The py command uses the latest Python version installed.
Permissions Issues (sudo or --user)
On Linux/macOS, you might need sudo for system-wide installation (but virtual environments are strongly preferred):
sudo pip3 install pytz # System-wide. Use with caution.
Alternatively, install into your user's site-packages:
pip install pytz --user # Install for current user only
Use sudo to install packages globally.
The --user installs packages in your user directory, which doesn't require administrator privileges.
Using Virtual Environments (Highly Recommended)
Always use virtual environments for Python projects to isolate dependencies:
Creating a Virtual Environment
python3 -m venv venv # Create an environment named "venv"
Activating the Environment
macOS / Linux:
source venv/bin/activate
Windows (Command Prompt):
venv\Scripts\activate.bat
Windows (PowerShell):
venv\Scripts\Activate.ps1
If this fails in PowerShell, you may need to adjust the execution policy:
After activation, your prompt will usually show the environment name (e.g., (venv)). pip install now installs into this environment only.
Installing pytz Inside the Environment
With your virtual environment activated:
pip install pytz # Installs into the active environment
Troubleshooting
If you're still getting the error after installing:
IDE Configuration (VS Code, PyCharm): Your IDE might be using a different Python interpreter.
VS Code: Press Ctrl+Shift+P (or Cmd+Shift+P on macOS), type "Python: Select Interpreter", and choose the correct interpreter (your virtual environment).
Conflicting Installations/Multiple Python Versions: If you have multiple Python versions without using virtual environments, you may have installed pytz for the wrong version. Always use virtual environments!
Incorrect Filenames/Variable Names:Never name your Python files pytz.py. This conflicts with the installed library. Avoid variable names like pytz as well.
Restarting Your IDE/Kernel: Sometimes (especially in Jupyter), you need to restart.
Reinstalling Pytz
As a last resort, you can try reinstalling the package, as it might resolve any issues that might be cause by incorrect or partial installations: