The problem is that you installed psycopg2 as a superuser, i.e. using 'sudo'. When you run commands as a super user, the command gets executed in a different shell that has the superuser's environment, and as a result the package will be installed in a different location in the filesystem that may not be accessible to your 'normal' environment. You need to install the package again as a regular user, i.e. without the leading 'sudo' in order to use the package. In short, just run:

pip install psycopg2
Answer from gilsho on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › error importing psycopg2 with sqlalchemy
r/learnpython on Reddit: Error importing psycopg2 with SQLAlchemy
March 4, 2024 -

I have an app using FastAPI and SQLAlchemy with PostgreSQL, when I run it I get an error message:

File "C:\Users\User\.pyenv\pyenv-win\versions\3.9.13\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 690, in import_dbapi

import psycopg2

ModuleNotFoundError: No module named 'psycopg2'

I tried both psycopg2 and psycopg2-binary but it still doesn't work.

If I open a separate python shell I can import psycopg2 without issue but inside my app I get this error.

I am not using a venv btw.

🌐
GitHub
github.com › psycopg › psycopg2 › issues › 1338
No module named 'psycopg2._psycopg python 3.9 ubuntu docker · Issue #1338 · psycopg/psycopg2
August 17, 2021 - File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/engine/init.py", line 435, in create_engine return strategy.create(*args, **kwargs) File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/engine/strategies.py", line 87, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 632, in dbapi import psycopg2 File "/usr/local/lib/python3.9/dist-packages/psycopg2/init.py", line 51, in from psycopg2._psycopg import ( # noqa ModuleNotFoundError: No module named 'psycopg2._psycopg'
Author   psycopg
People also ask

Why does Python say no module named psycopg2?
The active interpreter cannot find a package that provides the psycopg2 module, often because pip installed into another environment.
🌐
pythonpool.com
pythonpool.com › home › error › fix no module named psycopg2: install and environment checks
Fix No Module Named psycopg2: Install and Environment Checks
Should I install psycopg2-binary or psycopg?
Use the package and major version supported by the application; psycopg is the modern package while psycopg2-binary can simplify local development.
🌐
pythonpool.com
pythonpool.com › home › error › fix no module named psycopg2: install and environment checks
Fix No Module Named psycopg2: Install and Environment Checks
Why can source installation fail?
A source build may require PostgreSQL client headers and a compiler, while a compatible wheel avoids those system build dependencies.
🌐
pythonpool.com
pythonpool.com › home › error › fix no module named psycopg2: install and environment checks
Fix No Module Named psycopg2: Install and Environment Checks
🌐
Position Is Everything
positioniseverything.net › home › no module named psycopg2: the complete solution guide
No Module Named PSYCOPG2: The Complete Solution Guide - Position Is Everything
December 29, 2025 - The no module named ‘psycopg2’ Django code exception usually occurs when failing or forgetting to install the binary module before importing it to an incorrect virtual environment.
🌐
Reddit
reddit.com › r/cs50 › project1 modulenotfounderror: no module named 'psycopg2'
r/cs50 on Reddit: Project1 ModuleNotFoundError: No module named 'psycopg2'
January 6, 2019 -

Have an error on Flask server

Fist one was 'ModuleNotFoundError: No module named 'flask_session'. I fixed it with pip install flask_session.

Now I have 'No module named 'psycopg2''

What didn't help:

pip3 install -r requirements.txt

pip install sqlalchemy

pip3 install sqlalchemy

pip3 install psycopg2

pip install psycopg2

sudo apt-get install build-dep python-psycopg2

Any ideas?

🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
unable to access python libraries in flask - Raspberry Pi Forums
Google "Flask ModuleNotFoundError: ... problem and been unable to find a solution that works for me. Solutions I have found simply say to execute the command pip install psycopg2 Which I accept makes perfect sense....
🌐
GitHub
github.com › psycopg › psycopg2 › issues › 1282
Error loading psycopg2 module: No module named 'psycopg2' · Issue #1282 · psycopg/psycopg2
May 26, 2021 - (env) PS D:\Users\llagos.NB-KEYONE15\Visual Studio projects\ecommerce-psql> python manage.py makemigrations Traceback (most recent call last): File "D:\Users\llagos.NB-KEYONE15\Visual Studio projects\ecommerce-psql\env\lib\site-packages\django\db\backends\postgresql\base.py", line 25, in import psycopg2 as Database ModuleNotFoundError: No module named 'psycopg2' ...
Author   psycopg
🌐
Python Pool
pythonpool.com › home › error › fix no module named psycopg2: install and environment checks
Fix No Module Named psycopg2: Install and Environment Checks
December 9, 2021 - Fix psycopg2 import errors by checking the active environment, choosing psycopg2 or psycopg, and handling wheels and system dependencies.
Find elsewhere
🌐
GitHub
github.com › apache › superset › discussions › 31431
ModuleNotFoundError: No module named 'psycopg2' during k8 installation · apache/superset · Discussion #31431
To resolve the "ModuleNotFoundError: No module named 'psycopg2'" error during the deployment of Superset using a Helm chart, you should ensure that the correct version of psycopg2 is pinned.
Author   apache
🌐
pythontutorials
pythontutorials.net › blog › connecting-postgresql-with-sqlalchemy
How to Connect PostgreSQL with SQLAlchemy in Python: Fixing 'No Module Named psycopg2' ImportError — pythontutorials.net
Test the connection with SQLAlchemy’s engine.connect(). The No module named psycopg2 error is almost always fixed by installing psycopg2-binary or resolving system-level dependencies for psycopg2.
🌐
GitHub
github.com › pgadmin-org › pgadmin4 › issues › 7314
psycopg2 not found · Issue #7314 · pgadmin-org/pgadmin4
March 25, 2024 - ''' 2024-03-25 10:46:54,608: INFO ... ^^^^^^^^^^^^^^^^^^^^^^^^ File "/venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 690, in import_dbapi import psycopg2 ModuleNotFoundError: No module named 'psycopg2' '''...
Author   pgadmin-org
🌐
Esri Community
community.esri.com › t5 › arcgis-data-interoperability-ques › interop-workbench-returns-no-module-named-psycopg2 › td-p › 1603638 › jump-to › first-unread-message
Interop workbench returns 'No module named psycopg2'
April 8, 2025 - Here a screenshot of the installed module ... - We are living in the 21st century. GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS! ... Tom in the Workbench application, open the Tools menu and in the Translation options set the Python compatibility to agree with Pro - the option named "Esri ArcGIS 3.11" or similar, depending on version.
🌐
GitHub
github.com › 3loc › charts › issues › 2
No module named `psycopg2` when using postgresql · Issue #2 · 3loc/charts
May 8, 2020 - 2020/05/08 04:51:31 ERROR mlflow.cli: ...pi(**dbapi_args) File "/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 737, in dbapi import psycopg2 ModuleNotFoundError: No module named 'psycopg2'...
Author   3loc
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-modulenotfounderror-no-module-named-psycopg2-in-python
How to Fix 'ModuleNotFoundError: No Module Named psycopg2' in Python - GeeksforGeeks
July 23, 2025 - We can resolve this error by installing the psycopg2 package externally using the PIP. Execute the below command in the prompt to install the package. ... Once we execute the command, it will take a couple of minutes to completely install the ...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-no-module-named-psycopg2
ModuleNotFoundError: No module named 'psycopg2' in Python | bobbyhadz
April 8, 2024 - The Python ModuleNotFoundError: No module named 'psycopg2' occurs when we forget to install the `psycopg2-binary` module before importing it.
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › Superset-No-module-named-psycopg2 › td-p › 211213
Solved: [Superset]: No module named psycopg2 - Cloudera Community - 211213
September 19, 2022 - I made sure I installed this module using pip install psycopg2 in shell but it's still not working! ... Alternatively, We package superset with pygresql and you can change the connection URI to use that - postgresql+pygresql://user:password@host:port/dbname ... Alternatively, We package superset with pygresql and you can change the connection URI to use that - postgresql+pygresql://user:password@host:port/dbname ... Thank you @Nishant Bangarwa! "No module psycopg2" solved!
🌐
GitHub
github.com › apache › superset › issues › 32364
Docker compose No module named 'psycopg2' - 5.0.0rc1 · Issue #32364 · apache/superset
February 24, 2025 - Bug description During upgrading to new 5.0.0rc1 version is not possible due to No module named 'psycopg2'. Error: superset | File "/app/.venv/lib/python3.10/site-packages/sqlalchemy/d...
Author   apache