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

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

ModuleNotFoundError: 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.

By Livops team, the people who build and run LivopsUpdated

The app stops at startup, or the install stops, with this at the end of a traceback:

ModuleNotFoundError: No module named 'imp'

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

What the error means

imp was the standard library’s old interface to the import system. It was deprecated in Python 3.4 in favour of importlib[2], and removed in Python 3.12[3]. Python’s 3.12 release notes list it among the removals, next to asynchat and asyncore[1]. Python 3.11 is the last version that provides it[3].

Unlike distutils, for which the release notes point to setuptools as a way to keep it, there is no package that brings imp back: the release notes give a table that maps each imp function to its importlib replacement[1]. So the fix is always one of three: the code that imports imp changes, it is replaced by a newer release that already changed, or it runs on Python 3.11.

Causes, most common first

1. An old release of a library imports imp

The traceback ends in a file under site-packages. A library pinned in requirements.txt years ago still does import imp, and on Python 3.12 that line fails as soon as the library is imported.

Two examples from the projects’ own history:

  • eventlet 0.30.2 imports imp at the top of eventlet/patcher.py and uses its import locks[6]. Its changelog for 0.30.3 lists “Use _imp instead of deprecated imp”[5].
  • Invoke 2.0.0 imports imp in invoke/loader.py and loads task files with imp.find_module and imp.load_module[8]. Invoke 2.1.0 replaced it with importlib[7].

Fix: move the pin to a release that no longer imports imp, ideally the current one. The traceback names the file; the package is the folder under site-packages it lives in. Write the pin as a floor:

eventlet>=0.30.3     # instead of eventlet==0.30.2
invoke>=2.1          # instead of invoke==2.0.0

2. An old package’s setup.py imports imp during install

The deploy fails while installing dependencies, and pip’s output shows a setup.py importing imp. pip is building that release from source because it found no wheel to install. The fix is the same as cause 1: a newer release, ideally one with a wheel for your Python. Why pip builds from source and how to make it stop: Failed building wheel.

3. Your own code imports imp

If the last frame is your own file, replace the calls. Python’s 3.12 release notes give the mapping[1]:

imp importlib
imp.reload() importlib.reload()
imp.load_module() importlib.import_module()
imp.find_module() importlib.util.find_spec()
imp.new_module(name) types.ModuleType(name)
imp.cache_from_source() importlib.util.cache_from_source()
imp.source_from_cache() importlib.util.source_from_cache()
imp.get_magic() importlib.util.MAGIC_NUMBER
imp.get_tag() sys.implementation.cache_tag
imp.get_suffixes() importlib.machinery.SOURCE_SUFFIXES, EXTENSION_SUFFIXES, BYTECODE_SUFFIXES
imp.NullImporter insert None into sys.path_importer_cache
imp.load_source() the recipe below

The most common call in old code is imp.load_source(name, path), which loads a Python file by its path. The importlib docs give this recipe for it[4]:

import importlib.util
import sys

def import_from_path(module_name, file_path):
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module
    spec.loader.exec_module(module)
    return module

config = import_from_path("config", "/app/config.py")

The 3.12 release notes give an equivalent load_source() that does not add the module to sys.modules[1]. A few imp functions have no replacement at all: imp.load_compiled(), imp.load_dynamic(), imp.load_package(), imp.init_builtin(), and the lock functions imp.acquire_lock(), imp.release_lock() and imp.lock_held(), because the import system has used per-module locks since Python 3.3[1]. Code that calls those needs rewriting, not a rename.

4. Nothing can change today

When the library cannot be upgraded right now, run on Python 3.11 until it can: it is the last version with imp[3], where the module is still there, 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 imp"

On 3.12 or later the second line fails with the same error, which confirms the Python version is the cause. 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. To list every installed file that still imports imp, with the virtual environment in .venv:

grep -rlE "^[[:space:]]*(import imp|from imp import)" .venv/lib/python3.*/site-packages

Python 3.12 made other changes that break old projects the same way: it removed distutils (No module named ‘distutils’) and stopped pre-installing setuptools, which carries pkg_resources (No module named ‘pkg_resources’)[1]. If the missing module is one 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 runtime failure stops as start_failed. When a library imports imp as the app starts (cause 1), 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.

  • A build failure stops as install_failed. When an old setup.py imports imp (cause 2), the deploy stops as install_failed, pip’s output is in the build log, often with a Tip: line at its end, and the old version keeps running.

  • The fix goes in requirements.txt. Raise the pin of the library that imports imp in requirements.txt and deploy again.

  • The assistant explains. When a build fails, the log shows why and the assistant explains 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, and 3.11 is the last version that has the imp module. The server runs Python 3.12 or later. Compare python --version in both places.

Can I install imp with pip?

Python's 3.12 release notes offer no package to bring it back, the way setuptools brings back distutils. The documented path is importlib. Upgrade the library that imports imp, or run on Python 3.11 until you can.

What replaces imp.load_source?

importlib.util.spec_from_file_location and importlib.util.module_from_spec, then exec_module on the loader. Python's 3.12 release notes and the importlib docs both give a short recipe for it.

What replaces imp.reload and imp.find_module?

importlib.reload for imp.reload, and importlib.util.find_spec for imp.find_module. imp.load_module becomes importlib.import_module, and imp.new_module becomes types.ModuleType.

Is pinning Python 3.11 a real fix?

It is a stopgap. imp is still in 3.11, marked deprecated, so the app runs again. The library that imports imp still has to be upgraded before you can move to Python 3.12 or later.

Is this related to the distutils and pkg_resources errors?

Same release. Python 3.12 removed imp and distutils and stopped pre-installing setuptools, which carries pkg_resources, in new virtual environments. An old project often hits all three in a row.

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. Python 3.11 docs — imp, access the import internals (docs.python.org). Checked .
  3. Python docs — imp (Removed Modules) (docs.python.org). Checked .
  4. Python docs — importlib, the implementation of import (docs.python.org). Checked .
  5. eventlet — NEWS (raw.githubusercontent.com). Checked .
  6. eventlet 0.30.2 source — eventlet/patcher.py (raw.githubusercontent.com). Checked .
  7. Invoke — Changelog (raw.githubusercontent.com). Checked .
  8. Invoke 2.0.0 source — invoke/loader.py (raw.githubusercontent.com). 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