[Tutor] file accessibility across directories?
Mats Wichmann
mats at wichmann.us
Wed Feb 28 19:06:13 EST 2024
On 2/28/24 15:39, James Hartley wrote:
> 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'
When you run a script, the directory of the script is added to sys.path.
Not the directory you were in when you ran it. You can print out
sys.path in utils/baz.py to convince yourself of this. There's no src
in that directory (utils).
More information about the Tutor
mailing list