ModuleNotFoundError when running script from Terminal

前端 未结 2 1853
甜味超标
甜味超标 2021-02-12 15:28

I have the following folder structure:

  • app
    • __init__.py
    • utils
      • __init__.py
2条回答
  •  遥遥无期
    2021-02-12 16:22

    This expands on @Mad Physicist's answer.


    First, assuming app is itself a package (since you added __init__.py to it) and utils and products are its subpackages, you should change the import to import app.utils.transform, and run Python from the root directory (the parent of app). The rest of this answer assumes you've done this. (If it wasn't your intention making app the root package, tell me in a comment.)


    The problem is that you're running app.products.fish as if it were a script, i.e. by giving the full path of the file to the python command:

    python app/products/fish.py
    

    This makes Python think this fish.py file is a standalone script that isn't part of any package. As defined in the docs (see here, under

提交回复
热议问题