azure-functions-core-tools icon indicating copy to clipboard operation
azure-functions-core-tools copied to clipboard

Python 3.12 deployment fails due to outdated distlib (0.3.0) using removed modules

Open msetsma opened this issue 6 months ago • 2 comments

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

  1. Install Python 3.12 (I'm using 3.12.9)
  2. Install Azure Functions Core Tools v4
  3. Create a Python function app
  4. 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>

msetsma avatar May 15 '25 12:05 msetsma

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:

  1. Replace import imp with appropriate importlib imports
  2. Replace distutils.util with equivalent functionality from setuptools
  3. Replace imp.get_suffixes() with importlib.machinery.all_suffixes()

Environment

  • Azure Functions Core Tools version: 4.0.7030
  • Python version: 3.12.9
  • Operating System: macOS (Apple Silicon)

msetsma avatar May 15 '25 12:05 msetsma

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

liliankasem avatar Jun 17 '25 21:06 liliankasem