psycopg / psycopg2

PostgreSQL database adapter for the Python programming language
https://www.psycopg.org/
Other
3.36k stars 505 forks source link

(Django)(MAC Big Sur 11.0.1) ImportError: dlopen #1216

Closed awildjosh closed 2 years ago

awildjosh commented 3 years ago

Apologies in advance -- this is probably doubling up on #1200 and #1208, but I'm curious as to whether there's any workaround whatsoever (as I really would like to use postgresql with my project, but I'm too much of a newb to comprehend most of the issues I run into). I've tried pretty much every suggestion to be found through googling to no avail. The issue is as below:

Installed pyscopg2 to the venv without any issues using: pip install psycopg2

Meanwhile, Postgresql was successfully installed and the db I'd like to connect to is active (using pgadmin4). Django settings are configured accordingly.

However, when trying to access psycopg2 through the terminal, whether it be through: python -c "import psycopg2" or python manage.py migrate, etc.

The below error occurs: (venv) ...-MacBook-Air project % python -c "import psycopg2" Traceback (most recent call last): File "<string>", line 1, in <module> File "/Users/.../Documents/project/venv/lib/python3.9/site-packages/psycopg2/__init__.py", line 51, in <module> from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/.../Documents/project/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): no suitable image found. Did find: /Users/.../Documents/project/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so: mach-o, but wrong architecture /Users/.../Documents/project/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so: mach-o, but wrong architecture

Any help/insight would be greatly appreciated. Or are all of us who jumped ship to M1 doomed until Apple stop breaking things? :)

leohakim commented 3 years ago

Second this. Unable to use natively under M1. I am forced to use under Intel Translation / Rosetta 2.

Psycopg3 also does not compile natively...

How can you do that??

Thank you

bschollnick commented 3 years ago

Under Big Sur 11.0, 11.1, if you do a get info on the terminal window, there is a check box to run under Rosetta 2. So what you do is duplicate the terminal, rename it (eg Intel Terminal), and have that “checked” with the Rosetta 2 check box.

With those versions, that would restrict anything run in the terminal, to run under rosetta 2.

With 11.2x, something has changed, which means that the applications seem to be checked to see if they are M1 native, and if so, it’s as if another process is spawned and it’ll run as M1 native, even though it was in a rosetta 2 terminal.

What I ended up doing is revising my profile script to include:

https://cutecoder.org/software/detecting-apple-silicon-shell-script/

arch_name="$(uname -m)"

if [ "${arch_name}" = "x86_64" ]; then if [ "$(sysctl -in sysctl.proc_translated)" = "1" ]; then echo "Running on Rosetta 2" ARM=0 else echo "Running on native Intel" ARM=0 fi elif [ "${arch_name}" = "arm64" ]; then echo "Running on ARM" ARM=1 else echo "Unknown architecture: ${arch_name}" fi echo "Arm Status: $ARM, $arch_name"

Shellscript updates, imports from .bash_profile

set PYTHONIOENCODING=UTF-8

export PATH=".:$PATH"

brew path

if [ $ARM -eq 0 ]; then

intel

echo "Rosetta2"
# homebrew
export PATH="/opt/local/bin:$PATH"
# homebrew python
export PATH="/usr/local/opt/python@3.9/libexec/bin:$PATH"
export PATH="/usr/local/lib/python3.9/site-packages:$PATH"
export PLAT="ROSETTA2"
alias brew='arch -x86_64 /usr/local/bin/brew'
alias virtualenv='arch -x86_64 python -m virtualenv ~/venvs/%*'
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"

else echo "M1 Native!"

Homebrew

export PATH="/opt/homebrew/bin:$PATH"
# Homebrew - Python
export PATH="/opt/homebrew/opt/python@3.9/libexec/bin:$PATH"
export PATH="/opt/homebrew/lib/python3.9/site-packages:$PATH"
# OpenSSH
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
export PLAT="M1"
alias brew='arch -arm64e /opt/homebrew/bin/brew'
alias virtualenv='arch -arm64e python -m virtualenv ~/venvs/$*'

fi

On Feb 20, 2021, at 12:08 PM, Leonardo Ismael Hakim notifications@github.com wrote:

Second this. Unable to use natively under M1. I am forced to use under Intel Translation / Rosetta 2.

Psycopg3 also does not compile natively...

How can you do that??

Thank you

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/psycopg/psycopg2/issues/1216#issuecomment-782716318, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHPEZQWOU2H3N3WQDGT2X3S77UBRANCNFSM4VS5GJ4Q.

ezeagwulae commented 3 years ago

having a similar issue connecting to a Postgres server through psycopg2 on a Flask app (sqlalchemy)

I'm running:

File "/Users/ezeagwulae/taco-sounds/venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 778, in dbapi
    import psycopg2
  File "/Users/ezeagwulae/taco-sounds/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/ezeagwulae/taco-sounds/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/ezeagwulae/taco-sounds/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/ezeagwulae/taco-sounds/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so: mach-o, but wrong architecture

driving me mad with more than enough time spent on this issue. also tried with psycopg2-binary.

Jfeng3 commented 3 years ago

same issue here. any update?

asuraphel commented 3 years ago

Enormous thanks to the psycopg team! I ran into this problem with a Rosetta-based install of Postgres.app and an Apple Silicon install of python3.9.

Installing libpq from source got things working again.

` $ brew install libpq --build-from-source

$ export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"

$ pip install psycopg2 `

Cheers, Ryan

This worked for me too. Thanks!

HIRANO-Satoshi commented 3 years ago

All, Don't forget --no-cache-dir!!

pip install psycopg2 --no-cache-dir

kpowelltech commented 3 years ago

Hello folks - I've been on this for hours now to no avail. I've tried pretty much all the suggestions here, but cannot connect to postgresql from my terminal. Strange thing is, I am able to connect from another application that was created a few days ago.

Running:

This is my error

.django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/User/Desktop/sei/servers/instructor_notes/django3/.env/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so, 2): no suitable image found. Did find: /Users/User/Desktop/sei/servers/instructor_notes/django3/.env/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so: mach-o, but wrong architecture /Users/user/Desktop/sei/servers/instructor_notes/django3/.env/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so: mach-o, but wrong architecture

jackstine commented 3 years ago

This worked for me If you need to change your python path, please do it using `source ///bin/activate

  1. make sure that the current architecture is arm64, to do this you can set env /usr/bin/arch -arm64 /bin/zsh --login brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" pip install psycopg2-binary Make sure to uninstall psycopg2 and psycopg2-binary to do a new install
dvarrazzo commented 3 years ago

@jackstine if you need the libpq, it looks like you are installing psycopg2 from source anyway

Could you please try if the packages I've uploaded yesterday on TestPyPI work? They should work without setting LDFLAGS and and ideally they shouldn't require the libpq (that's what the binary package should be for).

pip install -i https://test.pypi.org/simple psycopg2-binary==2.9.0.dev0
jackstine commented 3 years ago

That worked too, thanks

mathcoder3141 commented 3 years ago

@dvarrazzo Still getting Symbol not found: _PQbackendPID after trying that package uploaded to TestPyPI. Any other ideas?

I'm having to do something for work and I cannot (I don't think) use my personal laptop for this.

junctionapps commented 3 years ago

Having toyed with this for an hour or so, my path to success went something like:

Python 3.9.5 psycopg2-binary==2.8.6

I've not started the pg instance from the brew install. Verified that the server running through the postgresapp is housing the new tables created by migration. It appears Python is using pg from homebrew to connect to the postgresapp running instance. Good enough for me to keep moving forward with "I'll try mac for a month".

zarinpy commented 3 years ago

Hi there I have the same issue when I want to migrate the database

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/omid/workspace/.envs/phoenix/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so, 2): no suitable image found. Did find: /Users/omid/workspace/.envs/phoenix/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so: mach-o, but wrong architecture /Users/omid/workspace/.envs/phoenix/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so: mach-o, but wrong architecture

with Mac BugSur 11.4 M1 Python3.8 by pyenv and installed psycopg2-binary==2.9.1

FedericoGregori commented 3 years ago

I fixed the same problem in Mac BigSur 11.5.1 M1 following the tips of this thread in the next order:

$ pip3 uninstall psycopg2 $ brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" # Add to .zshrc $ pip3 install psycopg2 --no-cache-dir

Thanks to everyone who helped.

ghost commented 3 years ago

I'm still having this issue: >>> import psycopg2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/bartek/testenv2/lib/python3.9/site-packages/psycopg2/__init__.py", line 51, in <module> from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/bartek/testenv2/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): Symbol not found: _PQbackendPID Referenced from: /Users/bartek/testenv2/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so Expected in: flat namespace in /Users/bartek/testenv2/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so

My setup: macOS 11.5.1, M1 python 3.9.6 [pyenv] using virtualenv

What I have tried: creating new virtualenv with 3.9.2 and 3.9.6 to do following: pip install psycopg2-binary --no-cache-dir python import psycopg2

also: brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" # Add to .zshrc $ pip3 install psycopg2 --no-cache-dir

not sure if this is related since it's an import issue: I have Postgress.app installed, but I have not used in long time and it's not running. I use Docker for Postgress.

UPDATE: What solved the issue for me is: brew install postgresql create new virtualenv pip install psycopg2-binary --no-cache-dir

abhishekjain-fin commented 3 years ago

pyscopg2-binary needs to be installed in mac instead of psycopg2

pip uninstall psycopg2 pip install psycopg2-binary

samrxth commented 3 years ago

If anyone is struggling to make it work, make sure your terminal application supports M1 natively(the inbuilt terminal or iterm2 support m1 without rosetta)

peter-feeney commented 3 years ago

pip install psycopg2-binary solved the issue for me.

nicksergeant commented 2 years ago

@samrathchadha thank you so much for this:

https://github.com/psycopg/psycopg2/issues/1216#issuecomment-928521351

I'm using Alacritty and its ARM support is busted, so I had to install via x86 🤦.

samrxth commented 2 years ago

No problem @nicksergeant I was using alacritty too before I tried installing it in iterm2

lyntree commented 2 years ago

I fixed the same problem in Mac BigSur 11.5.1 M1 following the tips of this thread in the next order:

$ pip3 uninstall psycopg2 $ brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" # Add to .zshrc $ pip3 install psycopg2 --no-cache-dir

Thanks to everyone who helped.

Thank you for this, I just discovered this issue yesterday with Django, I gave it ago and and restarted my terminal and sure enough, at last started working :)

EtienneLeconte-Stuart commented 2 years ago

Hi there, I face the same issue as some of you guys. And after trying a lot of different solutions you proposed, I'm still faced with this error when trying to create an engine:

ImportError: dlopen(/opt/homebrew/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): no suitable image found. Did find: /opt/homebrew/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so: mach-o, but wrong architecture /opt/homebrew/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so: mach-o, but wrong architecture

I'm on Big Sur 11.0 M1 and using Pycharm as my IDE.

Any tip would be greatly appreciated!

lyntree commented 2 years ago

@EtienneLeconte-Stuart try following @bartfto post below as that did work for me and I was using Big Sur 11.0 M1 and got this fixed just 4 days ago :) / Also might I also add, What I also found with this is - There is another version of Python3 which is supported for 'universal' which is made for M1 (which i didn't know either)

''

My setup: macOS 11.5.1, M1 python 3.9.6 [pyenv] using virtualenv

What I have tried: creating new virtualenv with 3.9.2 and 3.9.6 to do following: pip install psycopg2-binary --no-cache-dir python import psycopg2

also: brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" # Add to .zshrc $ pip3 install psycopg2 --no-cache-dir

not sure if this is related since it's an import issue: I have Postgress.app installed, but I have not used in long time and it's not running. I use Docker for Postgress.

UPDATE: What solved the issue for me is: brew install postgresql create new virtualenv pip install psycopg2-binary --no-cache-dir''

EtienneLeconte-Stuart commented 2 years ago

@lyntree thanks a lot for your answer! I tried the proposed solution and unfortunately that did nothing for me... I'm still at the same point.

Something must definitely be wrong with my configuration, but I struggle to get what.

lyndseyjw commented 2 years ago

thank you @lyntree & everyone else above! after a longgggg time attempting most everything above, the key for me was "create new virtualenv"

I am running MacOS Big Sur Version 11.3.1, M1

I deleted the old venv I was working with & did the following: in root folder ran: pip3 uninstall psycopg2 pip3 install psycopg2 --no-cache-dir brew install postgresql created new virtualenv & in project folder ran: pip3 install psycopg2-binary --no-cache-dir

the issue was magically solved

good luck everyone else, this is definitely a frustrating error!

lyntree commented 2 years ago

Thats brilliant to hear, I don't always use the create a new env as I don't find its useful to me much but then again, sometimes it works :)

Glad to hear you got it working in end :)

On Mon, 17 Jan 2022 at 17:25, Lyndsey @.***> wrote:

thank you @lyntree https://github.com/lyntree & everyone else above! after a longgggg time attempting most everything above, the key for me was "create new virtualenv"

I am running MacOS Big Sur Version 11.3.1, M1

I deleted the old venv I was working with & did the following: in root folder ran brew install postgresql created new virtualenv in project folder ran pip3 install psycopg2-binary --no-cache-dir''

the issue was magically solved .. I also ran pip3 uninstall psycopg2 previously so this may have been key as well

good luck everyone else, this is definitely a frustrating error!

— Reply to this email directly, view it on GitHub https://github.com/psycopg/psycopg2/issues/1216#issuecomment-1014758505, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQ7KPVNUCLGTXHBGRLP6J3DUWRGKDANCNFSM4VS5GJ4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

--

Bridget

Kindest Regards,

Lyntree Web Studio

Website Design | Website Hosting | Mobile Applications

Full Stack App|Web|Mobile Developer

Website: https://lyntree.co.uk | https://lyntree.io tbc

Phone / WhatsApp: 07761 308429

Email: @. / @.

winny1212 commented 2 years ago

@nicksergeant @Jfeng3 @dvarrazzo @kevbradwick @leohakim Sorry, I am totally new here, a bit confused about to make my texts following looks good.

I am having a similar issue connecting to a Postgres server through psycopg2 , please please help me have a look if anyone is free, thank you very much. I have tried to uninstall and reinstall, change the env installation method like the above solution but it doesn't work at all , the error is the same

I'm running:

Mac Big Sur 11.2 Python 3.10

when I running "python manage.py makemigrations", the errors accur as follows:

Traceback (most recent call last): File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 25, in import psycopg2 as Database File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/init.py", line 51, in from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 2): Symbol not found: _PQbackendPID Referenced from: /Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so Expected in: flat namespace in /Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so

During handling of the above exception, another exception occurred:

_Traceback (most recent call last): File "/Users/yujingchen/Desktop/real-estate/backend/manage.py", line 22, in main() File "/Users/yujingchen/Desktop/real-estate/backend/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/core/management/init.py", line 425, in execute_from_command_line utility.execute() File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/core/management/init.py", line 401, in execute django.setup() File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/apps/registry.py", line 114, in populate app_config.import_models() File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/apps/config.py", line 300, in import_models self.models_module = import_module(models_module_name) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1050, in _gcd_import File "", line 1027, in _find_and_load File "", line 1006, in _find_and_load_unlocked File "", line 688, in _load_unlocked File "", line 883, in exec_module File "", line 241, in _call_with_frames_removed File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 47, in class AbstractBaseUser(models.Model): File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/models/base.py", line 122, in new new_class.add_to_class('_meta', Options(meta, app_label)) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/models/base.py", line 326, in add_to_class value.contribute_to_class(cls, name) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/models/options.py", line 207, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/utils/connection.py", line 15, in getattr return getattr(self._connections[self._alias], item) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/utils/connection.py", line 62, in getitem conn = self.create_connection(alias) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/utils.py", line 204, in create_connection backend = load_backend(db['ENGINE']) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/utils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 29, in raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 2): Symbol not found: _PQbackendPID Referenced from: /Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so Expected in: flat namespace in /Users/yujingchen/Desktop/real-estate/backend/venv/lib/python3.10/site-packages/psycopg2/psycopg.cpython-310-darwin.so

Danitocode commented 2 years ago

This worked for me If you need to change your python path, please do it using `source ///bin/activate

  1. make sure that the current architecture is arm64, to do this you can set env /usr/bin/arch -arm64 /bin/zsh --login brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" pip install psycopg2-binary Make sure to uninstall psycopg2 and psycopg2-binary to do a new install

You are the best man, you saved my life.

m-hollow commented 2 years ago

@winny1212 -- were you able to resolve the issue you posted? I got the same error today, and there are so many different proposed 'solutions' I'm not sure where to start; my error output is identical to yours, and occurs for the same reason -- when I run python manage.py makemigrations. thanks!

Danitocode commented 2 years ago

@winny1212 -- were you able to resolve the issue you posted? I got the same error today, and there are so many different proposed 'solutions' I'm not sure where to start; my error output is identical to yours, and occurs for the same reason -- when I run python manage.py makemigrations. thanks!

Maybe you can try with this as is solving the architecture issue with Macs M1 but maybe you already have tried. You have to run this commands with Rosseta terminal, just that's it, almost for me:

env /usr/bin/arch -arm64 /bin/zsh --login brew install libpq --build-from-source export LDFLAGS="-L/opt/homebrew/opt/libpq/lib" pip install psycopg2-binary

benthorner commented 2 years ago

A slightly shorter version worked for me:

brew install libpq
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
pip install --force psycopg2-binary

I think this issue is related to https://github.com/psycopg/psycopg2/issues/1286 and may possibly be fixed by https://github.com/psycopg/psycopg2/pull/1348.

ArhanChaudhary commented 2 years ago

None of the above solutions worked for me, so I tinkered around and got this to work:

env /usr/bin/arch -arm64 pip uninstall psycopg2-binary
env /usr/bin/arch -arm64 pip install psycopg2-binary --no-cache-dir
env /usr/bin/arch -arm64 pip uninstall psycopg2
env /usr/bin/arch -arm64 pip install psycopg2 --no-cache-dir

Cheers!

holdenweb commented 2 years ago

An additional data point: I found that my m1 Mac was installing an x86_64 version of any extension packages (not pure Python), in my case apsw-wheels,lxml and psycopg2-binary. A typical error (reformatted with additional whitespace) was

ImportError: dlopen(/private/tmp/tmpTamato/lib/python3.9/site-packages/apsw.cpython-39-darwin.so, 0x0002):
    tried: '/private/tmp/tmpTamato/lib/python3.9/site-packages/apsw.cpython-39-darwin.so' 
    (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')),
    '/usr/local/lib/apsw.cpython-39-darwin.so' (no such file),
    '/usr/lib/apsw.cpython-39-darwin.so' (no such file)

I eventually discovered by trial and error how to overcome this, which was to uninstall with pip uninstall and then reinstall with (for example)

ARCHFLAGS="-arch arm64" pip install lxml --compile --no-cache-dir --no-binary :all:

This tells pip not to use any wheels with binary content (i.e., compile from the source distribution), and not to trust any code downloaded earlier but always seek fresh copies. Adding ARCHFLAGS to the process's environment forces compilation of arm binaries. After that everything loaded and ran without complaint.

Having discovered this I investigated why the standard process was producing x86 binaries. Examining the value of ARCHFLAGS in a brand new shell to my horror showed me this:

sholden@overhead ~ % echo $ARCHFLAGS
-arch x86_64

This was a setting I tracked down to my .zshrc, but I have no memory of that line so I'm assuming it was an Apple precautionary measure I didn't notice when I migrated from bash to zsh back before the m1 days, then pulled across thoughtlessly to the m1.

Mea culpa for not checking more carefully when I migrated (usual story, did it in a rush while busy with other things). I've removed the line from .zshrc, and a standard build in a clean shell now runs happily. So the moral of this story is:

"Check your ARCHFLAGS setting."
denangeles commented 2 years ago

Thanks for the info above, was encountering this after upgrading from python3.8 to python3.10 and installing psycopg2-binary. My machine is OSX Big Sur 12.3.1, M1 Pro.

My project uses pipenv so just needed to convert these pip args: --compile --no-cache-dir --no-binary :all:

into the following environment variables: PIP_COMPILE=True PIP_NO_CACHE_DIR=True PIP_NO_BINARY=:all

Reinstalled psycopg2-binary and everything was working again.

HardyNapses commented 2 years ago

None of the above solutions worked for me, so I tinkered around and got this to work:

env /usr/bin/arch -arm64 pip uninstall psycopg2-binary
env /usr/bin/arch -arm64 pip install psycopg2-binary --no-cache-dir
env /usr/bin/arch -arm64 pip uninstall psycopg2
env /usr/bin/arch -arm64 pip install psycopg2 --no-cache-dir

Cheers!

Worked fine for me and I guess this is proper solution !!!

io-ma commented 2 years ago

@HardyNapses thank you, this fixed it for me!

dvarrazzo commented 2 years ago

Assuming this was fixed with the release of M1 binary packages and can be fixed.

PABourdais commented 1 year ago

None of the above solutions worked for me, so I tinkered around and got this to work:

env /usr/bin/arch -arm64 pip uninstall psycopg2-binary
env /usr/bin/arch -arm64 pip install psycopg2-binary --no-cache-dir
env /usr/bin/arch -arm64 pip uninstall psycopg2
env /usr/bin/arch -arm64 pip install psycopg2 --no-cache-dir

Cheers!

Worked for me, thank you ♥️

dvarrazzo commented 1 year ago

Closing the conversation because the ticket is solve and only attracting metoo replies.

(on a nonsense answer, but hey, whatever floats your macOS).