Issue Fixed: Azure Function on Mac M1 Chip using Python

Mars Wang
4 min readMay 5, 2023

--

Photo by Ales Nesetril on Unsplash

As a MacBook M1 user, you probably ran into this kind of situation that your Mac can’t be run on specific software/framework of specific programming languages, for example, Azure Function Core Tools right here is one of the library doesn’t support Python function development on ARM64 devices, and this is the reason why you click inside of this story to find the solution I got.

Reproduce The Issue I Got

  • Chip: Apple M1 Pro (ARM64)
  • OS Version: MacOS Ventura (13.3.1(a))
  • Python Version: 3.9.16
  1. Create virtual environment and pip install requirements.txt:
python3.9 -m venv .venv # I got multiple python version, so setting alias for each Python version

#=====activate virtual environment====
source ./.venv/bin/activate

pip install -r requirements.txt # install all packages

2. Run “func host start” to start the Azure Function on localhost. Boom! I got the first error said:
Microsoft.Azure.WebJobs.Script: Architecture Arm64 is not supported for language python.”

ARM64 is not supported for Azure Function

Once I fixed the architecture issue, YES!

Another error came out said:
Import Error: ….. “/usr/local/Cellar/azure-functions-core-tools@4/4.0.5148/workers/python/3.9/05X/X64/grpc/_cython/cygrpc.cpython-39-darwin.so (mach-o file, but is an incompatible architecture (have ‘x86_64”, need ‘arm64’))

In a nutshell, package should use x86_64 version instead of arm64.

Pretty bummed… I followed all steps from official doc. What’s really happened??? Let’s find out.

mood…

Solution

If you try to find official solution from Microsoft, please take this link as reference.

Setup Before Running Azure Function

Before we get started, I have 2 tools to introduce first. Rosetta, which is an x86 emulation on ARM64. Besides, you probably know “Homebrew,” which is a package manager for MacOS or Linux. Here is the point !! Homebrew will be installed in different path in your Mac when you change the architecture behind, and the packages you installed from Homebrew will be stored into different folders under the architecture that Mac was running.

  • i386(x86): /usr/local/bin/brew
  • ARM64: /opt/homebrew/bin/brew

In my cases, it isn’t enough to just open using Rosetta. I got to set alias and revised my .zshrc as well.

Step1: Reset your zsh or bash configuration file

# In my ~/.zshrc [~/.bashrc works too]
...
export PATH=/opt/homebrew/bin:$PATH
alias intel="/usr/bin/arch -x86_64 /bin/zsh"
alias arm="/usr/bin/arch -arm64 /bin/zsh"
if [ $(arch) = "i386" ]; then
alias python="/usr/local/bin/python3.9" # --> set python to specific version (3.9)
alias brew86='/usr/local/bin/brew'
alias pyenv86="arch -x86_64 pyenv"
alias func="/usr/local/bin/func"
fi
...

Once you updated your ~/.zshrc, you could either close and reopen the terminal or activate the new ~/.zshrc as below without closing the terminal:

source ~/.zshrc

This step can fix the first issue, which is ARM64 not supported for Azure Function. The configuration setting helps us to switch the architecture and the package coupled with the architecture using just one command.

Try type: “arch” & “intel” & “arm“ in your terminal, and try “which python” & “which brew86” & “which func” to see the path all these commands linked to.

arch # show the current architecture you use

arm # switch back to arm64

intel # switch to x86_64 simulation

which python # show the path of python located at -> /usr/local/bin/python3.9 (under "intel")

which func # /usr/local/bin/func (under "intel")

Step2: Delete Your Virtual Environment!

You heard me! If you run “func host start” in your Azure Function, and the error came out like the second error as mine. You got a big chance you installed all the packages under ARM64 architecture, and that is the reason you can’t run your Python code normally even if you Rubber duck debugging again and again.

Switching the architecture to x86_64, and recreating your virtual environment and pip install all modules in your requirements.txt again.

arch # check whether your environment is under x86(i386)

intel # switch to x86

python -m venv .venv # create virtual environment under x86 architecture

source ./.venv/bin/activate # activate your virtual environment

pip install -r requirements.txt # install all modules you need

func host start # run azure function on localhost

Finally

Congrat! You’ve done.That’s all the job you need to do. Now, welcome to join Macbook M1 chip family and happy use Azure Function.

Reference: Microsoft.Azure.WebJobs.Script: Architecture Arm64 is not supported for language python (issue on Mac M1 Chip) — Stack Overflow

Welcome to leave a comment or give me a clap if you think this post is helpful. Thanks :)

My Github: MarsWangyang (Mars Wang) (github.com)

My LinkedIn: Meng-Yang (Mars) Wang | LinkedIn

--

--

Mars Wang
Mars Wang

Written by Mars Wang

99' | Software Development | Cloud | Adventurer

No responses yet