Error: No Module Named psycopg2 in python – fix

At times we get error in python stating that No Module named psycopg2 in python or  ModuleNotFoundError: No module named ‘psycopg2’. psycopg2 is a popular PostgreSQL adapter for Python. Which helps us to connect python with PostgreSQL

It looks like you’re trying to use the psycopg2 module in Python but encountering an error indicating that it’s not installed.

 

Solution for Error: No Module named psycopg2 in python:

To resolve this issue, you need to install the psycopg2 package. You can do this using pip, which is the package installer for Python. Here’s how you can install psycopg2:

  1. Open your command prompt or Anaconda prompt
  1. Run the following command
pip install psycopg2

Error No Module Named psycopg2 in python 1

This command will download and install psycopg2 and its dependencies.

If you are using jupyter notebook you can use the same command in the notebook itself

Error No Module Named psycopg2 in python 2

 

 

Verify the installation (optional):

by starting a Python interpreter or Jupyter notebook you can try to import psycopg2 as shown below

 
import psycopg2
print(psycopg2.__version__)

this will import the psycopg2 package and will print its version

Output:

Error No Module Named psycopg2 in python 3

 

Note :

If you continue to have issues, make sure that pip is up to date, you can upgrade pip by running

 
pip install --upgrade pip

And then try installing psycopg2 again.

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts