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

4

5

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].

Thrift answered 30/1, 2020 at 11:58 Comment(3)
See psycopg.org/articles/2018/02/08/psycopg-274-released. You may install psycopg2-binaryHalakah
were you able to resolve the issue?Bread
hey did you resolve this issue? I am running into the same problemPreprandial
H
6

Psycopg project has modified the way they distribute the package. Starting from version 2.8.0, psycopg2 wheel on Pypi is a source distribution. To get the same package you used to install, you have to

pip install psycopg2-binary

Explanations can be found in psycopg-2.7.4 release note:

The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: </docs/install.html#binary-install-from-pypi>.

Halakah answered 30/1, 2020 at 12:16 Comment(1)
Thanks for reply. I've tried this already and got 'Requirement already satisfied: psycopg2-binary in c:\users\jeff\anaconda3\lib\site-packages (2.8.4)'Thrift
A
1

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

Abernon answered 18/5, 2022 at 11:33 Comment(0)
J
0

I guess you need to first install the dependencies.

sudo apt-get install build-dep python-psycopg2

Now run

pip install pyschopg2
Juggins answered 30/1, 2020 at 12:7 Comment(4)
I got 'sudo' is not recognized as an internal or external command, operable program or batch file. Tried it without sudo and got 'apt-get' is not recognized as an internal or external command, operable program or batch file.Thrift
Oh! Sorry that was for ubuntu. For windows try conda install build-dep python-psycopg2 and then the second commandJuggins
Yes, because sudo or apt-get are Debian/Linux commands, and you are working on Microsoft Windows.Halakah
I got this.... Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. PackagesNotFoundError: The following packages are not available from current channels: - build-dep - python-psycopg2Thrift
M
0

One thing you can do is check is where the psycopg2 module is installed:

$ pip show psycopg2
Name: psycopg2
Version: 2.9.6
Summary: psycopg2 - Python-PostgreSQL Database Adapter
Home-page: https://psycopg.org/
Author: Federico Di Gregorio
Author-email: [email protected]
License: LGPL with exceptions
Location: /usr/lib/python3/dist-packages

And ensure that the directory named in Location: is on your path:

$ python3 -c 'import sys; print(sys.path)'
['', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/razzi/.local/lib/python3.11/site-packages', '/usr/local/lib/python3.11/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.11/dist-packages']

If the path is missing, you can add it to your PYTHONPATH or modify sys.path. Ideally you wouldn't have to do these; they may be indicative of a faulty install or a misconfigured environment, but they might be a useful workaround.

Maurer answered 4/1 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.