'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.
Solution 1:[1]
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:
- Disable SIP: reboot MacBook in Recover Mode. Open
Utilities > Terminalentercsrutil disable. ref: Disabling and Enabling System Integrity Protection - Run SHELL as x86_64 mode: enter
arch -x86_64 $SHELLin Terminal. - 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
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sean Cheng |
| Solution 2 |
