itu-algorithms/itu.algs4

pytest 'itu' ModuleNotFoundError in venv

Closed this issue · 3 comments

Hello,

I just tried to clone, install and test itu.algs4:

git clone https://github.com/itu-algorithms/itu.algs4.git
cd itu.algs4
python3 -m venv venv # python 3.7.6
source venv/bin/activate
pip install --upgrade -e '.[dev]'
pytest 

And, I get the following error:

ImportError while importing test module '/Users/crkrenn/Dropbox/code/itu.algs4/tests/test_symbol_tables.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_symbol_tables.py:5: in <module>
    from itu.algs4.searching.binary_search_st import BinarySearchST
E   ModuleNotFoundError: No module named 'itu'

Do you have suggestions for how to fix this?

Very gratefully,

-Chris

I am unable to reproduce this error using Python 3.9.0 and pytest-6.2.1 on Linux. Can you provide more details? I think this is an issue with how your version of pytest resolves paths.

Perhaps this subcomment helps?
https://stackoverflow.com/questions/41748464/pytest-cannot-import-module-while-python-can#comment77079570_41752043
That is, try to add an empty file tests/init.py. In any case, if you solve it, please let us know how.

Moreover, thanks for this, we should probably add your idea for opening a virtual environment to CONTRIBUTING.md.

Please also try whether python -m pytest works. This seems to resolve paths differently than running pytest directly.

Thanks very much for your quick reply!

python -m pytest did work. As well as manually putting the venv/bin directory at the front of my search path.

I actually had run into this problem before, but had forgotten how to solve it. It seems to be some strangeness with either OS X or anaconda:

(venv_36) (base) ➜  itu.algs4 git:(master) ✗ which python
/Users/crkrenn/Dropbox/code/itu.algs4/venv_36/bin/python

(venv_36) (base) ➜  itu.algs4 git:(master) ✗ which pytest
/opt/anaconda3/bin/pytest

(venv_36) (base) ➜  itu.algs4 git:(master) ✗ echo $PATH
/Users/crkrenn/Dropbox/code/itu.algs4/venv_36/bin:/opt/anaconda3/bin:/opt/anaconda3/condabin:/opt/anaconda3/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMwareFusion.app/Contents/Public:/Library/TeX/texbin:/usr/local/go/bin:/usr/local/MacGPG2/bin:/opt/X11/bin:/Library/Apple/usr/bin

(venv_36) (base) ➜  itu.algs4 git:(master) ✗ export PATH=/Users/crkrenn/Dropbox/code/itu.algs4/venv_36/bin:${PATH}

(venv_36) (base) ➜  itu.algs4 git:(master) ✗ which pytest
/Users/crkrenn/Dropbox/code/itu.algs4/venv_36/bin/pytest