'ModuleNotFoundError: No module named 'psycopg2' (But it is installed)

I've been stuck on a part of a Udemy course. Even the (very helpful) tutor on there has run of ideas. When I try and run my script I get:

ModuleNotFoundError: No module named 'psycopg2'

I've done pip install psycopg2 and pip install psycopg2-2.8.4-cp37-cp37m-win_amd64.whl. Both result in 'requirement already satisfied'. I tried CTRL+SHIFT+P, Select Interpreter, and got the same problem with all of the three options. Only difference is Python3.8.0 gives me a Unable to import 'psycopg2' pylint(import-error) [1,1] error as well.

C:\Python\Database>pip install psycopg2
Requirement already satisfied: psycopg2 in c:\users\jeff\anaconda3\lib\site-packages (2.8.4)

C:\Python\Database>script1.py
Traceback (most recent call last):
  File "C:\Python\Database\script1.py", line 1, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

Edit

I still can't figure this out. And now I just got the same problem with Tweepy. ModuleNotFoundError: No module named 'tweepy' after I'd just successfully installed it. And similar error in the problems tab on VSC Unable to import 'tweepy' pylint(import-error) [1,1].



Solution 1:[1]

I guess you need to first install the dependencies.

sudo apt-get install build-dep python-psycopg2

Now run

pip install pyschopg2

Solution 2:[2]

I had a similar issue. I solved it by installing psycopg2 using PyCharm UI as follows :

  1. Go to Current interpreter at the bottom of the window Python Interpreter

  2. Interpreter Settings Interpreter Settings

  3. Press (+) to add a package Add package

  4. Type psycopg2 on the search bar and press Install Package at the botton Install psycopg2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anirudh Duggal
Solution 2 Michael Kohen