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:
- Upgrade that package. The traceback names the file; the package is the folder under
site-packagesit lives in. Most maintained libraries removed theirdistutilsimports when 3.12 came out. - Install setuptools as a stopgap. Its copy of
distutilsmakes the import work again[1][3]. Add it torequirements.txtso 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-versionfile at the project root. While you upgrade an old dependency, pin 3.11:3.11Once the dependency is upgraded, move the file to
3.12or 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 asinstall_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 importsdistutilsas the app starts (cause 2), the build passes and the deploy fails asstart_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.txtlists, so addsetuptoolsthere 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
- Python docs — What's New In Python 3.12 (docs.python.org). Checked .
- PEP 632 — Deprecate distutils module (peps.python.org). Checked .
- Setuptools docs — Porting from Distutils (setuptools.pypa.io). Checked .
- setuptools source — setup.py (the distutils-precedence .pth file) (raw.githubusercontent.com). Checked .
- virtualenv — Changelog (docs/changelog.rst) (raw.githubusercontent.com). Checked .
- NumPy 1.26.0 Release Notes (numpy.org). Checked .
- Python Packaging User Guide — Package formats (packaging.python.org). Checked .
- 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 appRelated
- ModuleNotFoundError: No module named 'pkg_resources': how to fix itModuleNotFoundError: No module named 'pkg_resources' means code imported pkg_resources, which is part of setuptools, not the standard library. Python 3.12 stopped pre-installing setuptools in new virtual environments, and setuptools 82.0.0 removed pkg_resources altogether, so installing the latest setuptools no longer brings it back. Upgrade the package that imports it, move your own code to importlib.metadata and importlib.resources, or pin setuptools<82 as a stopgap.
- ModuleNotFoundError: No module named 'imp' on Python 3.12+: how to fix itModuleNotFoundError: No module named 'imp' means code running on Python 3.12 or later imported imp, which was deprecated since Python 3.4 and removed from the standard library in 3.12. It is almost always an old release of a library, or old code of your own. Upgrade that library, replace the imp calls with their importlib equivalents, or pin Python 3.11 with a .python-version file while you upgrade.
- ModuleNotFoundError: No module named — after a deployPython searched sys.path and could not find the module you import. After a deploy that almost always means the package is not in requirements.txt, is listed under a different name than the one you import, or the start command runs a Python that the packages were not installed into.
- pip: Failed building wheel / failed-wheel-build-for-install — how to fix itpip found no ready-made wheel for a package on your Python version and platform, tried to compile one from source, and the compiler step failed. The fix is almost never to install a compiler: move to a version of the package that ships a wheel for your Python, switch to its binary variant (psycopg[binary], psycopg2-binary), or pin the Python version the package supports.
- pip: Could not find a version that satisfies the requirement / No matching distribution found — how to fix itERROR: Could not find a version that satisfies the requirement X (from versions: none), followed by No matching distribution found for X, means pip searched the package index and found no release of X it can install here. The name is wrong (often the import name instead of the package name), the pinned version does not exist, every release needs a different Python, no file fits your platform, or pip is looking at the wrong index or cannot reach it. The versions list in the message tells you which.
- error: externally-managed-environment (pip): how to fix iterror: externally-managed-environment means pip found an EXTERNALLY-MANAGED marker in the Python it was about to install into: that Python belongs to your operating system's or Homebrew's package manager (PEP 668), and pip will not change it outside a virtual environment. Install into a virtual environment instead (python3 -m venv .venv), use pipx for command-line tools, or build on the official python Docker image; --break-system-packages is the last resort.
- How to run a Python script 24/7 (and keep it running)A script started in a terminal stops when the terminal closes, the SSH connection drops or the laptop sleeps. To keep it running you need something that starts it again: a systemd service with Restart=always, Docker with a restart policy, or a platform's background worker. On Livops it runs as a worker for 19 credits a month (19,000 so'm).
- Django hosting in 2026: how to deploy a Django app, and whereFor most Django apps, a platform that deploys from Git is the right pick: Render and DigitalOcean App Platform are the simplest fixed monthly prices, Fly.io is the cheapest in dollars, Railway bills by usage, and a VPS is for when you want root. Livops fits Django apps whose users are in Uzbekistan, at 39 credits a month. Below: prices and free tiers side by side, a Django deployment checklist, and how to host a Django project on Livops.