Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Python 3.11 and newer fail to import formatargspec from inspect

Your error

ImportError: cannot import name 'formatargspec' from 'inspect' (/usr/lib/python3.12/inspect.py). Did you mean: 'formatargvalues'?1 more line it can show

Fix it fast

Most common cause, 1 of 2

An old wrapt is installed under Python 3.11 or newer

Upgrade wrapt, then let pip check list any installed package whose declared range no longer fits.

Fix
python -m pip install -U "wrapt>=1.14"
python -m pip check
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: cannot import name 'formatargspec' from 'inspect' means code written for old Python runs on 3.11 or newer, where inspect.formatargspec was removed after years of deprecation. The usual caller is a wrapt release older than 1.14.0, pulled in by another package. Upgrade wrapt (current 2.5), or replace the call with inspect.signature() in your own code.

  • Applies to Python 3.11 to 3.15, wrapt 2.5.2, pip 25 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Search the installed packages and your own code for calls to the removed functions. The folder of each hit names the package to upgrade.

Run this first
grep -rnE "inspect\.(formatargspec|getargspec)|from inspect import .*(formatargspec|getargspec)" "$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')" . --include=*.py | head

Then find the line you see in this list and open its fix:

  1. 1An old wrapt is installed under Python 3.11 or newerThe hit is inside site-packages/wrapt, and pip show wrapt prints a version below 1.14.0
  2. 2Your code, or an unmaintained package, calls the removed inspect function directlyThe hit is in your own source tree or in a package with no release since Python 3.11 came out

What does the error look like?

  • ImportError: cannot import name 'formatargspec' from 'inspect' (/usr/lib/python3.12/inspect.py). Did you mean: 'formatargvalues'?
  • AttributeError: module 'inspect' has no attribute 'getargspec'. Did you mean: 'getargs'?

Cause 1 of 2

An old wrapt is installed under Python 3.11 or newer

You seeThe hit is inside site-packages/wrapt, and pip show wrapt prints a version below 1.14.0

Check

The cannot-import-name error from wrapt is a known Python 3.11 break. wrapt's changelog says 3.11 dropped inspect.formatargspec, which wrapt used for signature-changing decorators, and 1.14.0 bundles its own replacement. A requirements file pinned to an older wrapt, or a package that caps it, keeps the broken copy installed.

Check
python -m pip show wrapt | grep Version

Fix

Upgrade wrapt, then let pip check list any installed package whose declared range no longer fits. Upgrade whichever package it reports as well.

Fix
python -m pip install -U "wrapt>=1.14"
python -m pip check

Verify

wrapt imports and the app starts past the line that failed.

Verify
python -c "import wrapt; print(wrapt.__version__)"

Confirmed by github.com

Did this fix it?

Cause 2 of 2

Your code, or an unmaintained package, calls the removed inspect function directly

You seeThe hit is in your own source tree or in a package with no release since Python 3.11 came out

Check

Python 3.11 removed inspect.formatargspec, deprecated since 3.5, and inspect.getargspec, deprecated since 3.0. The 3.11 notes point to inspect.signature() and the Signature object for formatting, and to getfullargspec() for the old tuple form.

Check
grep -rn "formatargspec\|getargspec" --include=*.py .

Fix

Replace the call. str(inspect.signature(func)) prints the parameter list, and inspect.getfullargspec(func) is the documented stand-in for getargspec. For a dead package, switch to a maintained alternative or vendor a patched copy.

Fix
import inspect

def describe(func):
    # was: inspect.formatargspec(*inspect.getargspec(func))
    return str(inspect.signature(func))

Verify

The module imports on Python 3.11 or newer and prints the same parameter list as before.

Verify
python -c "import inspect; print(inspect.signature(lambda a, b=1: None))"

Confirmed by docs.python.org

Did this fix it?

How do I stop it coming back?

  • Pin the exact Python minor version in the Dockerfile (for example python:3.12-slim) and change it only in a pull request that also re-locks dependencies.
  • Run the full test suite on the next Python version in CI before switching the production image.
  • Add a CI step that fails when pip check reports a broken requirement after the install.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.