pip install fails with 'use_2to3 is invalid' for an old package
error in anyjson setup command: use_2to3 is invalid.Short answer
Setuptools 58 removed the use_2to3 option, and an old package that still sets it fails to build. The official advice is to port the package or pin an older setuptools. To install it anyway, install a setuptools below 58 and tell pip not to use build isolation.
Fix it fast
1Find out which cause you have
See which setuptools version pip would use and whether the package is pulled in by something you can upgrade.
pip --version pip show setuptools pip install anyjson 2>&1 | tail -n 152Match what you see, then jump to its fix
What does the error look like?
error in anyjson setup command: use_2to3 is invalid.ERROR: Failed building wheel for anyjson
Cause 1 of 2
The package asks for 2to3 conversion and setuptools no longer supports it
1Check
The setuptools changelog says v58.0.0 removed 2to3 support during builds, and later 58.0.x releases fail fast when use_2to3 is supplied (not when it is set to a false value).
pip download anyjson --no-deps --no-binary :all: -d /tmp/pkg && grep -R use_2to3 /tmp/pkg2Fix
Preferred: upgrade the package that depends on it, or a maintained replacement. If you must keep it, pin setuptools below 58 as the changelog advises, and build without isolation so pip uses that setuptools.
pip install "setuptools<58" wheel
pip install --no-build-isolation anyjson3Verify
The package installs.
pip show anyjsonConfirmed by setuptools.pypa.io
Cause 2 of 2
pip's isolated build environment fetches a new setuptools every time
1Check
pip builds source packages in an isolated environment by default. --no-build-isolation turns that off, and the build dependencies must then be present in your environment.
pip config list; env | grep PIP_2Fix
Either use --no-build-isolation as above, or keep isolation and give pip a constraints file for build dependencies.
echo "setuptools<58" > build-constraints.txt
pip install --build-constraint build-constraints.txt anyjson3Verify
pip builds the wheel with the pinned setuptools and the install completes.
pip show anyjsonConfirmed by pip.pypa.io
How do I stop it coming back?
- Replace unmaintained dependencies instead of pinning old build tools forever.
- Pin setuptools in the Dockerfile or constraints file, so the build does not change when a new release appears.
- Check how old a package is before you add it to requirements.
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 built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Servers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
pip install cryptography tries to compile and fails in a Docker build
fatal error: 'openssl/aes.h' file not found
cryptography ships prebuilt wheels.
3 causes, each with a fixServers
cv2.imshow raises 'not implemented' because the wrong OpenCV package is installed
cv2.imshow() works in one environment and fails in another
The headless OpenCV packages are built without any GUI code, so cv2.imshow cannot open a window.
2 causes, each with a fixServers
pip install Cython succeeded, but the shell cannot find cythonize
a pip warning that a script was installed to a directory outside PATH
pip put the cythonize script in a folder your shell does not search.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Import of python_2_unicode_compatible fails after upgrading Django
Code importing django.utils.six fails on Django 3.0 and later
Django 3.0 removed the leftover Python 2 helpers, including python_2_unicode_compatible and django.utils.six.
2 causes, each with a fix