AssertionError inside of ensure_local_distutils when building a PyInstaller exe using setuptools/distutils

Question:

I am trying to convert some Python code into an .exe with PyInstaller. My code uses distutils, which has already caused me some head scratching in the past as it seems to duplicate setuptools functionality, and interacts with it strangely. It e.g. requires an unused import of setuptools to work properly which seems very unpythonic to me.

I need to use both packages inside my project. I import both packages in the order import setuptools; import distutils because the other order throws a warning which is already worrying.

My script runs fine but after I turn it into an exe I get a traceback inside of the suspicious _distutils_hack submodule of setuptools. And yes it just prints a file name with no context.

Traceback (most recent call last):
  <18 lines omitted>
  File "PyInstallerloaderpyimod03_importers.py", line 495, in exec_module
  File "_distutils_hackoverride.py", line 71, in <module>
  File "_distutils_hack__init__.py", line 71, in do_override
  File "_distutils_hack__init__.py", line 59, in ensure_local_distutils
AssertionError: C:Users<omitted>AppDataLocalTemp_MEI294562distutilscore.pyc 

I am using

  • Python 3.7.7 on win32 (but I reproduced it with 3.8, 3.9, and 3.10)
  • pyinstaller==4.8 (Jan 2022) for Windows
  • distutils==3.7.7 (built-in)
  • setuptools==60.5.0 (Jan 2022)

Apparently, setuptools is listed on PyPI and thus upgradable, but distutils is not listed on PyPI and thus not upgradable (the version is bundled with Python).

Asked By: xjcl

||

Answers:

A workaround I found was downgrading to

pip install setuptools==59.8.0

I reported this issue to the project and then confirmed the bug was introduced in setuptools==60.0.0. I’ll update this question when I find out more, or it gets fixed.

Answered By: xjcl

I reported my issue to the setuptools project, but then the PyInstaller project stepped in to include a workaround on their side. Now any release starting from 5.4 should work!

pip install pyinstaller>=5.4
Answered By: xjcl