Unable to install psycopg2 (pip install psycopg2)

76,028

Solution 1

Try this:

pip install psycopg2-binary

Solution 2

I had the same issue when I tried 'pip installing' packages for an ongoing project on a brand new MacBook running on Big Sur OS. After some research, I came across this solution which worked for me. Here are the steps:

  1. Install Homebrew using this command:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  2. brew install postgresql

  3. brew install openssl

  4. brew link openssl

  5. Set the following environment variables ("flags"):

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"

export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

  1. And finally install psycopg2 using the following command:

pip install psycopg2-binary

Here is the result:

Collecting psycopg2-binary Using cached psycopg2-binary-2.9.1.tar.gz (380 kB) Building wheels for collected packages: psycopg2-binary Building wheel for psycopg2-binary (setup.py) ... done Created wheel for psycopg2-binary: filename=psycopg2_binary-2.9.1-cp39-cp39-macosx_10_9_universal2.whl size=241235 sha256=e825a38765f20a331ef619e1368ee9d1a678f34969e3c467d94bc4122af1ac6f Stored in directory: /Users/me/Library/Caches/pip/wheels/4b/c8/c2/72089ea1a611c119754d513bdacea935cfeb19600d06d45b4b Successfully built psycopg2-binary Installing collected packages: psycopg2-binary Successfully installed psycopg2-binary-2.9.1

Solution 3

Same issue, forgot to install psql: https://wiki.postgresql.org/wiki/Homebrew

So i ran:

brew install postgresql
brew services start postgresql

Solution 4

You're using an older Python without the most secure TLS implementation, you need to upgrade it. Otherwise, you will not be able to "pip install" packages from PyPI.

1) To check your Python interpreter's TLS version, install the "requests" package and run a command. For example, for Python 2:

python2 -m pip install --upgrade requests
python2 -c "import requests;
print(requests.get('https://www.howsmyssl.com/a/check',verify=False).json()['tls_version'])"

Or Python 3:

python3 -m pip install --upgrade requests
python3 -c "import requests; 
print(requests.get('https://www.howsmyssl.com/a/check',verify=False).json()['tls_version'])"

If you see "TLS 1.2", your interpreter's TLS is up to date. If you see "TLS 1.0" or an error like "tlsv1 alert protocol version", then you must upgrade.

2) The reason Python's TLS implementation is falling behind on macOS is that Python continues to use OpenSSL, which Apple has stopped updating on macOS. In the coming year, the Python Packaging Authority team will investigate porting pip to Apple's own "SecureTransport" library as an alternative to OpenSSL, which would allow old Python interpreters to use modern TLS with pip only. "This is a non-trivial amount of effort," writes Stufft, "I'm not sure it's going to get done."

In the long run, the Python interpreter itself would easily keep up with TLS versions, if it didn't use OpenSSL on platforms like macOS and Windows where OpenSSL is not shipped with the OS. Cory Benfield and Christian Heimes propose to redesign the standard library's TLS interfaces to make it easier to swap OpenSSL with platform-native TLS implementations.

Solution 5

I have faced a similar issue in the docker setup. I used the below package instead of psycopg2

pip install psycopg2-binary
Share:
76,028
Rakesh Kumar
Author by

Rakesh Kumar

Updated on September 13, 2021

Comments

  • Rakesh Kumar
    Rakesh Kumar over 2 years

    I'm using MAC and python version 2.7.14

    Collecting psycopg2
      Could not fetch URL https://pypi.python.org/simple/psycopg2/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:661) - skipping
      Could not find a version that satisfies the requirement psycopg2 (from versions: )
    No matching distribution found for psycopg2
    
  • Aseem
    Aseem about 5 years
    first pip uninstall psycopg2 psycopg2-binary Then pip install psycopg2 . That way dependencies dont get messed up
  • SuperFunkyMonkey
    SuperFunkyMonkey almost 3 years
    According to pypi.org/project/psycopg2-binary "The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources"
  • sta
    sta almost 3 years
    Daniel Lagiň, a link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
  • Paul Bénéteau
    Paul Bénéteau over 2 years
    Working on Apple Silicon too, thanks!