Attempted relative import with no known parent package

Python 3.xPython ImportImporterror

Python 3.x Problem Overview


from ..box_utils import decode, nms

This line is giving error

> ImportError: attempted relative import with no known parent package

What is this error and how to resolve this error?

Python 3.x Solutions


Solution 1 - Python 3.x

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.

Solution 2 - Python 3.x

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
      script.py
    /zeta

...then a script in the epsilon directory can be called by:

from alpha.gamma.epsilon import script

Solution 3 - Python 3.x

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPuneet ShekhawatView Question on Stackoverflow
Solution 1 - Python 3.xTobias FeilView Answer on Stackoverflow
Solution 2 - Python 3.xWychhView Answer on Stackoverflow
Solution 3 - Python 3.xKrishnaView Answer on Stackoverflow