How to fix ModuleNotFoundError: No module named 'psycopg2' in Python

PythonBEGINNERMEDIUMUpdated Dec 7, 2025ยท 126 views

This error occurs when Python can't find the module you're trying to import. The package either isn't installed, is installed in a different Python environment, or you have a typo in the import statement.

Quick fix

Fixes ModuleNotFoundError: No module named 'psycopg2'

which python3
python3 --version
  1. Verify Python interpreter
  2. Check if package is installed
  3. Install the missing package
  4. Use virtual environment
python โ€” error
which python3
python3 --version
ModuleNotFoundError: No module named 'psycopg2'

What this error means

The "ModuleNotFoundError: No module named 'X'" error means Python looked for a module called 'X' but couldn't find it. This happens when: 1. The package isn't installed in your current Python environment 2. You're using the wrong Python interpreter (python vs python3) 3. The package is installed globally but you're using a virtual environment 4. You have a typo in the import statement or module name 5. The package has a different import name than its installation name (e.g., install 'pillow', import 'PIL') The solution is to install the missing package using `pip install` in the correct Python environment.

Typical symptoms

  • ModuleNotFoundError: No module named 'package'
  • ImportError: cannot import name 'module'
  • Script works on one system but not another
  • Works in interactive Python but fails in scripts

Common causes

  1. Package is not installed in current Python environment
  2. Using wrong Python interpreter (python vs python3)
  3. Package installed in global Python but script uses virtual environment
  4. Typo in module name or import statement
  5. Package has different name than import (e.g., PIL vs pillow)

How to fix "ModuleNotFoundError: No module named 'psycopg2'"

1Verify Python interpreter

Check which Python your script is using:

bash
which python3
python3 --version

Always use python3 explicitly to avoid Python 2.

2Check if package is installed

List installed packages:

bash
pip list
# or
pip3 list

Search for your package:

bash
pip show package-name
3Install the missing package

Install using pip (check PyPI for correct package name):

bash
pip install package-name
# or
pip3 install package-name

For example, to import PIL, install pillow:

bash
pip install pillow
4Use virtual environment

Create and activate a virtual environment to isolate dependencies:

bash
python3 -m venv myenv
source myenv/bin/activate  # On Linux/Mac
myenv\\Scripts\\activate    # On Windows

pip install package-name
5Check import statement syntax

Verify your import statement is correct:

python
# Correct
import numpy
from pandas import DataFrame

# Incorrect
import numpy as np  # This works but imports 'numpy' package
import nump  # Typo - package name is 'numpy'

Advanced notes

Some packages have different import names than package names (e.g., install 'Pillow' but import 'PIL', install 'pyyaml' but import 'yaml'). Check PyPI documentation for the correct import name.

Frequently asked questions

What does "ModuleNotFoundError: No module named 'psycopg2'" mean?
The "ModuleNotFoundError: No module named 'X'" error means Python looked for a module called 'X' but couldn't find it. This happens when: 1. The package isn't installed in your current Python environment 2.
How do I fix "ModuleNotFoundError: No module named 'psycopg2'"?
This error occurs when Python can't find the module you're trying to import. The package either isn't installed, is installed in a different Python environment, or you have a typo in the import statement.
What causes "ModuleNotFoundError: No module named 'psycopg2'"?
Package is not installed in current Python environment Using wrong Python interpreter (python vs python3) Package installed in global Python but script uses virtual environment
Is the "ModuleNotFoundError: No module named 'psycopg2'" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading