Skip to content
Minsing Log
Go back

English Edition

What is __init__.py?

Programming & CS

While working on a Python project, I ran into an import error.

Additional unfair issue found on 10/13 -> The issue may occur when creating an instance rather than in the import section (the yellow-underlined part in the image below).

-> Similarly, you need to specify in the init.py file that the class I created is a module.


Why does this happen?

One-line summary: This problem occurs because the module BM25Reranker is not recognized as a package.

The init.py file, which should indicate that the directory [RAGchain.reranker] is part of a package, must be present inside the directory. However, if the directories included in the package, such as BM25reranker, do not contain an init.py file, they will not be recognized as packages.

Also, in the following situation, the init.py file exists and the existing rerankers are present, but an error occurs because the newly created bm25 reranker has not been added to it.

In fact, even if only the file is added, the directory is automatically recognized as a package. However, in this case, where it is not recognized properly, define the module to import in init.py as follows.

Solution

Define the module to import in init.py as follows.

from RAGchain.reranker import BM25Reranker

The example image below should make this easier to understand.

Additional note

If you define a variable in init.py, it becomes a variable shared throughout the package and can be imported and used by different modules.

Starting with Python 3.3, a package is recognized even without an init.py file. However, it is recommended to create an init.py file for backward compatibility.

References

  1. https://dojang.io/mod/page/view.php?id=2450
  2. https://passwd.tistory.com/entry/Python-initpy

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
What is yield? (feat. generators)
Next Post
What are : and ->?