ModuleNotFoundError: No module named ‘psycopg2’

In Python, ModuleNotFoundError: No module named ‘psycopg2’ error occurs if we try to import the ‘psycopg2-binary‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the psycopg2-binary module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘psycopg2’ error.  

What is ModuleNotFoundError: No module named ‘psycopg2’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘psycopg2’ error

  • Trying to use the module- without installing the psycopg2-binary package.
  • If the IDE is set to the incorrect version of the Python/Python interpreter.
  • You are using the virtual environment and the psycopg2-binary module is not installed inside a virtual environment
  • Installing the psycopg2-binary package in a different version of Python than the one which is used currently.
  • Declaring a variable name as the module name(psycopg2-binary)

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘psycopg2’?

psycopg2-binary is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Psycopg is the most popular PostgreSQL database adapter for the Python programming language. Its main features are the complete implementation of the Python DB API 2.0 specification and the thread safety (several threads can share the same connection).

We can fix the error by installing the ‘psycopg2-binary‘ module by running the pip install psycopg2-binary command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: psycopg2-binary.

pip show psycopg2-binary

Output

Name: psycopg2
Version: 2.9.3
Summary: psycopg2 - Python-PostgreSQL Database Adapter
Home-page: https://psycopg.org/
Author: Federico Di Gregorio
Author-email: fog@initd.org
License: LGPL with exceptions
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the psycopg2-binary module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the psycopg2-binary module.

# If you are using Python 2 (Windows)
pip install psycopg2-binary

# if you are using Python 3 (Windows)
pip3 install psycopg2-binary

# If the pip is not set as environment varibale PATH
python -m pip install psycopg2-binary

# If you are using Python 2 (Linux)
sudo pip install psycopg2-binary

# if you are using Python 3 (Linux)
sudo pip3 install psycopg2-binary

# In case if you have to easy_install
sudo easy_install -U psycopg2-binary

# On Centos
yum install psycopg2-binary

# On Ubuntu
sudo apt-get install psycopg2-binary

# If you are installing it in Anaconda 
conda install -c conda-forge psycopg2-binary

Once you have installed the psycopg2-binary module, we can now import it inside our code and use it as shown below.

import psycopg2

# Connect to your postgres DB
conn = psycopg2.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
cur = conn.cursor()

# Execute a query
cur.execute("SELECT * FROM my_data")

# Retrieve query results

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

Python Interpreter

Solution 3 – Installing psycopg2-binary inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the psycopg2-binary module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install psycopg2-binary inside the virtual environment
pip install psycopg2-binary

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as psycopg2.py as it may shadow the original psycopg2-binary module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘psycopg2’ error occurs when we try to import the ‘psycopg2-binary‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the psycopg2-binary module by running the pip install psycopg2-binary command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.