153
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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Puneet Shekhawat
  • 1,657
  • 2
  • 7
  • 13

4 Answers4

52

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.

Tobias Feil
  • 2,399
  • 3
  • 25
  • 41
20

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

Danny Bullis
  • 3,043
  • 2
  • 29
  • 35
Wychh
  • 656
  • 6
  • 20
  • 41
    But what if you want to import a file, w/o referencing the root package? Eg, what if one wanted to import `gamma/epsilon.py` from `delta` with a relative reference (ie, not referring to `alpha`) – svangordon Jun 12 '20 at 03:34
  • 1
    @svangordon That would also interest me. Or in other words, beta is a package and gamma is another package. But they are not two subpackages of alpha. How to handle this? – Isi Sep 24 '20 at 16:03
  • 4
    @svangordon: I guess I figured it out. If both of them are packages (i.e., there are _ _ init _ _ .py files in beta and gamma, but not in alpha), go in the alpha directory and type: `pip install -e .`. I have not tried when two packages are in that directory, but at least if there is only one package in the directory and you do that, you can in another Python file type `import beta` or `import beta.delta` – Isi Oct 09 '20 at 12:14
  • 1
    If you have a module in `delta` called `script.py` (alpha > beta > delta > script.py) and want to call the `epsilon` module (alpha > gamma > epsilon.py), you can import it using `from ...gamma import epsilon`. NOTE that if you want to run this as a script, `python -m alpha/beta/delta/script.py` will NOT work! You need to call it using `python -m alpha.beta.delta.script` instead. – slow-but-steady Mar 28 '22 at 00:10
  • 4
    This was pretty confusing and I needed to play around with this a bit until I understaood this, so uploaded some example code to https://github.com/slow-but-steady/relative-imports-in-python for anyone who wants to play around with this more. – slow-but-steady Mar 28 '22 at 00:49
-2

in the latest python version, import it, directly don't use .. and .library import the file which you want. this technique will work in the child directory. If you import it from parent directory, then place the directory's full path.

Aonu
  • 1
  • 4
  • 4
    Welcome to SO. Please read https://stackoverflow.com/editing-help, then update your answer with code explanations and improved formatting. It will help others understand. – Azhar Khan Aug 02 '22 at 13:58
-10
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.

Paaksing
  • 432
  • 1
  • 4
  • 17
Krishna
  • 6,107
  • 2
  • 40
  • 43
  • 24
    the error still exist even when __init__.py exist in `package` directory – thinkdeep Jan 07 '21 at 19:08
  • make sure it's exactly `__init__.py` – Krishna Jan 08 '21 at 04:29
  • 73
    The `__init__.py` file makes no difference (I think it's no longer required as of Python 3.3). This isn't working for me either (with or without `__init__.py`), and I've got no idea why. – Tom Jan 09 '21 at 22:47