Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

ModuleNotFoundError: No module named 'distutils' on Python 3.12+: how to fix it

ModuleNotFoundError: No module named 'distutils' means code running on Python 3.12 or later imported distutils, which PEP 632 removed from the standard library in 3.12. It is usually an old package pinned in requirements.txt, built from source or imported at runtime. Upgrade that package to a release that supports your Python, install setuptools for code that still imports distutils, or pin Python 3.11 with a .python-version file while you upgrade.

By Livops team, the people who build and run LivopsUpdated

The install or the app stops with this, often at the end of a traceback that starts in a setup.py or deep inside a library:

ModuleNotFoundError: No module named 'distutils'

The Python running that code is 3.12 or newer, and distutils is no longer part of it. This page explains what was removed and why, how to tell which package is still importing it, and the fix for each case, most common first.

What the error means

distutils was Python’s original module for building and installing packages. PEP 632 deprecated it in Python 3.10 and 3.11 and removed it in 3.12: from 3.12, code that imports distutils no longer works[2]. Python’s 3.12 release notes list the removal among the release highlights[1]. The reason given in the PEP is that setuptools had integrated a complete copy of distutils and no longer depended on the standard library’s[2].

The same release notes name the way back: the third-party setuptools package continues to provide distutils on Python 3.12 and beyond[1]. Since setuptools 60.0.0 it includes its own vendored copy, enabled by default[3], and when it is installed it adds a small .pth file that runs at startup and makes import distutils find that copy[4].

Why it appears now

Before 3.12, setuptools was usually there without anyone asking for it. Python 3.12 stopped pre-installing setuptools in environments created with venv, so distutils, setuptools and pkg_resources are no longer available in a new environment by default[1]. virtualenv made the same change: from version 20.23.0 it does not install the setuptools and wheel seed packages for Python 3.12 and later[5]. So code that quietly relied on setuptools’ copy of distutils breaks too, not only code that relied on the standard library’s.

Causes, most common first

1. An old package pinned in requirements.txt, built from source

A pin written before Python 3.12 existed, numpy==1.24.4 for example, names a release with no wheel for 3.12. When pip finds no wheel to install, it downloads the source distribution, compiles a wheel from it and installs that[7]. For an old release that build often goes through setup.py and distutils, and it fails with this error in the install log.

NumPy is the best-known case: its release notes say that because Python 3.12 dropped distutils, supporting 3.12 meant replacing its setup.py/distutils build, and 1.26.0 was the first release to support Python 3.12[6].

Fix: move the pin to a release that supports your Python. Look up the package’s release notes for the first version with your Python in its supported list, and pin that or newer:

numpy>=1.26

The fix is in your requirements.txt, not on the server: any Python 3.12 that builds the old release fails the same way. To make pip fail fast instead of compiling, --only-binary :all: tells it not to use source packages at all, so a package without a wheel for your Python fails to install with a short message[8]. Why a missing wheel ends in a compiler run, and the other ways that build fails: Failed building wheel.

2. A package imports distutils when your code runs

The install passed, and the app stops at startup with the error in a library’s traceback: an older release of some package does from distutils.version import LooseVersion or similar at import time. Two fixes, in this order:

  1. Upgrade that package. The traceback names the file; the package is the folder under site-packages it lives in. Most maintained libraries removed their distutils imports when 3.12 came out.
  2. Install setuptools as a stopgap. Its copy of distutils makes the import work again[1][3]. Add it to requirements.txt so the server gets it too:
setuptools

The fix works through the .pth file setuptools installs. It adds the shim only while the variable SETUPTOOLS_USE_DISTUTILS is unset or local[4], so do not set it to stdlib: that value selects the standard library’s distutils[3], which on 3.12 does not exist.

3. Your own code imports distutils

If the traceback ends in your own file, replace the import. PEP 632 lists the replacements[2]:

You use Replace with
distutils.version (LooseVersion, StrictVersion) packaging.version (the packaging package)
distutils.sysconfig sysconfig (standard library)
distutils.spawn.find_executable shutil.which
distutils.spawn.spawn subprocess.run
distutils.util.get_platform platform
distutils.core.setup, distutils.command.* setuptools

For a few helpers, distutils.util.strtobool among them, the PEP’s advice is to write your own version[2]. For the build commands, setuptools’ porting guide maps each one to its setuptools equivalent[3].

4. The environment has no setuptools at all

A script or a tool that runs setup.py directly, or an old packaging tool, expects setuptools in the environment, and on 3.12 a fresh venv or virtualenv does not have it[1][5]. Install it into the environment you run the tool with:

python -m pip install setuptools

5. None of the above can change today

When an old package cannot be upgraded right now, run on Python 3.11 until it can. distutils is still in 3.11, marked deprecated[2]. Pinning the Python version buys time; it does not fix the dependency, and the next Python upgrade brings the error back.

How to find which package imports it

Run these with the same Python the app runs with:

python --version
python -c "import distutils; print(distutils.__file__)"
python -m pip show setuptools

On 3.12 or later without setuptools, the second line fails with the same error. With setuptools installed it prints a path inside the setuptools package, which shows its copy is the one being used[4]. Then read the traceback from the bottom up: the last frame outside the standard library is the package to upgrade, or your own file to edit. For an install failure, the package name is in pip’s lines above the traceback.

If the error is not about distutils but about a module your own code needs, the general case is in ModuleNotFoundError: No module named.

On Livops

  • Choose the Python. Livops runs Python 3.10 to 3.14 and reads the version from a .python-version file at the project root. While you upgrade an old dependency, pin 3.11:

    3.11

    Once the dependency is upgraded, move the file to 3.12 or later.

  • A build failure stops as install_failed. When the error comes from a package that pip tries to build (cause 1), the deploy stops as install_failed, pip’s output is in the build log, and the previous version keeps running.

  • A runtime failure stops as start_failed. When a library imports distutils as the app starts (cause 2), the build passes and the deploy fails as start_failed. The reason is on the Logs page. A web app’s previous version keeps running; a bot’s was stopped before the new one started, so the bot is down until a deploy succeeds.

  • setuptools goes in requirements.txt. Livops installs what your requirements.txt lists, so add setuptools there rather than installing it by hand.

  • The assistant explains. The button next to a failed build reads the log and says what happened and how to fix it. Changing the pin is yours to commit.

A Python web app costs 39 credits a month; a bot, worker or cron job 19. The install errors are in Install failed, and the startup ones in The app did not start.

Questions

Why does it work on my machine and fail on the server?

Your machine most likely runs Python 3.11 or older, where distutils is still in the standard library, or a 3.12 environment that happens to have setuptools installed. The server runs Python 3.12 or later without it. Compare python --version in both places.

Can I pip install distutils?

Not as a package of that name. The supported replacement is setuptools: since version 60.0.0 it carries its own copy of distutils, and Python's 3.12 release notes point to it for code that still needs distutils. Run pip install setuptools in the same environment.

I installed setuptools and the error is still there. Why?

Either setuptools went into another Python than the one that runs your code, or the error happens while pip builds an old package from source in its own build environment. Check with python -m pip show setuptools using the same python, and for a build error upgrade the package that is being built.

Is SETUPTOOLS_USE_DISTUTILS=stdlib a fix?

No. That value tells setuptools to use the standard library's distutils, and on Python 3.12 and later there is none. Leave the variable unset; its default is setuptools' own copy.

Should I just downgrade to Python 3.11?

As a stopgap, yes: distutils is still in 3.11, deprecated. Plan the upgrade anyway, because the package that imports distutils will not work on any newer Python without a change.

My own code uses distutils. What do I replace it with?

PEP 632 lists replacements: packaging.version for distutils.version, sysconfig for distutils.sysconfig, shutil.which for distutils.spawn.find_executable, subprocess.run for distutils.spawn.spawn and setuptools for the build commands. A few helpers such as strtobool have no replacement and are a few lines to write yourself.

Which Python versions does Livops run?

Python 3.10 to 3.14. Put the version in a .python-version file at the project root, for example 3.11 or 3.12.

Sources

  1. Python docs — What's New In Python 3.12 (docs.python.org). Checked .
  2. PEP 632 — Deprecate distutils module (peps.python.org). Checked .
  3. Setuptools docs — Porting from Distutils (setuptools.pypa.io). Checked .
  4. setuptools source — setup.py (the distutils-precedence .pth file) (raw.githubusercontent.com). Checked .
  5. virtualenv — Changelog (docs/changelog.rst) (raw.githubusercontent.com). Checked .
  6. NumPy 1.26.0 Release Notes (numpy.org). Checked .
  7. Python Packaging User Guide — Package formats (packaging.python.org). Checked .
  8. pip docs — pip install (pip.pypa.io). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Python app

Got a question? — @livops_uz