-1

Today I tried to work in some exercises and I created two files, the '~/bankocr/bank.py' and '~/bankocr/tests/test_working.py'

And when I tried to execute 'pytest' in the console I have the next error:

ImportError while importing test module '~/bankocr/tests/test_bank.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_bank.py:2: in <module>
    from bank import size_of
E   ImportError: No module named bank

I didn't see the difference in the files using an IDE to create a new project, but still, one works and the other don't.

Did I forget something?

3 Answers3

0

bank.py and test_bank.py are not in the same folder. You can try to use relative import,

from ..bank import size_of
Simba
  • 23,537
  • 7
  • 64
  • 76
0

This error is merely based on the pythonpaths.You should introduce that path in your pytest.ini/tox.ini file.

Please follow the steps below:-

(1) Create pytest.ini file in your parent directory.

(2) Edit in the following ways:-

[pytest]
python_paths = .  ~/bankocr/

Execute the code then. I anticipate that you would not observe the error after creating the above file.

Reference:-

https://github.com/bigsassy/pytest-pythonpath/blob/master/pytest.ini

nikhilesh_koshti
  • 393
  • 1
  • 10
0

Instead you can run the tests as,

python -m bankocr.tests

It will run all your test modules inside the tests folder. Also, add init.py at '~/bankocr/' to import your other modules such as bank.py