How to run Python as X86 with Rosetta2 on ARM MacOS machine

13.9k views Asked by At

I have a python app with downstream dependencies on dynamic libraries that are available as X86 only.

The app runs on a X86 MacOS machine, but on a ARM MacOS machine it fails with an ImportError.

I've run lipo -archs on the libraries and they are x86_64 only. I have Python running in a virtualenv and it is a universal binary x86_64 arm64. The intermediary object file built by the application when it installs is also a universal binary x86_64 arm64.

I suspect that Python is being run native as an ARM app, but because of the dependencies I need it run as an X86 app.

Is there a MacOS or Rosetta2 option or environmental setting that I can use that would force the X86 Python binary to be executed as opposed to the ARM binary?

2

There are 2 answers

0
chughts On BEST ANSWER

Looks like the only way to do this is to install a X86 version of python.

I found a how to guide here - https://towardsdatascience.com/how-to-use-manage-multiple-python-versions-on-an-apple-silicon-m1-mac-d69ee6ed0250

but couldn't quite get the pyenv build part to work.

So in the Rosetta i386 terminal I brew86 installed python. This put a X86 version of python into /usr/local/bin/python3 from which I was able to create a X86 only virtualenv.

Broadly the steps are from the above link (minus the pyenv parts):

  1. Install Rosetta
  2. Create a Rosetta terminal
  3. Install X86 homebrew in the Rosetta terminal
  4. Create an alias for the X86 homebrew in /usr/local/bin/brew
  5. Use the X86 brew to install X86 python (ends up /usr/local/bin/python3)
  6. Create a virtualenv based on the X86 python path
  7. pip install
0
jkitchen On

Conda can create x86 environments on Mac Arm.

  1. Install miniconda
  2. Create an environment configured for x86
    conda create -n my_x86_env -y
    conda activate my_x86_env
    conda config --env --set subdir osx-64
    
  3. Install whichever version of Python you want
    conda install python=3.10
    
  4. Install packages using conda or pip
    conda install numpy
    pip install networkx
    

For this environment (my_x86_env), python and pip are x86 versions so they will only grab x86 versions from anaconda, conda-forge, and pypi. Any environments you create without running conda config --env --set subdir osx-64 will be arm64 environments. So you can get the native speed when you want it and the compatibility with x86-only packages when you need it.