Issue with Python 3.12 causing distutils error on Streamlit Cloud deployment

Hello Streamlit Community,

I am currently facing an issue when deploying my app on Streamlit Cloud. The app throws the following error during startup:

ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).
Traceback:

File “/mount/src/cv-project-4/app.py”, line 2, in
import setuptools
File “/home/adminuser/venv/lib/python3.12/site-packages/setuptools/init.py”, line 10, in
import distutils.core

It appears that the app is using Python 3.12 by default, which leads to the issue since distutils is no longer included in this Python version.
Troubleshooting:

I’ve tried specifying a different Python version in the requirements.txt, but that doesn’t resolve the issue. Instead, it gives me the following error:

ERROR: Could not find a version that satisfies the requirement python_version<3.12,>=3.10
ERROR: No matching distribution found for python_version<3.12,>=3.10

It seems Streamlit Cloud defaults to Python 3.12 even when a specific version is mentioned, which is causing the error related to distutils.
Request:

Could anyone suggest how to resolve this issue or force Streamlit Cloud to use a compatible version of Python, such as Python 3.10 or 3.11, which works fine with the required dependencies?

You can view my project here: GitHub Repository

Thank you in advance for your help!

Why are you importing setuptools?

Hi!

I imported setuptools because I thought it might be needed for installing and managing dependencies in the project, especially if you’re working with packages that require setup.py for installation. In some cases, such as when creating packages or during deployment, setuptools can be useful for preparing the environment. Anyway, I deleted the import, and it still doesn’t work because of distutils.

The proper way to fix this would be finding an alternative to the apparently unmaintained gdrive.

Meanwhile, you can choose a python version when you deploy the app.

I tried changing the Python version in the advanced settings from 3.9 to 3.11, but it still defaults to Python 3.12 in Streamlit Cloud.
Using uv pip install.
Using Python 3.12.8 environment at /home/adminuser/venv.
Resolved 69 packages in 605ms.
Prepared 69 packages in 13.91s.
Installed 69 packages in 900ms.

No idea why that would happen. It works for me.

[     UTC     ] Logs for cv-project-4-fhiuo8i9xjmjtv9mwzwztc.streamlit.app/
────────────────────────────────────────────────────────────────────────────────────────
[13:30:40] 🖥 Provisioning machine...
[13:30:40] 🎛 Preparing system...
[13:30:40] ⛓ Spinning up manager process...
[13:30:42] 🚀 Starting up repository: 'cv-project-4', branch: 'main', main module: 'app.py'
[13:30:42] 🐙 Cloning repository...
[13:30:42] 🐙 Cloning into '/mount/src/cv-project-4'...
[13:30:42] 🐙 Cloned repository!
[13:30:42] 🐙 Pulling code changes from Github...
[13:30:43] 📦 Processing dependencies...

──────────────────────────────────────── uv ───────────────────────────────────────────

Using uv pip install.
Using Python 3.12.8 environment at /home/adminuser/venv
Resolved 91 packages in 927ms
Prepared 91 packages in 14.55s
Installed 91 packages in 1.09s
 + absl-py==2.1.0
 + altair==5.5.0
 + argparse==1.4.0
 + astunparse==1.6.3
 + attrs==24.3.0
 + blinker==1.9.0
 + cachetools==5.5.0
 + certifi==[2024-12-20 13:31:00.181089] 2024.12.14
 + cffi==1.17.1
 + charset-normalizer==3.4.0
 + click==8.1.7
 + contourpy==1.3.1
 + cryptography==44.0.0
 + cycler==0.12.1
 + flatbuffers==24.3.25
 + fonttools[2024-12-20 13:31:00.181341] ==4.55.3
 + gast==0.6.0
 + gdrive==0.1.5
 + gitdb==4.0.11
 + gitpython==3.1.43
 + google-api-core==2.24.0
 + google-api-python-client==[2024-12-20 13:31:00.181602] 2.43.0
 + google-auth==2.37.0
 + google-auth-httplib2==0.2.0
 + google-auth-oauthlib==0.5.3
 + google-pasta==0.2.0
 + googleapis-common-protos==1.66.0
 +[2024-12-20 13:31:00.181833]  grpcio==1.68.1
 + h5py==3.12.1
 + httplib2==0.22.0
 + idna==3.10
 + jeepney==0.8.0
 + jinja2==[2024-12-20 13:31:00.182033] 3.1.4
 + jsonschema==4.23.0
 + jsonschema-specifications==2024.10.1
 + keras==3.7.0
 + kiwisolver==1.4.7
 + libclang==18.1.1[2024-12-20 13:31:00.182253] 
 + markdown==3.7
 + markdown-it-py==3.0.0
 + markupsafe==3.0.2
 + matplotlib==3.10.0
 + [2024-12-20 13:31:00.182546] mdurl==0.1.2
 + ml-dtypes==0.4.1
 + namex==0.0.8
 + narwhals==1.19.0
 + numpy==2.0.2
 + oauthlib==[2024-12-20 13:31:00.182754] 3.2.2
 + opt-einsum==3.4.0
 + optree==0.13.1
 + packaging==24.2
 + pandas==2.2.3
 + pillow==11.0.0[2024-12-20 13:31:00.182872] 
 + proto-plus==1.25.0
 + protobuf==5.29.2
 + pyarrow==18.1.0
 + pyasn1==0.6.1
 + pyasn1-modules==[2024-12-20 13:31:00.183006] 0.4.1
 + pycparser==2.22
 + pydeck==0.9.1
 + pygments==2.18.0
 + pyparsing==3.2.0
 + [2024-12-20 13:31:00.183144] python-dateutil==2.9.0.post0
 + pytz==2024.2
 + referencing==0.35.1
 + requests==2.32.3
 + requests-oauthlib==2.0.0[2024-12-20 13:31:00.183265] 
 + rich==13.9.4
 + rpds-py==0.22.3
 + rsa==4.9
 + secretstorage==3.3.3
 + setuptools==[2024-12-20 13:31:00.183435] 59.6.0
 + six==1.17.0
 + smmap==5.0.1
 + streamlit==1.41.1
 + tenacity==9.0.0
 + tensorboard==[2024-12-20 13:31:00.183582] 2.18.0
 + tensorboard-data-server==0.7.2
 + tensorflow==2.18.0
 + termcolor==2.5.0
 + toml==0.10.2
 + [2024-12-20 13:31:00.183712] tornado==6.4.2
 + typing-extensions==4.12.2
 + tzdata==2024.2
 + uritemplate==4.1.1
 + urllib3==2.2.3
 + versioneer==0.29
 + watchdog==6.0.0
 [2024-12-20 13:31:00.183826] + werkzeug==3.1.3
 + wheel==0.37.1
 + wrapt==1.17.0
Checking if Streamlit is installed
Found Streamlit version 1.41.1 in the environment

────────────────────────────────────────────────────────────────────────────────────────

[13:31:02] 🐍 Python dependencies were installed from /mount/src/cv-project-4/requirements.txt using uv.
Check if streamlit is installed
Streamlit is already installed
[13:31:04] 📦 Processed dependencies!



────────────────────── Traceback (most recent call last) ───────────────────────
  /home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru  
  nner/exec_code.py:88 in exec_func_with_error_handling                         
                                                                                
  /home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru  
  nner/script_runner.py:579 in code_to_exec                                     
                                                                                
  /mount/src/cv-project-4/app.py:2 in <module>                                  
                                                                                
      1 import streamlit as st                                                  
  ❱   2 import tensorflow as tf                                                 
      3 import numpy as np                                                      
      4 import matplotlib.pyplot as plt                                         
      5 from PIL import Image                                                   
                                                                                
  /home/adminuser/venv/lib/python3.12/site-packages/tensorflow/__init__.py:30   
  in <module>                                                                   
                                                                                
     27 """                                                                     
     28 # pylint: disable=g-bad-import-order,protected-access,g-import-not-at-  
     29                                                                         
  ❱  30 import distutils as _distutils                                          
     31 import importlib                                                        
     32 import inspect as _inspect                                              
     33 import os as _os                                                        
────────────────────────────────────────────────────────────────────────────────
ModuleNotFoundError: No module named 'distutils'
2024-12-20 13:31:11.386 Uncaught app execution
Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
  File "/mount/src/cv-project-4/app.py", line 2, in <module>
    import tensorflow as tf
  File "/home/adminuser/venv/lib/python3.12/site-packages/tensorflow/__init__.py", line 30, in <module>
    import distutils as _distutils
ModuleNotFoundError: No module named 'distutils'
────────────────────── Traceback (most recent call last) ───────────────────────
  /home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru  
  nner/exec_code.py:88 in exec_func_with_error_handling                         
                                                                                
  /home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru  
  nner/script_runner.py:579 in code_to_exec                                     
                                                                                
  /mount/src/cv-project-4/app.py:2 in <module>                                  
                                                                                
      1 import streamlit as st                                                  
  ❱   2 import tensorflow as tf                                                 
      3 import numpy as np                                                      
      4 import matplotlib.pyplot as plt                                         
      5 from PIL import Image                                                   
                                                                                
  /home/adminuser/venv/lib/python3.12/site-packages/tensorflow/__init__.py:30   
  in <module>                                                                   
                                                                                
     27 """                                                                     
     28 # pylint: disable=g-bad-import-order,protected-access,g-import-not-at-  
     29                                                                         
  ❱  30 import distutils as _distutils                                          
     31 import importlib                                                        
     32 import inspect as _inspect                                              
     33 import os as _os                                                        
────────────────────────────────────────────────────────────────────────────────
ModuleNotFoundError: No module named 'distutils'
2024-12-20 13:31:12.792 Uncaught app execution
Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
  File "/mount/src/cv-project-4/app.py", line 2, in <module>
    import tensorflow as tf
  File "/home/adminuser/venv/lib/python3.12/site-packages/tensorflow/__init__.py", line 30, in <module>
    import distutils as _distutils
ModuleNotFoundError: No module named 'distutils'

Yes, you need to change the python version. I think you did something wrong when trying to change it in the settings dialog, because it has always worked for me and others.