반응형

Python 실행 파일(exe) 만들기

매번 .py로 실행하기 번거로워서 .exe 실행파일로 만들었습니다. exe 파일을 만드는 방법은 여러가지 많지만, 여러 방법 중 하나인 pyinstaller 모듈을 이용한 exe 파일 만드는 방법을 포스팅 했습니다.

 

실행환경

운영체제 : window 10

파이썬 버전 : python 3.7

 

pyinstaller 설치

pip를 이용하면 간단하게 설치 할 수 있습니다.

 

커맨드 명령어

>> pip install pyinstaller
C:\Users\75385\Desktop>pip install pyinstaller
Collecting pyinstaller
  Downloading https://files.pythonhosted.org/packages/03/32/0e0de593f129bf1d1e77eed562496d154ef4460fd5cecfd78612ef39a0cc/PyInstaller-3.4.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 3.3MB/s
  Installing build dependencies ... done
Requirement already satisfied: setuptools in c:\users\75385\appdata\local\programs\python\python37-32\lib\site-packages (from pyinstaller) (40.6.2)
Collecting pefile>=2017.8.1 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/ed/cc/157f20038a80b6a9988abc06c11a4959be8305a0d33b6d21a134127092d4/pefile-2018.8.8.tar.gz (62kB)
    100% |████████████████████████████████| 71kB 5.2MB/s
Collecting macholib>=1.8 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/41/f1/6d23e1c79d68e41eb592338d90a33af813f98f2b04458aaf0b86908da2d8/macholib-1.11-py2.py3-none-any.whl
Collecting altgraph (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/0a/cc/646187eac4b797069e2e6b736f14cdef85dbe405c9bfc7803ef36e4f62ef/altgraph-0.16.1-py2.py3-none-any.whl
Collecting pywin32-ctypes (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/9e/4b/3ab2720f1fa4b4bc924ef1932b842edf10007e4547ea8157b0b9fc78599a/pywin32_ctypes-0.2.0-py2.py3-none-any.whl
Collecting future (from pefile>=2017.8.1->pyinstaller)
  Downloading https://files.pythonhosted.org/packages/90/52/e20466b85000a181e1e144fd8305caf2cf475e2f9674e797b222f8105f5f/future-0.17.1.tar.gz (829kB)
    100% |████████████████████████████████| 829kB 5.0MB/s
Installing collected packages: future, pefile, altgraph, macholib, pywin32-ctypes, pyinstaller
  Running setup.py install for future ... done
  Running setup.py install for pefile ... done
  Running setup.py install for pyinstaller ... done
Successfully installed altgraph-0.16.1 future-0.17.1 macholib-1.11 pefile-2018.8.8 pyinstaller-3.4 pywin32-ctypes-0.2.0

설치가 완료되었습니다.

 

exe 파일 만들기

테스트용 test.py 만들기

import datetime

if __name__ == "__main__" :
    print("Start.")

    cur_time = datetime.datetime.now()
    print("Current time : %s" % cur_time)
    
    print("End.")

현재 시간을 출력해주는 간단한 프로그램입니다.

 

test.py가 있는 디렉토리로 이동하여, pyinstaller 명령어를 입력해줍니다.

>> pyinstaller --onefile test.py

참고로 --onefile 이라는 옵션을 넣어주면, 하나의 실행파일로 생성이 됩니다. (참고로, --noconsole 옵션을 넣어주면 콘솔창이 뜨지 않고 실행이 됩니다.)

 

실행 결과

명령어를 실행하면, 이와같이 dist, build 등 여러개의 파일이 생성됩니다.

dist 폴더로 들어가보면, 아래와 같이 실행파일(test.exe)이 생성된것을 확인 할 수 있습니다.

 

더블 클릭하여 실행할 수 도 있고, cmd로 확인할 수도 있습니다.

 

실행 결과가 너무 빨라서 사진을 찍기 힘들어, cmd를 이용하여 실행 해봤습니다.

 

참고 - 실행파일에 이미지 넣기

실행파일의 아이콘을 바꾸고 싶다면, 실행파일로 빌드하는 명령어에 --icon 옵션을 추가해줍니다.

>> pyinstaller --icon=test.ico --onefile test.py

그리고 옵션값에 원하는 이미지의 경로를 넣어줍니다. 이미지의 포맷은 ico이어야 합니다.

(https://icoconvert.com/ 해당 사이트에서 ico 이미지로 convert 가능합니다.)

 

 

실행파일이 이쁘게 잘 만들어졌습니다.

 

출처 : https://hongku.tistory.com/338

반응형

+ Recent posts