Saturday, February 6, 2021

Installing Python 3.9.1 and psycopg2 on Big Sur 11.1 - Apple M1

 I recently purchased my first Mac.  It is a MacBook Pro 13" M1, great little machine with lots of power.  The work I do is Python, ansible, and other miscellaneous development projects connecting to databases, lately PostgreSQL. I do use a laptop for photography work as well.  The first steps were getting the development environment up and running.  Microsoft VS Code went in smoothly and synced everything once connected to my Microsoft account.

Next was installing Python.  This was interesting as I wanted at least Python 3.8+ and for the M1 it seemed that 3.9.1 was the first real supported release.  The M1 with Big Sur comes with Python 2.7 and you do not want to replace it.

I found two ways to install Python 3.9, one using pyenv and one using brew. I did find that you do not want to mix the two.  I removed both and tired just pyenv by itself and got lots of errors with matplotlib, pandas, and psycopg2.  Most of the solutions I found did not seem to help and maybe I did not do the right things.  So I removed the pyenv install and went the brew install of Python 3.9.1 and here are the steps I used to get them the modules I wanted installed and working.


First installing Python 3.9.1

First thing I noticed with this installation is that everything ends up in /opt/homebrew and you will need to add directories to your path.

In my case homebrew was installed in /opt/homebrew by default. I followed the instructions on the Brew Homepage. Update your .zshrc to update the PATH on terminal startup.

export PATH="/opt/homebrew/bin:$PATH" 
Next install openssl and Python 3.9

$ brew install openssl
$ brew install python@3.9

I had number of modules that had issues getting installed, particularly dependencies for matplotlib and the next steps got numpy, pandas, and matplotlib installed without issues.

$ brew install openblas

$ OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install cython --no-use-pep517

$ OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install numpy --no-use-pep517

$ OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install pandas --no-use-pep517

$ OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install pybind11 --no-use-pep517

$ OPENBLAS="$(brew --prefix openblas)" MACOSX_DEPLOYMENT_TARGET=11.1 python3 -m pip install scipy --no-use-pep517

$ brew install libjpeg zlib

$ python3 -m pip install pillow

$ python3 -m pip install matplotlib

Next I installed PostgreSQL 12.5
   
$ brew install postgresql@12
export PATH="/opt/homebrew/opt/postgresql@12/bin:$PATH" 


Validate pip3 is being used from /opt/homebrew/bin/pip3
   
$ which pip3
Next set the following flags to install psycopg2-binary
   
export LDFLAGS="-L/opt/homebrew/opt/openssl/lib"
export CPPFLAGS="-L/opt/homebrew/opt/openssl/include"

Install psycopg2-binary using pip3
   
$ pip3 install psycopg2-binary

I found that I needed to ensure the python script has at the top a call to use python3
   
#!/usr/bin/env python3

2 comments:

  1. Hello, I really need your help. I'm using macOS Big SUR version 11.2.1, Python 3.8.2, Postgresql 13, with Django. I’m trying to connect Postgresql with Django in a virtual environment. I’m unable to resolve the below error. Any help would be much appreciated.

    ImproperlyConfigured: Error loading psycopg2 module: dl
    open(/Users/inqline/Library/Python/3.8/lib/python/site-packages/psycopg2/_psyc
    opg.cpython-38-darwin.so, 2): Library not loaded: @rpath/libssl.1.1.dylib


    Referenced from: /Users/inqline/Library/Python/3.8/lib/python/site-packages/
    psycopg2/_psycopg.cpython-38-darwin.so
    Reason: image not found

    ReplyDelete
  2. It looks like the psycopg2 module is not find the package in your path. Based on what I am seeing, you may have installed this to a different directory that is not on the path that python is searching for the package. It might be your PATH or LIBPATH.

    ReplyDelete