본문 바로가기
Python/Python FAQ

Python 하위 디렉토리에서 파일을 가져오시겠습니까?, Import a file from a subdirectory?

by 베타코드 2023. 9. 8.
반응형

질문


I have a file called tester.py, located on /project.

/project has a subdirectory called lib, with a file called BoxTime.py:

/project/tester.py
/project/lib/BoxTime.py

I want to import BoxTime from tester. I have tried this:

import lib.BoxTime

Which resulted:

Traceback (most recent call last):
  File "./tester.py", line 3, in <module>
    import lib.BoxTime
ImportError: No module named lib.BoxTime

Any ideas how to import BoxTime from the subdirectory?

EDIT

The __init__.py was the problem, but don't forget to refer to BoxTime as lib.BoxTime, or use:

import lib.BoxTime as BT
...
BT.bt_function()

답변


다음을 확인하십시오. 패키지 문서 (6.4 섹션).

간단히 말해서, lib 디렉토리에 빈 파일을 다음과 같이 넣어야합니다.

__init__.py

입니다.

반응형

댓글