Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/pip process are killed in virtualenv (Apple M1 chip)

When I execute pip or python in MacBook M1 chip virtualenv, the process always terminates immediately with message like:

[1]    29116 killed     pip

or

[1]    29141 killed     python

Python and pip in M1 MacBook native environment run fine.

Only in the virtualenv they don't work.

like image 670
Sean Cheng Avatar asked Mar 29 '21 19:03

Sean Cheng


2 Answers

After some research, I find out it is related with "x86_64" and "arm 64" architecture. To solve the problem, you have to run virtualenv python/pip in x86_64 mode.

Steps:

  1. Disable SIP: reboot MacBook in Recover Mode. Open Utilities > Terminal enter csrutil disable. ref: Disabling and Enabling System Integrity Protection
  2. Run SHELL as x86_64 mode: enter arch -x86_64 $SHELL in Terminal.
  3. Execute python/pip successfully

After that, I enable SIP again and keep using arch -x86_64 $SHELL every time. It will execute python/pip in virtualenv succesfully.

To save my time, I add an alias in my .zshrc:

alias x86="arch -x86_64 $SHELL"

Now if I need execute some process in x86_64 mode, I just type x86 to open the x86_64 shell.

Note: MxJ24 on github provides this solution and lists some useful scripts for python/django/brew: https://gist.github.com/MxJ24/e1386c9012f533cfbedfed5114da3e60

like image 130
Sean Cheng Avatar answered Sep 18 '22 16:09

Sean Cheng


Python 2 virtualenvs are just not working on Macs with Apple Silicon chip as of now. See https://github.com/pypa/virtualenv/issues/2024 for more information.

The workarounds involving disabling System Integrity Protection and using x86_64 emulation may be unsafe and inefficient and I suggest against following them.


The best solution would obviously be to switch/migrate/upgrade to Python 3, for which virtualenv works like a charm.


If that's not a viable option, then I would consider using Docker instead of virtualenv. python:2 Docker image has the latest Python 2.7.18 version and is available on aarch64 architecture, so it will run without x86_64 emulation. I don't know your use case, but I am using IntelliJ IDEA/PyCharm and it supports using Docker as the SDK, so after a one-time setup it's seamless.

like image 22
Greg Dubicki Avatar answered Sep 20 '22 16:09

Greg Dubicki