나노 바나나
4일 전
import sys
from pathlib import Path
import os
# -------------------------
# 1. 루트 자동 탐지
# -------------------------
def find_project_root(root_package_name="super_inteligencer"):
current = Path(__file__).resolve()
for parent in [current] + list(current.parents):
if (parent / root_package_name).exists():
return parent / root_package_name
return current.parents[-1] / root_package_name
PROJECT_ROOT = find_project_root()
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
print(f"프로젝트 루트 경로 등록: {PROJECT_ROOT}")
# -------------------------
# 2. 안전한 __init__.py 업데이트
# -------------------------
INIT_CODE = """import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).parent.resolve()
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
"""
def append_init_file_safely(path: Path, code: str):
init_file = path / "__init__.py"
if not init_file.exists():
init_file.write_text(code.strip() + "\n", encoding="utf-8")
print(f"0. 생성: {init_file}")
else:
# 기존 내용 유지 + 중복 방지 후 코드 추가
with init_file.open("r+", encoding="utf-8") as f:
content = f.read()
if code.strip() not in content:
f.write("\n\n" + code.strip() + "\n")
print(f"0. 기존 __init__.py 유지 + 코드 추가: {init_file}")
def create_package_inits_safely(base: Path):
for root, dirs, _ in os.walk(base):
for d in dirs:
folder = Path(root) / d
append_init_file_safely(folder, INIT_CODE)
# 루트 __init__.py 포함 모든 하위 폴더
append_init_file_safely(PROJECT_ROOT, INIT_CODE)
create_package_inits_safely(PROJECT_ROOT)
댓글을 작성해보세요.