[Ch.5: Unit Testing Classes] import error introduced: "ModuleNotFoundError: No module named 'stuff'"
Bobby-H opened this issue · 3 comments
After diff-checking and confirming that my code & file structure was 1:1 with Pandy Knight's, im left thinking an error is introduced within the chapter 5 section, at the accumulator class' import statement in the /test_accum.py file
from stuff.accum import Accumulator
Either that, or PyCharm (2023.2.2 (Community Edition)) or Python (v3.12.0) dislikes how im attempting to import the Accumulator class from the stuff directory. After attempts to resolve it failed I pulled down the course repo and called python3 -m pytest -v test_accum.py
on the repo version to be greeted with the same error:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ERROR collecting test_accum.py _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
ImportError while importing test module '/PycharmProjects/test-automation-university/venv/tau-pytest/tests/test_accum.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py:90: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test_accum.py:2: in <module>
from stuff.accum import Accumulator
E ModuleNotFoundError: No module named 'stuff'
Since im new to it, I considered that PyCharm did some virtual environment or PATHing oddness(?), though this was also the errored outcome when I used another IDE like VSC.
If any additional info is needed i'll do my best to provide it.
[Screenshot below was taken while troubleshooting with a duplicate file, creatively named 'stuff2']
From what directory are you running the python -m pytest
command?
If the tau-pytest/stuff
directory contains the accum.py
module, then if you run the python -m pytest
command from the tau-pytest
directory (not from within tau-pytest/tests
), then there should not be an import error.
The screenshot shows a directory structure that I would not expect. Does the repo
folder contain the example code you checked out from the GitHub repository for the course? If so, you might want to remove it to make the code in your directory clearer. That might be causing some confusion and errors.
Ahhh thank you thank you thank you - I had it all wrong; I had been exclusively running python -m pytest
from within my /tests folder (both in my local and in the forked repo). Looking back I think I might've confused the pytest file naming convention (all test files having to begin with 'test_') for having to run pytest from within the /tests folder.
I suppose, because i'd been getting away with it up until any use of inheritance, I didn't think to consider I might've gotten that wrong.
But i just reset my local's state to the way it was when i'd hit the error & ran pytest from outside the /tests file:
everything passed; no errors.
Thank you for getting me on the right path! and my apologies for what i assume was a pretty sloppy PR from me (im knocking off a lot of rust) 😅
No problem. Glad to help.