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

3

9

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?

Fanchie answered 31/3, 2022 at 11:21 Comment(1)
Have you looked into qemu?Bedlam
F
5

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
Fanchie answered 14/4, 2022 at 14:58 Comment(0)
A
18

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.

Alterant answered 23/3, 2023 at 18:56 Comment(0)
F
5

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
Fanchie answered 14/4, 2022 at 14:58 Comment(0)
A
-1

You can select the desired architecture when executing a universal binary with the arch command.

In terminal write:

arch -x86_64 python -m pip install --user virtualenv
arch -x86_64 python -m venv env
source env/bin/activate
arch -x86_64 python -m pip install numpy

When executing your script or module you have to prepend the python command with arch -x86_64 such as:

arch -x86_64 python my_script.py
Attenweiler answered 16/4 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.