티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Attempted relative import with no known parent package

'알려진 상위 패키지가 없는 상대 가져오기를 시도했습니다' 오류 수정하기

 문제 내용 

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?

이 오류는 무엇이며, 이 오류를 해결하는 방법은 무엇인가요?

 

 

 

 높은 점수를 받은 Solution 

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.

보통, box_utils.py가 패키지의 일부가 아닙니다. 여전히 이 파일에 정의된 함수를 가져올 수는 있지만, box_utils.py를 가져오려는 파이썬 스크립트가 box_utils.py와 같은 디렉토리에 있어야 합니다. 이 답변을 참조하세요.

 

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.

참고: 저는 import 문에서 점 하나(.)를 사용했을 때 이 오류를 만났습니다. 예를 들어, from .foo import foo. 그러나 이 구문은 foo.py가 패키지의 일부인 것으로 Python에게 알려주므로, 이 경우에는 점을 제거하면 오류가 사라집니다.

 

 

 

 가장 최근 달린 Solution 

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

다른 디렉토리에 script.py가 있는 경우 루트에서 액세스할 수 있습니다. 예를 들어:

 

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

 

 

출처 : https://stackoverflow.com/questions/55084977/attempted-relative-import-with-no-known-parent-package

반응형
댓글
공지사항
최근에 올라온 글