Home > front end >  Python: ModuleNotFoundError: No module named ''
Python: ModuleNotFoundError: No module named ''

Time:03-08

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.

CodePudding user response:

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/.

CodePudding user response:

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.

  • Related