azure-functions-core-tools
azure-functions-core-tools copied to clipboard
Python 3.12 deployment fails due to outdated distlib (0.3.0) using removed modules
Version
4.0.7030
Description
When attempting to deploy a Python function app using Python 3.12, the deployment fails because the Azure Functions Core Tools uses distlib version 0.3.0, which imports modules that were removed in Python 3.12 (imp and distutils).
The error occurs during the func pack command when creating a deployment package. The bundled distlib version tries to use these removed modules, causing the deployment to fail with errors like:
There was an error restoring dependencies. Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.7030/tools/python/packapp/__main__.py", line 14, in <module>
import distlib.wheel
File "/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.7030/tools/python/packapp/distlib/wheel.py", line 15, in <module>
import imp
ModuleNotFoundError: No module named 'imp'
and
There was an error restoring dependencies. Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.7030/tools/python/packapp/__main__.py", line 14, in <module>
import distlib.wheel
File "/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.7030/tools/python/packapp/distlib/wheel.py", line 12, in <module>
import distutils.util
ModuleNotFoundError: No module named 'distutils'
The issue is that both imp and distutils modules were removed in Python 3.12:
imp was deprecated since Python 3.4 and removed in 3.12
distutils was deprecated since Python 3.10 and removed in 3.12
Steps to reproduce
- Install Python 3.12 (I'm using 3.12.9)
- Install Azure Functions Core Tools v4
- Create a Python function app
- Try to deploy the function app using:
func pack --output deployment.zip
or
az functionapp deployment source config-zip --resource-group <resource-group> --name <app-name> --src <zip-file-path>
Suggested fix
The issue is in build/Settings.cs which pins distlib version 0.3.0:
public const string DistLibVersion = "distlib-0.3.0";
public const string DistLibUrl = "https://github.com/vsajip/distlib/archive/0.3.0.zip";
This needs to be updated to a newer version of distlib that's compatible with Python 3.12. The latest version (0.3.8 as of this writing) has been updated to use importlib instead of imp and has removed dependencies on distutils.
Alternatively, the bundled version could be patched to:
- Replace
import impwith appropriateimportlibimports - Replace
distutils.utilwith equivalent functionality fromsetuptools - Replace
imp.get_suffixes()withimportlib.machinery.all_suffixes()
Environment
- Azure Functions Core Tools version: 4.0.7030
- Python version: 3.12.9
- Operating System: macOS (Apple Silicon)
We just had a PR go in replacing distlib so this should be fixed in the next release:
https://github.com/Azure/azure-functions-core-tools/pull/4462