Attempted relative import with no known parent package [duplicate]
Asked Answered
L

4

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

Lombardi answered 10/3, 2019 at 6:11 Comment(1)
Out of curiosity, what version of python are you using (specifically, not just 3.x)?Witten
B
57

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.

Burlburlap answered 3/6, 2019 at 15:17 Comment(6)
how to make it part of a package?Deus
put a file called __init.py__ in the same directoryBurlburlap
I did add __ init __.pyDeus
This worked for me. Just remove the . in front of foo in from .foo import fooNawab
The comment from @RagavY worked for me - i.e. just remove the "." in front of the pathReckless
@RagavY but both are supposed to be valid.Eraeradiate
P
22

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

Prominent answered 4/6, 2020 at 9:15 Comment(5)
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)Bruno
@Bruno 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?Avenge
@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.deltaAvenge
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.Substructure
This was pretty confusing and I needed to play around with this a bit until I understaood this, so uploaded some example code to github.com/slow-but-steady/relative-imports-in-python for anyone who wants to play around with this more.Substructure
G
-3

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.

Greyhen answered 30/7, 2022 at 20:2 Comment(1)
Welcome to SO. Please read stackoverflow.com/editing-help, then update your answer with code explanations and improved formatting. It will help others understand.Zouave
E
-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.

Elasticity answered 22/6, 2020 at 7:7 Comment(3)
the error still exist even when init.py exist in package directoryLansquenet
make sure it's exactly __init__.pyElasticity
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.Eugeniusz

© 2022 - 2024 — McMap. All rights reserved.