Skip to content

Conversation

@purplewall1206
Copy link

distutils is deprecated since python version 3.12, this is a temporary patch to avoid ModuleNotFoundError: No module named 'distutils' errors

details:
https://docs.python.org/3/whatsnew/3.12.html#summary-release-highlights

PEP 632: Remove the distutils package. See the migration guide for advice replacing the APIs it provided. The third-party Setuptools package continues to provide distutils, if you still require it in Python 3.12 and beyond.

…on >= 3.12

distutils is deprecated since python version 3.12, this is a  temporary patch.

details:
https://docs.python.org/3/whatsnew/3.12.html#summary-release-highlights
> PEP 632: Remove the distutils package. See the migration guide for advice replacing the APIs it provided. The third-party Setuptools package continues to provide distutils, if you still require it in Python 3.12 and beyond.
@jonnor
Copy link
Collaborator

jonnor commented Jan 6, 2025

@purplewall1206 Hi and thank you for the merge request. Does this actually fix the exception? Because the documentation says that the "setuptools" package still provides the "distutils" module. And this aligns with my experience - what is needed is to install "setuptools" - and I have been able to make the exception go away by doing that (without any change to the imports). Does that not work for you?

@purplewall1206
Copy link
Author

@jonnor Thanks for replying.

Does this actually fix the exception?

Yes, I use Python 3.12.4 and distutils cannot be installed.

what is needed is to install "setuptools" .... Does that not work for you?

Unfortunately, no. We cannot avoid modifying the import code.

setuptools is often installed by default with python-pip (maybe?), If we only install setuptools without modifying the import statements, it will still raise a No module named 'distutils' error in Python 3.12

@jonnor
Copy link
Collaborator

jonnor commented Jan 8, 2025

The "setuptools" package should install "distutils" module.
I have tested this on Arch Linux with Python 3.12 just now, and it works for me. I did "pip install setuptools", which brought in version 75.7.0. Which platform are you on and which setuptools version? Check with pip show setuptools | grep Version or python -c 'import setuptools; print(setuptools.__version__)'.

@purplewall1206
Copy link
Author

The root cause seems to be that I am running the emlearn code on Windows 11.

I have confirmed the version of setuptools, and the result is as follows:

PS D:\workspace\> python3 -c 'import setuptools; print(setuptools.__version__)'
75.7.0

# create a new compiler object
# force re-compilation even if object files exist (required)
cc = new_compiler(force=1)
cc = _distutils.new_compiler(force=1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be "distutils", not _distutils?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either will work on windows 11, distutils looks better

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_distutils will not work on other platforms, so that must be fixed.

Copy link
Collaborator

@jonnor jonnor Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, there is another usage of new_compiler that must also be updated.
And new_compiler is not in "distutils" module - but in "distutils.ccompiler", so that also needs to be fixed.

In general, please make sure to run the test suite when/before submitting a patch. It catches all these issues.

@jonnor
Copy link
Collaborator

jonnor commented Jan 14, 2025

@purplewall1206 aha. My ability to test on Windows is a bit limited. Anyway, I think that - if you chance the code to first try from "setuputils import distutils", but catch ImportError and then try distutils - then the change can go in and we are sure it also will work as it used to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants