실행파일 만들기
실행파일(.exe) 만들기
실행파일 만들기
파이썬으로 개발한 프로그램을 다른 사람에게 배포하기 위해서
실행파일(.exe) 파일을 만든는 방법에 대해 알아본다.
pyInstaller 는 파이썬으로 개발한 프로그램을 실행파일(.exe)로 변경해주는 프로그램이다.
이것을 사용해 변경하는 방법을 알아보자.
pyInstaller 설치
설치는 간단히 아래의 명령어로 설치 가능하다.
pip install pyInstaller
CMD 창에서 개발한 디렉토리 창으로 이동한다.
해당경로에서 아래의 명령어로 실행파일을 만들면 된다.
pyinstaller --onefile --windowed filename.py
# --onefile : 모든 필요한 파일을 하나의 실행 파일로 묶음
# --windowed : 콘솔창 없이 GUI 어플리케이션으로 실행파일 생성
실행하고 나면 dist 폴더가 생성되는데, 그 안에 들어가면 실행파일(.exe) 파일이 생성되어있다.
실행파일을 실행했더니 아래와 같은 에러가 발생했다.
.ui 파일은 인스톨러가 자동으로 포함하지 못해서 발생하는 에러라고 한다.
먼저, 코드 상에서 ui 파일 path 지정해주는 부분을 아래와 같이 바꾼다.
if getattr(sys, 'frozen', False):
# 실행 파일 모드일 때
application_path = sys._MEIPASS
else:
# 개발자 모드일 때
application_path = os.path.dirname(os.path.abspath(__file__))
ui_path = os.path.join(application_path, 'test_file.ui')
다음으로 이전 명령어를 실행하고 나면 dist 폴더와 .spec 파일이 생성되어있을거다.
해당 파일에서, Analysis의 datas를 튜플형태로 ('test_file.ui', '.') 형태로 변경해준다.
# -*- mode: python ; coding: utf-8 -*-
a = Analysis(
['stock_report_crawling.py'],
pathex=[],
binaries=[],
datas=[('test_file.ui', '.')],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name='stock_report_crawling',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
마지막으로 아래의 명령어로 .spec 파일을 실행해준다.
pyinstaller --onefile 파일명.spec
dist 폴더에 가서 실행파일을 실행하면 잘 동작하는것을 확인할 수 있다.
Reference
'Two > 파이썬' 카테고리의 다른 글
얕은복사 & 깊은복사 (0) | 2023.12.22 |
---|---|
Iterator (이터레이터) (0) | 2023.12.14 |