PyInstaller
에서 특정 폴더(hello/
이하의 모든 파일과 디렉터리)를 실행 파일(.exe
)에 포함하려면--add-data
옵션을 사용해야 합니다.
pyinstaller --onefile --add-data "my_package/hello/*;my_package/hello" main.py
-🔹 위 명령어는 hello/
폴더 내의 파일만 포함하며, 하위 폴더는 포함되지 않을 수 있음.
pyinstaller --onefile --add-data "my_package/hello;my_package/hello" main.py
🔹 디렉터리 자체를 포함하면 하위 폴더까지 자동으로 추가됨.
pyhton
import sys
import os
def get_resource_path(relative_path):
""" 실행 파일 내부 또는 개발 환경에서 리소스 파일의 경로를 찾음 """
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS # PyInstaller 실행 환경
else:
base_path = os.path.dirname(__file__) # 일반 실행 환경
return os.path.join(base_path, relative_path)
# my_package/hello/ 아래의 특정 파일 접근
resource_path = get_resource_path("my_package/hello/config.json")
with open(resource_path, "r", encoding="utf-8") as f:
print(f.read())
- ✅
--add-data
옵션에서 폴더 전체를 추가하려면"my_package/hello;my_package/hello"
사용 - ✅ 실행 시
sys._MEIPASS
를 활용하여.exe
내부의 리소스 파일에 접근 - ✅ 하위 폴더까지 포함하려면 폴더 자체를 추가하는 것이 가장 깔끔한 방법
'Python' 카테고리의 다른 글
Cycloid Curve Generator : 사이클로이드 곡선 생성 (0) | 2025.01.22 |
---|---|
find_native_python : 파이썬 네이티브 패키지(native package) 사용여부 검사 (0) | 2025.01.22 |
poker_hand_probabilities : 포커(Poker) 확률 계산기 (0) | 2025.01.22 |
find_native_python : 파이썬 네이티브 패키지 사용여부 검사 (0) | 2025.01.21 |
Windows에서 명령 프롬프트(Command Prompt)와 파워쉘(PowerShell)로 pyenv 설치 (0) | 2025.01.16 |