简体   繁体   中英

How do I fix the "mach-o, but wrong architecture" error in Pycharm?

I'm importing numpy and matplotlib in a Pycharm.py script on a Mac and I keep getting this error.

/usr/bin/python3 /Users/aksseet/PycharmProjects/Election/electorate.py
/Users/aksseet/PycharmProjects/Election/electorate.py:1: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle 
issues and is discouraged.
  import numpy as np
Traceback (most recent call last):
  File "/Users/aksseet/PycharmProjects/Election/electorate.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/Users/aksseet/Library/Python/3.8/lib/python/site-packages/matplotlib/__init__.py", line 107, in <module>
    from . import _api, cbook, docstring, rcsetup
  File "/Users/aksseet/Library/Python/3.8/lib/python/site-packages/matplotlib/rcsetup.py", line 24, in <module>
    from matplotlib import _api, animation, cbook
  File "/Users/aksseet/Library/Python/3.8/lib/python/site-packages/matplotlib/animation.py", line 34, in <module>
    from PIL import Image
  File "/Users/aksseet/Library/Python/3.8/lib/python/site-packages/PIL/Image.py", line 114, in <module>
    from . import _imaging as core
ImportError: dlopen(/Users/aksseet/Library/Python/3.8/lib/python/site-packages/PIL/_imaging.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/aksseet/Library/Python/3.8/lib/python/site-packages/PIL/_imaging.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/aksseet/Library/Python/3.8/lib/python/site-packages/PIL/_imaging.cpython-38-darwin.so: mach-o, but wrong architecture

I've already tried reinstalling python, numpy, and matplotlib. What should I do?

Full disclaimer: I do not own a Mac, so I don't have any means of testing whether or not this solution will work. However, I'll give it my best go, and hopefully will have at least provided some useful resources for further digging.

From what I'm reading across several GitHub issues citing similar errors, it looks like this is likely an issue with your pip installer grabbing the wrong prebuilt package wheel for your computer and is installing binaries meant for a different machine.

Approach 1

For some macOS users , it looks like this was solved pretty easily by setting the environment variable SYSTEM_VERSION_COMPAT=1 . However, if this doesn't fix the problem, there is a more in-depth approach.

Approach 2

1. Reinstall Python Using the Correct Installer

You mention that you tried reinstalling Python; my first recommendation is to ensure that you've installed the correct version for your processor. I also strongly suggest installing it directly from Python.org as opposed to any third-party package management systems. For an M1 Mac, you appear to need a macOS installation indicated for universal2 : If you're using an Intel or Intel-only installer, that could potentially be the reason that the pip module is grabbing wheel s with the wrong binaries.

2.1. Reinstall Pillow with Automatic wheel Selection

The provided traceback points to an issue with a Pillow shared object file. The most minimal example to replicate the error should be

from PIL import Image

There is an issue thread on the Pillow package's GitHub repo relating to this exact ImportError , the solution to which appeared to be to uninstall and reinstall the package, taking care to make sure you use the same pip module associated with your desired Python installation. You also typically want to make sure your pip module has the latest updates. From how you invoked Python in your example, this would probably be something like:

/usr/bin/python3 -m pip install --upgrade pip
/usr/bin/python3 -m pip uninstall Pillow
/usr/bin/python3 -m pip install Pillow

At this point, the installer should automatically choose, download, and install the correct wheel for your system. However, if you still have issues with it grabbing one with binaries for a different machine, you can override the installer's choice with your own.

2.2. Reinstall Pillow with Manual wheel Selection

To know which wheel to pick, first check your supported system tags :

/usr/bin/python3 -c "from packaging import tags; print('\n'.join([str(t) for t in tags.sys_tags()]))" | head -5

Which, for your system - depending on which OS version, Python version, and processor you have - might have some output like

cp38-cp38-macosx_11_0_universal2
cp38-cp38-macosx_11_0_arm64
cp38-cp38-macosx_11_0_x86_64
cp38-cp38-macosx_10_15_arm64
cp38-cp38-macosx_10_15_x86_64

You can the match this up with the list of Pillow 's prebuilt wheel s on PyPI , download one with a supported system tag, and install it from the local .whl file. For example, if one of your supported tags is cp310-cp310-macosx_11_0_arm64.whl , you might use the following:

/usr/bin/python3 -m pip install --upgrade pip
/usr/bin/python3 -m pip uninstall Pillow
/usr/bin/python3 -m pip install Pillow-9.1.1-cp310-cp310-macosx_11_0_arm64.whl

If needed, you can do the same thing with virtually any other package. Just take care to check if any packages you need which use your newly reinstalled package as a dependency have specific version requirements (eg, how matplotlib requires pillow>=6.2.0 ).

I hope this helps!

The discussion on Pillow on M1 is opened here:

https://github.com/python-pillow/Pillow/issues/5093

And this particular error is mentioned in the thread:

https://github.com/python-pillow/Pillow/issues/5093#issuecomment-748636026

  1. You may need to build from source:

Pillow-8.0.1-cp38-cp38-macosx_11_0_arm64.whl.zip

or

  1. First do brew install libjpeg then do pip install pillow

Option #1

Make sure you are using the right Python version. Run a shell, run the command file $(which python3) and make sure the result on your machine includes an arm64 executable.

Here's the output on my machine:

>>> file $(which python3)
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 (for architecture x86_64):       Mach-O 64-bit executable x86_64
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 (for architecture arm64):        Mach-O 64-bit executable arm64

If the output doesn't include an arm64 executable, go to the official Python downloads page and download one of the universal2 installers (for example, this 3.10.4 installer )

Once you have that Python installed, I would recommend isolating additional possible environmental issues and use a virtual environment.

Run the following commands and see if that works:

python3 -m venv env
. /env/bin/activate
pip install numpy matplotlib
python3 -c "import numpy; import matplotlib"

If the last command doesn't print any errors, you are golden!

Option #2

In case this thing doesn't seem to go away, and you really need a solution ASAP, I would recommend a slightly different approach.

The latest versions of Docker work REALLY WELL on M1 MacBooks. I use them all the time.

You can just install Docker for M1 and run a container with everything.

Create a Dockerfile with the following content:

FROM python:3.10.4-slim-bullseye

WORKDIR /app

ARG USER_NAME=user
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN groupadd --gid $USER_GID $USER_NAME && \
    useradd --create-home --uid $USER_UID --gid $USER_GID $USER_NAME

RUN pip install --no-cache-dir numpy pandas matplotlib

CMD ["/bin/bash"]

Build it using:

docker build -t datascience:latest -f Dockerfile .

And run it using:

docker run -it --rm datascience:latest /bin/bash

When inside the container, Python3 already contains numpy , pandas and matplotlib installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM