setuptools: [BUG] 71.0.0 fails with `backports.tarfile` on Python 3.8 when other backports imported

setuptools version

setuptools==71.0.0

Python version

Python 3.8

OS

Linux

Additional environment information

No response

Description

CI jobs started tripping when using setuptools 71, released two hours ago.

Expected behavior

CI builds work as expected.

How to Reproduce

No reproducer yet, but a stacktrace on GHA.

Output

ImportError: cannot import name 'tarfile' from 'backports' 
  (/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/backports/__init__.py)

– https://github.com/crate/cratedb-toolkit/actions/runs/9982521975/job/27589490156?pr=196#step:6:229

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 27 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I am also encountering this issue. Can reproduce it with the following packages installed:

Package                          Version
-------------------------------- -------
backports-datetime-fromisoformat 2.0.1
pip                              24.1.2
setuptools                       71.0.1

and then running:

>>> from backports.datetime_fromisoformat import MonkeyPatch
>>> import pkg_resources
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/temp/debug_setuptools_issue/lib64/python3.11/site-packages/pkg_resources/__init__.py", line 93, in <module>
    from jaraco.text import (
  File "/data/temp/debug_setuptools_issue/lib64/python3.11/site-packages/setuptools/_vendor/jaraco/text/__init__.py", line 12, in <module>
    from jaraco.context import ExceptionTrap
  File "/data/temp/debug_setuptools_issue/lib64/python3.11/site-packages/setuptools/_vendor/jaraco/context.py", line 17, in <module>
    from backports import tarfile
ImportError: cannot import name 'tarfile' from 'backports' (/data/temp/debug_setuptools_issue/lib64/python3.11/site-packages/backports/__init__.py)

Workarounds:

  1. Downgrade setuptools to <70
  2. Install it myself with: venv38/bin/pip install backports.tarfile

I faced a similar bug with python 3.10. It was fixed by downgrading to <70

pip install --force-reinstall -v "setuptools<70"

Not sure what is happening, but with both 71.0.0 and 71.0.1 - my pytest flow stopped working

We use pyfilesystem2 - which is using pkg_resources internally and has the same issue:

pytest is failing here:

$ venv38/bin/pytest -v
====================================== test session starts ======================================
platform linux -- Python 3.8.19, pytest-7.4.4, pluggy-1.5.0 -- venv38/bin/python
cachedir: .pytest_cache
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
configfile: setup.cfg
plugins: env-1.1.3, mock-3.14.0, error-for-skips-2.0.2, fail-slow-0.6.0, unordered-0.6.1, split-0.9.0, flask-1.3.0, timeout-2.3.1, benchmark-4.0.0, cov-5.0.0
collected 0 items / 1 error

==================== ERRORS =========================
_________________________________ ERROR collecting test session _________________________________
../.conda/envs/py38/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1014: in _gcd_import
    ???
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
venv38/lib/python3.8/site-packages/_pytest/assertion/rewrite.py:186: in exec_module
    exec(co, module.__dict__)
apps/myapp/myapp_test/conftest.py:13: in <module>
    from myapp.db.models import SQLAlchemy
apps/myapp/myapp/__init__.py:11: in <module>
    from myapp.config import setup_logging, setup_settings
apps/myapp/myapp/config/__init__.py:13: in <module>
    import fs as fs_library
venv38/lib/python3.8/site-packages/fs/__init__.py:4: in <module>
    __import__("pkg_resources").declare_namespace(__name__)  # type: ignore
venv38/lib/python3.8/site-packages/pkg_resources/__init__.py:93: in <module>
    from jaraco.text import (
venv38/lib/python3.8/site-packages/setuptools/_vendor/jaraco/text/__init__.py:12: in <module>
    from jaraco.context import ExceptionTrap
venv38/lib/python3.8/site-packages/setuptools/_vendor/jaraco/context.py:17: in <module>
    from backports import tarfile
E   ImportError: cannot import name 'tarfile' from 'backports' (venv38/lib/python3.8/site-packages/backports/__init__.py)

But note that - When I don’t use pytest - it works fine. When I do venv38/bin/python -c "import fs" and venv38/bin/python -c "import pkg_resources" - there is no error So, I am unclear how to make a Minimal Reproducible Example …

Workarounds:

  1. Downgrade setuptools to <70
  2. Install it myself with: venv38/bin/pip install backports.tarfile

A slightly longer stack trace from a python3.10 install:

  File "/usr/local/lib/python3.10/dist-packages/simple_history/__init__.py", line 1, in <module>
    from pkg_resources import DistributionNotFound, get_distribution
  File "/usr/local/lib/python3.10/dist-packages/pkg_resources/__init__.py", line 93, in <module>
    from jaraco.text import (
  File "/usr/local/lib/python3.10/dist-packages/setuptools/_vendor/jaraco/text/__init__.py", line 12, in <module>
    from jaraco.context import ExceptionTrap
  File "/usr/local/lib/python3.10/dist-packages/setuptools/_vendor/jaraco/context.py", line 17, in <module>
    from backports import tarfile
ImportError: cannot import name 'tarfile' from 'backports' (unknown location)

pip install setuptools==69.0.0

UPDATE: setuptools 71.0.4 also failed with the same error, but 70.3.0 worked.

If you’re still having the error on setuptools 71.0.4 or later, it’s a different root cause. Please open a new issue and importantly describe a reproducer (or at least more detail about your environment such that we might be able to reproduce it). Crucial will be to describe what other packages you have installed in the environment where the failure occurs.

@jaraco I can confirm that #4486 fixes the problem for me 😃

Is someone available to test if the PR in #4486 fixes the issue? Maybe pip install git+https://github.com/pypa/setuptools@refs/pull/4486/head.