1

I have the following directory structure and files:

├── a
│   ├── b
│   │   ├── b.py
│   └── c
│       ├── c.py
└── main.py

# main.py
from a.b import b
print('i am main')

# a/b/b.py
from a.c import c
print('i am b')

# a/c/c.py
print('i am c')

The following works just fine and is expected.

» python3 main.py
i am c
i am b
i am main

However, if I go inside the directory a/b and run

python3 b.py

I get:

» python3 b.py
Traceback (most recent call last):
  File "b.py", line 1, in <module>
    from a.c import c
ModuleNotFoundError: No module named 'a'

And if I try to run it from the root of the project I get

» python3 a/b/b.py
Traceback (most recent call last):
  File "a/b/b.py", line 1, in <module>
    from a.c import c
ModuleNotFoundError: No module named 'a'

It makes sense that it can't find the module which is one level above the directory but how would one run a/b/b.py script in this scenario?

shriek
  • 5,605
  • 8
  • 46
  • 75

1 Answers1

0

I'd still like a better explanation on why this works. But I got a hint for this from this answer. You can run

python3 -m a.b.b 

to run b.py which is inside a/b/ directory.

shriek
  • 5,605
  • 8
  • 46
  • 75