행위

Pyinstaller

DB CAFE

thumb_up 추천메뉴 바로가기


1 파이썬을 exe 파일로 배포 하기[편집]

1.1 실행 옵션[편집]

1.1.1 일반적인 옵션[편집]

--distpath DIR : bundled app을 저장할 경로 (디폴트 : ./dist)

--workpath WORKPATH : temporary 작업 파일을 저장할 곳 (디폴트 : ./build)

--noconfirm : output 경로를 물어보지 않고 지정 (디폴트 : SPECPATH/dist/SPECNAME)

--clean : 빌드하기 전에 캐시와 temp 파일들 제거

--log-level LEVEL : 빌드할 때, console에 프린트될 메시지 디테일 정도 (LEVEL은 TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL 중에 하나가 될 수 있으며, 디폴트는 INFO이다)

1.1.2 산출물 관련 옵션[편집]

--onedir : 하나의 folder bundle로 생성 (default)

--onefile : 하나의 실행 파일로 생성

1.1.3 묶을 파일들에 대한 옵션[편집]

--add-data <SRC;DEST or SRC:DEST> : executable 파일 외 더 추가되어야 할 non-binary 파일이나 폴더. 구분자는 윈도우에서는 ;로 사용하고, unix 계열 시스템에서는 :를 사용한다. 이 옵션은 여러번 사용 가능하다.cf. 이미지.png 등 포함 가능

--add-binary <SRC;DEST or SRC:DEST> : executable 파일 외 더 추가되어야 할 binary 파일. 여러번 사용 가능

--paths DIR : imports를 위해 탐색줘야 할 경로 설정. ':'로 구분하여 여러 path 설정 가능

--hidden-import MODULENAME : 스크립트 속 코드에 나와 있지 않은 import 명시

1.1.4 생성 방식에 대한 옵션[편집]

--debug  : frozen application의 디버깅을 제공해줌

1.1.5 윈도우 관련 옵션[편집]

--nowindowed : 입출력을 위해 console 창을 연다 (default)

--noconsole : 입출력을 위한 console 창이 만들어지지 않음

2 pyinstaller parameter[편집]

usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
                   [--add-data <SRC;DEST or SRC:DEST>]
                   [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
                   [--hidden-import MODULENAME]
                   [--additional-hooks-dir HOOKSPATH]
                   [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
                   [--key KEY] [-d {all,imports,bootloader,noarchive}] [-s]
                   [--noupx] [--upx-exclude FILE] [-c] [-w]
                   [-i <FILE.ico or FILE.exe,ID or FILE.icns>]
                   [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
                   [--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
                   [--win-no-prefer-redirects]
                   [--osx-bundle-identifier BUNDLE_IDENTIFIER]
                   [--runtime-tmpdir PATH] [--bootloader-ignore-signals]
                   [--distpath DIR] [--workpath WORKPATH] [-y]
                   [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
                   scriptname [scriptname ...]


2.1 1개 EXE파일에 묶어서 배포 하기[편집]

  옵션 : -F 
  (압축 방식으로 묶이기 때문에 실행 시 압축을 푸는 딜레이가 미세하게 발생한다.)
ex) pyinstaller.exe -F sample.py
     pyinstaller.exe -F --onefile sample.py
   sample.py 라는 파일이 sample.exe 파일 하나로 묶이게 된다.

2.2 exe 파일 실행 시 콘솔(도스창) 화면을 나타나지 않게 처리[편집]

총 세 가지 방법이 있는데 한 가지만 사용

ex) pyinstaller.exe --noconsole --onefile sample.py
 ex) pyinstaller.exe -w sample.py
 ex) pyinstaller.exe --windowed sample.py


2.3 관리자 권한으로 실행[편집]

ex) pyinstaller.exe --uac-admin sample.py

※ 해당 옵션은 exe 단일 파일로 묶는 -F 인자랑 같이 쓰면 적용 안됨

-F를 사용하지 않으면 파일이 여러개로 쪼개어 생성되겠지만 정작 메인 exe 파일은 관리자 권한 요청이 기본으로 적용

( 만약 빌드하고 싶은 파일이 x64 가 아닌 x86 이라면 굳이 pyinstaller 말고 py2exe 사용 )


2.4 빌드 파일에 아이콘을 적용하고 싶을 경우[편집]

ex) pyinstaller.exe --icon=icons\icon.ico sample.py
 ex) pyinstaller.exe -i=icons\icon.ico sample.py

※ 위의 인자값은 icons 폴더 안에 있는 icon.ico 파일을 사용한다는 의미. 위 예제문의 경우 .py 파일과 같은 경로에 icons 폴더가 존재.(만약 완전 다른 경로라면 full path 적용).

※ ico 파일 외에도 icon이 리소스에 적용되어 있는 exe 파일을 사용해도 된다.

  저렇게 하면 빌드된 exe 파일의 리소스 영역에 아이콘의 이미지 저장

3 maximum recursion error 발생시[편집]

https://stackoverflow.com/questions/38977929/pyinstaller-creating-exe-runtimeerror-maximum-recursion-depth-exceeded-while-ca

This worked for me

Run pyinstaller and stop it to generate the spec file :

pyinstaller filename.py A file with .spec as extension should be generated Now add the following lines to the beginning of the spec file :

import sys sys.setrecursionlimit(5000) Now run the spec file using :

pyinstaller filename.spec

4 파이참 import 자동 최적화 방법[편집]

https://www.jetbrains.com/pycharm/guide/tips/optimize-imports/