Attempted relative import with no known parent package

后端 未结 3 1449
無奈伤痛
無奈伤痛 2020-11-27 21:32
from ..box_utils import decode, nms

This line is giving error

ImportError: attempted relative import wi

相关标签:
3条回答
  • 2020-11-27 21:55

    Apparently, box_utils.py isn't part of a package. You still can import functions defined in this file, but only if the python script that tries to import these functions lives in the same directory as box_utils.py, see this answer.

    Nota bene: In my case, I stumbled upon this error with an import statement with one period, like this: from .foo import foo. This syntax, however, tells Python that foo.py is part of a package, which wasn't the case. The error disappeared when I removed the period.

    0 讨论(0)
  • 2020-11-27 21:57

    If a different dictionary contains script.py, it can be accessed from the root. For instance:

    If your program is structured:

    alpha
      > beta
        > delta
      > gamma
        > epsilon
        > zeta
    

    where alpha, beta, gamma, delta, epsilon, and zeta are different directories. A script in the epsilon directory can be called by:

    from alpha.gamma.eplsilon import script

    0 讨论(0)
  • 2020-11-27 22:00
    package
       |--__init__.py
       |--foo.py
       |--bar.py
    

    Content of bar.py

    from .foo import func
    ...
    

    If someone is getting the exactly same error for from .foo import func.

    It's because you've forgot to make it a package. So you just need to create __init__.py inside package directory.

    0 讨论(0)
提交回复
热议问题