Search code examples
pythonmacospipvirtualenvapple-m1

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

  • 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