I am running python code in docker.
I have the following file structure:
-my_dir
-test.py
-bird.py
-string_int_label_map_pb2.py
-inference.py
inference.py:
import test
import bird
from string_int_label_map_pb2 import StringIntLabelMap
test.py and bird.py both contain this code:
print('hello world!')
def this_is_test():
return 'hi'
In inference.py 'import test'
throws no error(but ‘hello world!’ is never printed).
'import bird'
throws: ModuleNotFoundError: No module named ‘bird’. Finally
'import string_int_label_map_pb2 '
throws: ModuleNotFoundError: No module named ‘string_int_label_map_pb2 ‘
Note: bird.py and test.py are not simultaneously existing in the dir, they are depticted as such only to make my point. They exist only when I rename the same file to either name to test if the name itself was to blame.
3
Answers
I added bird.py to /worker/ (the WORKDIR path configured in the dockerfile) and sure enough "Hello world!" printed.
So the issue was that inference.py was not searching its parent dir for imports as I thought, but rather the path configured in WORKDIR.
Still don't understand why import test gave no errors since there was never a test.py file in /worker/.
If one or the other is not in the folder but you still have import test and import bird in the inference.py, that’s your issue. Python is trying to import both files but can not find the file.
I also had this problem, how did I solve the problem?
For example.
My directory:
–Application
—hooks
—-init.py
—-helpers.py
—model
—-init.py
—-model_person.py
In script model_person.py
import sys
sys.path.append(‘../’)
from hooks.helpers import *
Thats all