pip install psycopg2 on MacOS M1 and python 3.10.5 not working
Install libpq
and openssl
from brew:
brew install libpq
brew install openssl
Check path and compiler flags:
>>> brew info libpq
libpq: stable 14.4 (bottled) [keg-only]
Postgres C API library
https://www.postgresql.org/docs/14/libpq.html
/opt/homebrew/Cellar/libpq/14.4 (2,338 files, 28.3MB)
Poured from bottle on 2022-07-20 at 16:31:04
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/libpq.rb
License: PostgreSQL
==> Dependencies
Required: krb5 ✔, openssl@1.1 ✔
==> Caveats
libpq is keg-only, which means it was not symlinked into /opt/homebrew,
because conflicts with postgres formula.
If you need to have libpq first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
For compilers to find libpq you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
>>> brew info openssl
openssl@3: stable 3.0.5 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/opt/homebrew/Cellar/openssl@3/3.0.5 (6,444 files, 27.9MB)
Poured from bottle on 2022-07-20 at 16:31:21
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
==> Dependencies
Required: ca-certificates ✔
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/opt/homebrew/etc/openssl@3/certs
and run
/opt/homebrew/opt/openssl@3/bin/c_rehash
openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.
If you need to have openssl@3 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl@3 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
Export path
for libpq and export LD/CPPFLAGS
for openssl (taken from output above) and install psycopg2
:
>>> export PATH=/opt/homebrew/opt/libpq/bin:$PATH
>>> export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
>>> export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
>>> python -m pip install psycopg2
Collecting psycopg2
Using cached psycopg2-2.9.3.tar.gz (380 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: psycopg2
Building wheel for psycopg2 (setup.py) ... done
Created wheel for psycopg2: filename=psycopg2-2.9.3-cp310-cp310-macosx_12_0_arm64.whl size=142252 sha256=78ca9fc7ca6752e234904bf38d052937e20b063cb68eb67caa874511207e076e
Stored in directory: /Users/.../Library/Caches/pip/wheels/81/b6/3d/091aad3e8919ea76c84c2674b02ce3ab52de882e091c39249e
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.3
Test:
python
Python 3.10.5 (main, Jul 20 2022, 17:05:05) [Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> c = psycopg2.connect("port=5556 host=localhost ...")
>>> c
<connection object at 0x102d8a500; dsn: 'user=xxx password=xxx dbname=xxx host=localhost port=5556', closed: 0>
>>> exit()
Cannot install psycopg2 Ubuntu
You've installed the python-dev libraries for Python 3. Your psycopg2 install is using python 2.7
If you want to use Python 3:
Make sure you've installed python3-pip
sudo apt-get install python3-pip
Then:
pip3 install psycopg2
If that still fails, try installing the development headers for postgresql:
sudo apt install python3-dev libpq-dev
pip3 install psycopg2
If on Python 2
sudo apt update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev
sudo apt-get install python-dev
sudo apt-get install python-pip
pip2 install psycopg2-binary
If on Ubuntu 20.04 or above
sudo apt-get install build-essential
Then run the other steps for either Python 2 or 3.
Error installing psycopg2 on Mac OS 10.6
According to this link, psycopg2 seems must work in 32bit mode on MacOSX
Can't install psycopg2-binary on macOS Monterey 12.3
psycopg2-binary
2.9.3 doesn't provide binary wheels for MacOS arm64; binary wheels for MacOS are only provided for Intel x86 64 bits. So pip
tried to compile from sources — and failed.
To compile from sources you need a lot of prerequisites.
Related Topics
How to Make a 4D Plot with Matplotlib Using Arbitrary Data
Websocket VS Rest API for Real Time Data
Matplotlib Log Scale Tick Label Number Formatting
Split a List into Parts Based on a Set of Indexes in Python
Check What Files Are Open in Python
How to Decorate an Instance Method with a Decorator Class
Getting Only Element from a Single-Element List in Python
How to Display a Pandas Data Frame with Pyqt5/Pyside2
How to Create an Encrypted Zip File
How to Load Files Using Pickle and Multiple Modules
Pycharm Current Working Directory
Easy Way of Finding Decimal Places
Link Atlas/Mkl to an Installed Numpy
Get the Name of a Pandas Dataframe