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

3

12

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.

Cinchona answered 29/3, 2021 at 19:21 Comment(2)
For searchability: on my Mac M1 using Bash the error message is slightly different: Killed: 9.Lindsy
This problem exists only for Python 2. For Python 3 virtualenvs work perfectly fine on Macs with Apple M1 chip.Lindsy
C
8

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

Cinchona answered 29/3, 2021 at 19:21 Comment(1)
Not sure why, but just setting the alias worked for me.Subsistent
L
5

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.

Lindsy answered 11/9, 2021 at 14:54 Comment(0)
S
0

use SHELL as x86_64 mode by executing arch -x86_64 $SHELL this comand in the terminal

Sheppard answered 29/7, 2022 at 12:40 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Tove

© 2022 - 2024 — McMap. All rights reserved.