Getting “ImportError: attempted relative import with no known parent package” when running from Python Interpreter

后端 未结 2 912
滥情空心
滥情空心 2021-01-04 05:20

I\'m creating a modular app using Flask blueprints feature. As a result, my directory structure is like this:

project
    __init__.py
    config.py
    mould         


        
2条回答
  •  不知归路
    2021-01-04 05:27

    This may be a bit outdated, but maybe someone else will benefit of my answer. Since python2 und python3 have different default import behavior, you have to determine between these two python versions.

    Python 2.X The default behavior for import models is to look up first the relative and then the absolute search path order. Therefore it should work.

    However, in Python 3.X the default behavior for import models is to look for the model only in the absolute paths (called absolute imports). The current package core gets skipped and since the module db cannot be found anywhere else in the sys.path search path, it throws an error. To resolve this issue you have to use the import statement with dots from . import models to make clear that you are trying to import from a relative directory.

    If you are interested to learn more about importing python modules, I suggest you to start your research with the following key words: module search path, python package import and relative package imports

提交回复
热议问题