2013년 12월 19일 목요일

pyinstaller 사용하여 python .exe 파일 만들기

pyinstaller를 사용하면 python으로 작성한 스크립트를 .exe 형태로 실행가능하게 만들수 있다. 아래는 각각 console mode와 window mode의 .exe 파일 생성을 위한 사용예이다.

# Console Mode
python c:\pyinstaller-2.0\utils\Makespec.py --onefile --out=dist ProSolManager.py
# 아래는 multiprocessing.Manager() 를 사용할 경우사용. (onefile 옵션은 에러발생)
# python c:\pyinstaller-2.0\utils\Makespec.py --onedir --out=dist ProSolManager.py
python c:\pyinstaller-2.0\utils\Build.py dist\ProSolManager.spec

# Window Mode
python c:\pyinstaller-2.0\utils\Makespec.py --onefile --windowed --icon=.\icon\kipi.ico --out=dist ProSolManagerGui.py
# 위의 옵션 대신 아래 옵션도 사용가능
# python c:\pyinstaller-2.0\utils\Makespec.py --onedir --windowed --icon=.\icon\kipi.ico --out=dist ProSolManagerGui.py
python c:\pyinstaller-2.0\utils\Build.py dist\ProSolManagerGui.spec
copy icon output_dir
위의 옵션을 사용하면 스크립트에서 사용하는 라이브러리 들을 하나의 .exe로 만들어 준다.


pyinstaller를 사용하는 경우 다음 두 가지를 기억하자.

첫째, 스크립트 내에서 multiprocessing 모듈을 사용할 경우에는 아래와 같이 main 모듈의 if __name__=='__main__': 아래에 multiprocessing.freeze_support()를 명시해야 한다.
if __name__ == '__main__':
    # On Windows calling this function is necessary.
    if sys.platform.startswith('win'):
        multiprocessing.freeze_support()

둘째, 스크립트 내에서 import psycopg2 를 사용면서 동시에 multiprocessing을 사용하는 경우 .exe 실행시 fork로 생성된 프로세스에서 psycopg2를 찾지 못하는 ImportError가 발생되며, pyinstaller의 bug로 판단된다.
이를 회피하기 위해 psycopg2 패키지 전체("C:\Python26_64\Lib\site-packages\psycopg2")를 pyinstall에 의해 생성된 .exe 파일과 같은 폴더내에 위치시킨다.
그러면 꼼수이긴 하지만 ImportError를 회피할 수 있다.

댓글 없음:

댓글 쓰기