zoukankan      html  css  js  c++  java
  • Compile Python 3.10 for M1 Chip MacBook Pro

    The brand new 2020 MacBook Pro comes with an ARMv8 based 64-bit chip called M1.

    It comes with a compiler clang version 12. Here we are going to build Python 3 for ARM64 M1 MacBook Pro.

    If you don't want to simple use the binaries pre-built from python.org, you can follow this.

    NOTICE THIS IS NOT UNIVERSAL BUILDING WHICH SUPPORTS BOTH INTEL AND ARM CPUs, ONLY FOR ARM (Saves more space and uses less memory!)

    Though Apple claimed that their Universal building is for 2 Architectures but it sacrifices for a bigger storage on your disk and it has an indirect detection for binary interpreter. Because we just want it be smaller and run faster!!!

    Step 1. Get source codes

    Download Python source codes from https://www.python.org/downloads/source/ 

    Get a latest version.

    Then extract to somewhere on your Mac, let's say it's 

    cd /User/someone/Python-3.10.0a4

    Step 2. Get openssl libs

    This step is important because we need to use that important module in the Python built.

    Here I used the pre-compiled openssl version 1.1.1 binaries from homebrew. I installed homebrew in the folder 

    /opt/homebrew
    

    Simply install the binaries using 

    brew install openssl

    Step 3. Compile the Python binaries

    You might face lots of troubles with the header files and dynamic libs from LDFLAGS, LD_LIBRARY, CPPFLAGS, don't worry becasue here's a simple solution which works. Just leave them alone.

    I wrote a shell file called auto_configure.sh which has

    ./configure --enable-optimizations --with-openssl=/opt/homebrew/Cellar/openssl@1.1/1.1.1i --prefix=/opt/python-3.10.0a4-arm64

    It will automatically configure the python with the pre-built openssl.  And it will install the the Python built to /opt/python-3.10.0a4-arm64

    Step 4. Built and install

    make -j8
    make install

    Step 5. Test it

    cd /opt/python-3.10.0a4-arm64/bin
    file python3.10
    # You will see 
    # python3.10: Mach-O 64-bit executable arm64

    # Get into Python interpreter
    /opt/python-3.10.0a4-arm64/bin/python3
    >>> import platform
    >>> print(platform.platform())

    macOS-11.1-arm64-arm-64bit

    Now you can use Python for ARM64 on M1 purely.

    FYI (For your information)

    I use Virtual Environment when developing python apps on M1 mac

    python3 -m venv my_env
    # enable the env
    . my_env/bin/activate
    (my_env) ~ cd some_third_libs_for_python
    (my_env) ~ ./configure && make && make install 

    This will not mess around with your main python built. Libs will be only installed in your Python virtual environment. I compiled psycogp2 which let python talk to Posgresql servers in this way. Because at the time of writting, there aren't many 3rd-party libs pre-built for MacOS ARM64 from python pip and homebrew. We need to do it manually. Or just use the rosetta 2 with x86 python interpreter.

    Other thoughts

    Building universal apps for MacOS is not a good idea because it makes your app bigger be a little slower theoretically. But this is the trend which Apple is moving to. The benifit is that developers can make their apps easier no need to compile twice which was ironically implenmented in the Clang compiler in the latest MacOS 11. This means the compiler indeed compiles twice. And the latest MacOS is getting bigger than 8GB. Some system apps from MacOS is still running using Rosetta 2 those are still built for x86 architecture. 

    Maybe 10 years later, there will be non x86 MacOS, and those old macs will never be supported. But think about that it is kind of possible to run MacOS on Raspberry Pi which Apple will never want you to do with M2 encrypt chip and so on.

    For those who want to get rid of the kidnapping by Apple might just use GCC as your main compiler.

    Path for freedom

    Use Clang compile GCC -> use GCC compile all kinds binaries for any platform.

    Humans civilization can never be kidnapped.

  • 相关阅读:
    树莓派使用记录 修改国内软件源《二》
    树莓派使用记录 安装系统《一》
    C# 委托 Action 实现代码执行时间日志记录
    微软 Visual Studio 离线下载
    项目框架搭建工具
    WebApi 重写 DefaultHttpControllerSelector 实现路由重定向
    开发相关网页收藏
    造SQL语句
    报错:Every derived table must have its own alias
    html
  • 原文地址:https://www.cnblogs.com/spaceship9/p/14394897.html
Copyright © 2011-2022 走看看