The Python "ModuleNotFoundError: No module named 'psycopg2'" occurs when we forget to install the psycopg2-binary module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install psycopg2-binary command.
The easiest way to install psycopg2 is to use the pip command. This command will install packages from the Python Package Index, or 'pypi'. NOTE: This command will install the latest version of psycopg2 . If you want a particular version, just add a version number after the word psycopg2 , like this: =2.7.
Install the psycopg2 module in Windows 10 Now that we have our virtual environment setup and ready, install the psycopg2 module to create a simple connection in the PostgreSQL database cluster. Make sure to upgrade the pip command to avoid error when installing a package module from python in a virtual environment.
For anyone looking for a solution for this on macOS Sierra 10.12 (or later, most likely): I fixed this by installing the command line tools:
xcode-select --install
After that, pip install psycopg2
should work.
If it doesn't, you could also try to link against brew's openssl:
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
with openssl installed via brew. Note that the brew link openssl --force
does not work anymore:
$ brew link openssl --force 17.5s
Warning: Refusing to link: openssl
Linking keg-only openssl means you may end up linking against the insecure,
deprecated system OpenSSL while using the headers from Homebrew's openssl.
Instead, pass the full include/library paths to your compiler e.g.:
-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib
As @macho points out below if this still does not work, you might need to use the --no-cache
option of pip, e.g.
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip --no-cache install psycopg2
Remember to adjust these paths accordingly should you for instance build on ARM/Apple M1 Macs (as homebrew is installed at /opt/homebrew/
); command as follows:
env LDFLAGS="-I/opt/homebrew/opt/openssl/include -L/opt/homebrew/opt/openssl/lib" pip --no-cache install psycopg2
I had OpenSSL installed from brew (brew install openssl
)
The following worked for me:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
pip install psycopg2
Using brew link openssl
is dangerous as it might mess up your system by symlinking Homebrew's OpenSSL headers while the actual libraries will remain the system-supplied ones, causing all kinds of issues. Homebrew actually warns you against this if you try (and other answers suggest linking won't solve the problem anymore anyway):
$ brew link openssl
Warning: Refusing to link: openssl
Linking keg-only openssl means you may end up linking against the insecure,
deprecated system OpenSSL while using the headers from Homebrew's openssl.
Instead, pass the full include/library paths to your compiler e.g.:
-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib
Following this advice here's the pip
command you need to use:
$ pip install -r requirements.txt --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"
For pipenv
, I am not aware of any command-line attributes you can pass to it, however you can export the aforementioned paths as environment variables prior to running pipenv install
:
$ export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include"
$ pipenv install
With MacOS Catalina 10.15.4, the following was the only command that worked for me:
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
What worked for me was the hint provided in the command to link openssl,
$ brew link openssl
Warning: Refusing to link macOS-provided software: openssl
If you need to have openssl first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"
$ pip install psycopg2
Collecting psycopg2
Using cached https://files.pythonhosted.org/packages/23/7e/93c325482c328619870b6cd09370f6dbe1148283daca65115cd63642e60f/psycopg2-2.8.2.tar.gz
Installing collected packages: psycopg2
Running setup.py install for psycopg2 ... done
Successfully installed psycopg2-2.8.2
On mojave I added these to the .bash_profile
export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/curl/lib -L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/curl/include -I/user/local/opt/openssl/include"
was then able to install psycopg 2.8.3 in a python 3.7.4 virtualenv.
This after reinstalling xcode and the command line tools.
All the answers above helped!
This's the problem of new macOs version, where pip cannot install cryptography
. What fixed my problem is to provide the env to the install command:
brew install openssl
env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" <YOUR COMMAND HERE>
You can replace <YOUR COMMAND HERE>
with pip install cryptography
, or pip install <SOMETHING THAT REQUIRES cryptography>
for example.
Credit to this article: Fixing macOS Sierra fatal error: 'openssl/opensslv.h' or 'openssl/aes.h' file not found
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With