Unable to install psycopg2 (pip install psycopg2)
Asked Answered
Y

9

46

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
Yoshi answered 13/4, 2018 at 7:54 Comment(1)
J
95

Try this:

pip install psycopg2-binary
Jacquerie answered 13/4, 2018 at 8:41 Comment(2)
first pip uninstall psycopg2 psycopg2-binary Then pip install psycopg2 . That way dependencies dont get messed upUneducated
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"Hitormiss
T
23

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

Togetherness answered 15/8, 2021 at 15:5 Comment(2)
Working on Apple Silicon too, thanks!Deportee
In my case the path of the installed openssl was /opt/homebrew/Cellar/openssl@3/3.3.0 so make sure to make the adjustments to the environment variable to make it work. Other than that works good on my M1 MacOS!Jetsam
C
10

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

So i ran:

brew install postgresql
brew services start postgresql
Conclusion answered 14/7, 2021 at 8:45 Comment(1)
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.Muumuu
H
6

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.

Heloise answered 13/4, 2018 at 8:10 Comment(0)
G
3

This is and old question, but for what it may be worth.

If you are using the Conda package manager simply run this on a terminal:

conda install psycopg2

Groce answered 21/3, 2022 at 16:41 Comment(0)
I
0

I had this issue, asked and answered succesfully here:

Cannot get psycopg2 to work, but installed correctly. Mac OS

[save you a click]

I have installed anaconda2. The install updated my path to include /anaconda/bin.

Then using the navigator I installed pyscopg2. Now I am able to use this in the shebang and my scripts execute fine and i'm able to import this module.

Gurmokhs-MBP:rest Gurmokh$ python
Python 2.7.12 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import psycopg2
if psycopg2.connect("dbname='postgres' user='postgres' host='localhost'"):
...     print "connection made"
... 
connection made
>>> 
Illaudable answered 13/4, 2018 at 9:30 Comment(0)
E
0

A portable solution to solve this error regardless of the OpenSSL version for Mac users using homebrew

OPENSSL_VERSION=$(brew info openssl --json | jq '.[0].name')

export CPPFLAGS="-I/opt/homebrew/opt/$OPENSSL_VERSION/include"
export LDFLAGS="-L/opt/homebrew/opt/$OPENSSL_VERSION/lib"

pip install psycopg2 # OR psycopg2-binary
Egger answered 2/6, 2022 at 21:19 Comment(0)
C
-1

*I did a trace with Process Monitor. D:\Anaconda3\DLLs_ssl.pyd search for the OpenSSL DLLs but in the wrong/current location! As they are not found the search goes to C:\Windows\System32 where we have the same DLLs, installed by an other application, but with a different version.

The DLLs delivered by Anaconda3 are located here: D:\Anaconda3\Library\bin

Resolution:

My workaround: I have copied the following files

libcrypto-1_1-x64.* libssl-1_1-x64.* from D:\Anaconda3\Library\bin to D:\Anaconda3\DLLs.

Conceit answered 28/8, 2020 at 20:44 Comment(0)
U
-1

Try this :

pip install pgcli==2.1.1 --only-binary psycopg2
Unhinge answered 25/7, 2022 at 9:23 Comment(1)
why would this help? Can you elaborate a bit?Bael

© 2022 - 2024 — McMap. All rights reserved.