pip-tools icon indicating copy to clipboard operation
pip-tools copied to clipboard

pip-compile errors when generating hashes with AttributeError 'cache_clear' with pip v25.1

Open EpicWink opened this issue 7 months ago • 8 comments

When running with pip v25.1, pip-compile --generate-hashes fails with exception:

AttributeError: 'function' object has no attribute 'cache_clear'

Environment Versions

  1. OS Type: Linux - Ubuntu 22.04.5 LTS (Jammy Jellyfish)
  2. Python version: 3.10.12
  3. pip version: 25.1
  4. pip-tools version: 7.4.1

Steps to replicate

# requirements.in
numpy
pip install -U 'pip >= 25.1'
pip-compile --generate-hashes

Expected result

Successful lock file generation

Actual result

Exception: AttributeError: 'function' object has no attribute 'cache_clear'

Traceback (click to expand)
Traceback (most recent call last):
  File "./venv/bin/pip-compile", line 8, in <module>
    sys.exit(cli())
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1161, in __call__
    return self.main(*args, **kwargs)
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1082, in main
    rv = self.invoke(ctx)
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1443, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "./venv/lib/python3.10/site-packages/click/core.py", line 788, in invoke
    return __callback(*args, **kwargs)
  File "./venv/lib/python3.10/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "./venv/lib/python3.10/site-packages/piptools/scripts/compile.py", line 471, in cli
    hashes = resolver.resolve_hashes(results) if generate_hashes else None
  File "./venv/lib/python3.10/site-packages/piptools/resolver.py", line 167, in resolve_hashes
    with self.repository.allow_all_wheels(), log.indentation():
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "./venv/lib/python3.10/site-packages/piptools/repositories/pypi.py", line 452, in allow_all_wheels
    self.finder.find_all_candidates.cache_clear()
AttributeError: 'function' object has no attribute 'cache_clear'

Additional notes

Caused by pypa/pip#13306 - you may want to catch AttributeError when running finder.find_all_candidates.cache_clear(), then attempt to run finder._all_candidates.clear()

Workaround

Use pip v25.0 or lower

pip install 'pip < 25.1'

EpicWink avatar Apr 27 '25 05:04 EpicWink

Running into this as well.

ericscales avatar Apr 27 '25 16:04 ericscales

https://github.com/pypa/pip/commit/24f4600851bbb3d7f22aed0ba6b1e2dcc4973412 seems to be the pip commit that broke this. It replaces @lru_cache with a custom cache that can no longer be cleared (afaict).

It seems to me like a potential route to fix this would be to re-build the finder object instead of trying to clear the cache on it. I.e., replace self.finder.find_all_candidates.cache_clear() with self._finder = self.command._build_package_finder(...).

I'm not sufficiently familiar with pip/pip-tools internals to know if that's a bad idea for other reasons (it's unclear to me how much state lies in the finder object, beyond the cache).

gkreitz avatar Apr 29 '25 14:04 gkreitz

Has anyone considered using uv pip compile?

I just tried it in one of my projects and it worked without issues.

cesarcoatl avatar May 01 '25 07:05 cesarcoatl

Reproduced in https://github.com/peterdemin/pip-compile-multi/pull/497


Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/piptools/__main__.py", line 19, in <module>
    cli()
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/click/core.py", line 1161, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/click/core.py", line 1082, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/click/core.py", line 1697, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/click/core.py", line 1443, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/click/core.py", line 788, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/piptools/scripts/compile.py", line 471, in cli
    hashes = resolver.resolve_hashes(results) if generate_hashes else None
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/piptools/resolver.py", line 167, in resolve_hashes
    with self.repository.allow_all_wheels(), log.indentation():
  File "/usr/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/piptools/repositories/local.py", line 104, in allow_all_wheels
    with self.repository.allow_all_wheels():
  File "/usr/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/runner/work/pip-compile-multi/pip-compile-multi/.tox/py312-linux/lib/python3.12/site-packages/piptools/repositories/pypi.py", line 452, in allow_all_wheels
    self.finder.find_all_candidates.cache_clear()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'cache_clear'

peterdemin avatar May 03 '25 16:05 peterdemin

Any idea when a new pip-tools will get released to fix (yes, I understand there is a workaround).

ericscales avatar May 23 '25 15:05 ericscales

Any idea when a new pip-tools will get released to fix (yes, I understand there is a workaround).

I asked on Matrix and got this reply from @webknjaz:

Hello, I just ran into this issue while trying to compile my requirements and I assume a lot more people are gonna hit it soon. Can the PR that fixes this be merged soon?

We'd need to fix the CI first. @sirosen was going to take a look at some point. But you could too.

noelleleigh avatar May 23 '25 15:05 noelleleigh

UPD: @sirosen and I started looking into the CI during the PyCon US sprints. We're both traveling to our respective homes today. It'll take some time to settle this.

Meanwhile, I'm expected to gain release privileges in about a dozen days. So things are moving.

Stephen was also going to look into handling the most recent pip compat. Plus, we brainstormed a few aspects of his dependency groups PR, among many other things. Expect that people will need to step away for a couple of days and decompress.

webknjaz avatar May 23 '25 17:05 webknjaz

I had a similar issue running:

pip-compile --generate-hashes requirements/development.in --output-file requirements/development.txt

using:

  • Python 3.12.9
  • pip 25.1.1
  • pip-tools 7.4.1
  • pip-compile 7.4.1

However, downgrading pip to 24.3.1 (I had upgraded after installing requirements) addressed my issue and the file was generated correctly.

Here is the error output from the issue with pip 25:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/piptools/__main__.py", line 19, in <module>
    cli()
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/click/core.py", line 1442, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/click/core.py", line 1363, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/click/core.py", line 1830, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/click/core.py", line 1226, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/click/core.py", line 794, in invoke
    return callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/click/decorators.py", line 34, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/piptools/scripts/compile.py", line 471, in cli
    hashes = resolver.resolve_hashes(results) if generate_hashes else None
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/piptools/resolver.py", line 167, in resolve_hashes
    with self.repository.allow_all_wheels(), log.indentation():
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/.asdf/installs/python/3.12.9/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/piptools/repositories/local.py", line 104, in allow_all_wheels
    with self.repository.allow_all_wheels():
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jvanderz/.asdf/installs/python/3.12.9/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/jvanderz/src/MyDjango5Project/.venv/lib/python3.12/site-packages/piptools/repositories/pypi.py", line 452, in allow_all_wheels
    self.finder.find_all_candidates.cache_clear()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'cache_clear'

It should be noted that I am using the ASDF version management tool to manage my non-system python versions. I only have 3.12.9 installed in addition to the system version of 3.11

$ ls -l .venv/bin/python
lrwxrwxrwx 1 jvanderz jvanderz 54 May 27 16:30 .venv/bin/python -> /home/jvanderz/.asdf/installs/python/3.12.9/bin/python

vanderzielj avatar May 28 '25 14:05 vanderzielj

Looking at a recent CI test run, it seems that this issue is what causes at least some of the tests to fail: https://github.com/jazzband/pip-tools/actions/runs/16041294513/job/45263254425#step:10:5449

taleinat avatar Jul 03 '25 12:07 taleinat

That "Cron" workflow has been busted for a while and is misleading (sorry!). I'd advise anyone looking at pip-tools CI to ignore it for now; I haven't decided yet if I want to try to fix it or just remove it to replace later. CI runs triggered by commits for main should be stable right now -- we aren't testing against the newer pip versions in that workflow, so that builds are more reproducible.

We have a contribution which fixes this particular issue (#2178), and I've taken that work and rebased it as part of #2195, but I unfortunately got stuck on some Windows path shenanigans and haven't got that cleaned up yet. The new tests ended up exploring some subtle and slightly weird behavior in pip internals (which is not user-facing in pip, but which pip-compile is exposing 😢 ).

I consider support for the latest pip version the main issue which prevents me from wanting to push out a release, so if I can get #2195 across the finish line, we can incorporate some final fixes and hopefully have this issue fixed. Huge thanks to everyone who has chipped in on this effort, by the way!

sirosen avatar Jul 03 '25 14:07 sirosen

Thanks for the info, the update and your work on this @sirosen!!

taleinat avatar Jul 03 '25 15:07 taleinat

@sirosen @webknjaz I've successfully tested 757e62fca257a4ae686299893a591f20e78668be (which includes #2195), verifying this is issue fixed

EpicWink avatar Jul 14 '25 07:07 EpicWink

@EpicWink thanks for verifying!

webknjaz avatar Jul 14 '25 09:07 webknjaz

Our renovatebot setup with pip-compile manager is broken because of this with the same error. Seems it uses pip-tools 7.4.1 (from March 2024). Can you please create a new release with the fix?

udaya2899 avatar Jul 24 '25 16:07 udaya2899

We're actively working on it.

I haven't had much time for FOSS work this week, but we're getting the changelog cleaned up so that we can push a release.

I'm not sure what the best way is to communicate this publicly, but we are getting asked in multiple threads about this. We're well aware that a release is needed; there's only one thing left in the 7.5.0 milestone.

sirosen avatar Jul 24 '25 18:07 sirosen

https://pypi.org/project/pip-tools/7.5.0/#pip_tools-7.5.0-py3-none-any.whl

webknjaz avatar Jul 31 '25 10:07 webknjaz