[Tutor] file accessibility across directories?

James Hartley jjhartley at gmail.com
Wed Feb 28 17:39:11 EST 2024


While in the project directory:
$ pwd
/home/me/project
$ cat src/foo.py
class Foo:
    pass
$ cat utils/baz.py
from src.foo import Foo
$ python utils/baz.py
Traceback (most recent call last):
  File "./baz.py", line 1 in <module>
    from src.foo import Foo
ModuleNotFoundError: No module named 'src'
Conversation opened. 4 messages. All messages read.
Conversation opened. 4 messages. All me
import issues
python/tutor at python.org
James Hartley <jjhartley at gmail.com>
Jan 9, 2024, 12:06 PM


to tutor

Skip to content <https://mail.google.com/mail/u/0/>
Using Gmail with screen readers <https://mail.google.com/mail/u/0/>
14 of 941
import issues
python/tutor at python.org
James Hartley <jjhartley at gmail.com
Jan 9, 2024, 12:06



On Wed, Feb 28, 2024 at 1:25 PM dn via Tutor <tutor at python.org> wrote:

> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list