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?
A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There are two types of relative imports: implicit and explicit. Implicit relative imports have been deprecated in Python 3, so I won't be covering them here.
The beyond top level package error in relative import error occurs when you use a relative import without the file you are importing being part of a package. To fix this error, make sure that any directories that you import relatively are in their own packages.
If you have setup.py in your project and you use find_packages() within it, it is necessary to have an __init__.py file in every directory for packages to be automatically found.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With