[Tutor] file accessibility across directories?

James Hartley jjhartley at gmail.com
Wed Feb 28 13:02:50 EST 2024


I am having issues with accessing files across different directories.  The
file structure I am using is simple:

+-project
    |
    +- .git
    +-src
    |    |
    |    +- foo.py
    |    +- bar.py
    |
    +- tests
    |    |
    |    +- test_foo.py
    |
    +- utils
          |
          +- baz.py

Test files within project/tests reference classes successfully in foo.py
through Pytest as:

from src.foo import Foo

Pytest is actually executed from the ./project directory successfully as:

python -m pytest -v tests/test_foo.py

baz.py had been in the src directory, where it successfully referenced
class Foo as:

from foo import Foo

Now baz.py has been moved to utils/baz.py.  I would think that this would
require the import statement to changed to:

from src.foo import Foo

...but I receive a "ModuleNotFoundError:  No module named 'src'" error.

I have tried adding an empty src/__init__.py file which doesn't change the
error received.  I always invoke the Python interpreter from the project
directory.  Always.

The ./project/tests directory does not have a __init__.py file present.  To
date, I have not required this file anywhere, so I have not created it.

Any insights shared which can help me resolve this simple issue would be
appreciated.


More information about the Tutor mailing list