my problem can be summarized as follows.
I have the following files structure:
<path>/
- a.py
<path>/subdir/
- b.py
- c.py
The relation is that a imports b and b imports c (a->b->c).
More precisely, the content of each file is as follows:
- c.py
def foo():
print("Hello World!")
- b.py
import c
def fie():
c.foo()
print("Second Hello World!")
if __name__=="__main__":
fie()
- a.py
from subdir import b
b.fie()
What I want is that invoking a.py will print those strings, namely:
$ python3 a.py
Hello World!
Second Hello World!
$
and invoking b.py from /subdir will print the same thing:
$ cd subdir
$ python3 b.py
Hello World!
Second Hello World!
$
Instead if I invoke a.py from <path>
I have this:
$ python3 a.py
Traceback (most recent call last):
File "a.py", line 1, in <module>
from subdir import b
File "<path>/subdir/b.py", line 1, in <module>
import c
ModuleNotFoundError: No module named 'c'
$
if I, instead, invoke b.py from <path>/subdir
I have the intended behavior:
$ cd subdir
$ python3 b.py
Hello World!
Second Hello World!
$
If I change b.py the following way:
- b.py
from subdir import c
def fie():
c.foo()
print("Second Hello World!")
if __name__=="__main__":
fie()
the invocation of a.py works as intended, but this time is the invocation of b.py that gives ModuleNotFoundError
:
$ cd subdir
$ python3 b.py
Traceback (most recent call last):
File "b.py", line 1, in <module>
from subdir import c
ModuleNotFoundError: No module named 'subdir'
$
Strangely enough even this invocation from <path>
gives the same error:
$ python3 subdir/b.py
Traceback (most recent call last):
File "b.py", line 1, in <module>
from subdir import c
ModuleNotFoundError: No module named 'subdir'
$
If I replace the following string in b.py:
from subdir import c
in
from . import c
I have exactly the same effect of the lastest test.
In the real case scenario both b and c are part of a library self-contained in subdir that is meant to be used both directly (with the if __name__
trick) and imported from anywhere else in order to use their functions.
Said library can not be installed in a fixed space of the file system at this moment (or at least I shouldn't rely on that).
I never had such problems when working with Python 2.6 and I'm recently migrating to Python 3.x. Reading the python 3.x guide to importing was helpful but I didn't find any useful information to solve this case which should be pretty basic (which is why I'm thinking that I've omitted something trivial there).
Can you suggest a way to change any of [a,b,c].py files to cover this use case?