简体   繁体   中英

Module not found error with Python in Docker

My directory structure:


    .
    |--app
    |   |--__init__.py
    |   |--main.py
    |  
    |
    |--Dockerfile
    |--requirements.txt

I can build this and run it fine. But when I add another.py file to my project, like this:


    .
    |--app
    |   |--__init__.py
    |   |--main.py
    |   |--handler.py
    |
    |--Dockerfile
    |--requirements.txt

and I try to import a function from handler.py in main.py, like this:


    # main.py
    from handler import someFunc

then main.py gives me a NoModuelFound error for handler.py

Can someone explain how to fix this (and maybe also explain why this error is happening)?

Print out your sys.path as one of the first things you do in your application, there's a good chance it's not set in such a way to get easy access to the stuff under app .

The Google guidelines (the ones we follow, and the ones that have gotten us out of the bad habit of fiddling around with PYTHONPATH ) have two rules applicable here.

  • Use absolute imports; and
  • Don't import anything less than a module.

That would involve importing (and using) as follows:

from app import handler
handler.someFunc()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM