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 OverflowI 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.
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
I was experiencing problems trying to install this package. Could not install it at all. https://pypi.org/project/psycopg2/
From that page I've found this option:
pip install psycopg2-binary
and boom! it worked!
Why does Python say no module named psycopg2?
Should I install psycopg2-binary or psycopg?
Why can source installation fail?
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?
The AWS Lambda runtime environment doesn't include the PostgreSQL libraries so you need to include them within your AWS Lambda upload.
One way to do this is to get them from the jkehler/awslambda-psycopg2 repo at GitHub. Note that you don't need to build this project from scratch as the repo includes a pre-built package in the psycopg2 folder that you can simply include in your Lambda upload.
The psycopg2 build library from jkehler/awslambda-psycopg2 was built for python 3.6 and make sure that while uploading your code to AWS lambda, select Python Runtime environment as 3.6, and it should work. I banged my head on this for a full day and then when i changed to 3.6, the import error just vanished.
If you are going to attempt to build it yourself, remember that you must build on a machine or VM with the same architecture as your target at AWS.