[Tutor] file accessibility across directories?

dn PythonList at DancesWithMice.info
Wed Feb 28 14:17:34 EST 2024


James,


On 29/02/24 07:02, James Hartley wrote:
> 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.


Please clarify the command leading to the error:

- running pytest and some unmentioned test file
(which contains what code) testing baz.py

- running ~/project/utils/baz.py as a script from the project-directory

- running a some unmentioned project-script which calls baz.py 
successfully, before baz.py fails

- something else

-- 
Regards,
=dn


More information about the Tutor mailing list