At some point since the last time I used it (in the past few months), SQLAlchemy has stopped working on my MacBook Pro. I've boiled down the error to the following
from sqlalchemy import create_engine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/pvandivier/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/pvandivier/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/Users/pvandivier/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 753, in dbapi
File "/Users/pvandivier/.pyenv/versions/3.7.3/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: dlopen(/Users/pvandivier/.pyenv/versions/3.7.3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
Referenced from: /Users/pvandivier/.pyenv/versions/3.7.3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
Reason: image not found
Fair enough - I installed openssl 1.1 since the last time I used SQL Alchemy. Sadly however, removing openssl 1.1 as the default in my PATH (setting the default to LibreSSL 2.8.3) doesn't fix the issue.
I also tried uninstalling and reinstalling SQL Alchemy (via `pip --no-cache-dir`) but this also did not fix the issue.
[StackOverflow suggests](https://stackoverflow.com/a/59184347/4709762) that I should symlink back to openssl 1.0 (`brew switch openssl 1.0.2s`) - but a comment below this advice has proved sadly prophetic
> ...Homebrew will remove packages that are deprecated. Not sure when will this solution stop working – [tom10271](https://stackoverflow.com/users/2736817/tom10271) [Dec 19 '19 at 2:33](https://stackoverflow.com/questions/59006602/dyld-library-not-loaded-usr-local-opt-openssl-lib-libssl-1-0-0-dylib#comment104993968_59184347)
I do not have openssl 1.0 locally [^1] and it no longer appears to be available from Homebrew. Fortunately I can spelunk into homebrew-core and find the deprecated package.
@@@ answer 878
# the following snippet fetches the full homebrew commit history and
# gives us those commits that reference openssl 1.0
# I've saved the cached output as of 2020-10-01 (b291a9e32a) as a gist at the below link
pushd "$(brew --repo homebrew/core)"
git fetch --unshallow
git log --pretty=oneline --abbrev-commit | grep -i openssl | grep '1\.0'
I'm nervous though - I was reticent to highjack the default OpenSSL install back when I did it because I didn't fully understand what all would be affected. And attempting to revert it here doesn't appear to fix this issue! Am I trying to [dig my way out of a hole](https://en.wikipedia.org/wiki/Law_of_holes) with this approach?
As well a closer reading of [the SQL Alchemy docs](https://docs.sqlalchemy.org/en/13/core/engines.html#postgresql) reveals that I can highjack the [default `psycopg` driver](https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#module-sqlalchemy.dialects.postgresql.psycopg2). Manually selecting `pg8000` as a driver does allow my code to run, but is [explicitly not recomended](https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#module-sqlalchemy.dialects.postgresql.pg8000)
> The pg8000 dialect is **not tested as part of SQLAlchemy’s continuous integration** and may have unresolved issues. The recommended PostgreSQL dialect is psycopg2.
from sqlalchemy import create_engine
The `pg8000` engine creates successfully but has not been without issues as a drop-in replacement. Is there a "boxed" solution to installing openssl 1.0 for use only by SQL Alchemy (so I don't break _other_ other things)? Or is setting an alternate driver a better rabbit hole to follow?
[^1]: Which makes me wonder why this was _ever_ working months ago 🤔 Am I perhaps ["_barking up the wrong tree_"](https://www.google.com/search?q=barking+up+the+wrong+tree)?
Frustratingly, I've been unable to re-install `psycopg2`, but I'm guessing that's a problem for another day (given the _beefy_ error message)
![Screenshot 2020-10-01 at 13.20.29.png](/image?hash=e6b8760f386bbaff94d9bbe98c5b51e0215d70db11007e8a33752543fc90960f)
However, upgrading `psycopg2-binary` from 2.8.4 to 2.8.6 appears to have ameliorated the issue for now.
![Screenshot 2020-10-01 at 13.22.08.png](/image?hash=1e05d2c3961acdc203e25e929d82084a7aacd18df647f0c89cd08a93bc04417f)
At least I don't need to perform tomfoolery with deprecated versions of openssl ¯\\\_(ツ)_/¯