python 소스를 작성 exe 실행 파일 만들기

python post logo 파이썬

python으로 프로그램을 만들면 텍스트 파일(예 : test.py)로 저장하여 python.exe(python)또는 py.exe(py)로 실행할 수 있습니다. 하지만 소스를 공개하지 않고 다른 사람에게 이 프로그램을 배포하고자 한다면 확장자 .exe의 실행 파일(예 : test.exe)로 만들어서 배포해야 합니다. 이 글에서는 Hello World 문자를 콘솔 화면에 표시하는 python 프로그램 test.py를 만들어 test.exe 실행 파일로 컴파일 해주는 pyinstaller에 대해서 소개 하겠습니다.

pyinstaller 확장 패키지 설치

Python 추가 패키지의 설치 하는 pip 명령으로 python 소스를 컴파일하는 확장 패키지를 설치 합니다.

C:\>python -m pip install pyinstaller
Defaulting to user installation because normal site-packages is not writeable
Collecting pyinstaller
  Downloading pyinstaller-4.3.tar.gz (3.7 MB)
     |████████████████████████████████| 3.7 MB 1.7 MB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done

... 중간 생략 ...
Successfully built pyinstaller
Installing collected packages: future, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, altgraph, pyinstaller
    Running setup.py install for future ... done
    Running setup.py install for pefile ... done
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.3 pyinstaller-hooks-contrib-2021.1 pywin32-ctypes-0.2.0

소스를 작성해서 컴파일 하기

  1. 소스 파일 test.py를 작성합니다.
print("Hello World")
input("Press Enter to exit...")
  1. 에러가 없음을 확인하기 위해 python.exe(python)또는 py.exe(py)로 test.py을 실행합니다.
C:\test>py test.py
Hello World
Press Enter to exit...
  1. 다음과 같이 pyinstaller 명령을 사용하여 test.py를 컴파일해서 실행 파일 test.exe를 작성합니다.
D:\test>pyinstaller --onefile --console test.py
73 INFO: PyInstaller: 4.2
73 INFO: Python: 3.9.2
74 INFO: Platform: Windows-10-10.0.19041-SP0
76 INFO: wrote D:\test\test.spec
78 INFO: UPX is not available.
82 INFO: Extending PYTHONPATH with paths
['D:\\test', 'D:\\test']
... 중간 생략 ...
7244 INFO: Updating resource type 24 name 1 language 0
7250 INFO: Appending archive to EXE D:\test\dist\test.exe
7320 INFO: Building EXE from EXE-00.toc completed successfully.
D:\test>

–onefile 옵션을 지정하지 않는다면 test.py 파일과 함께 python39.dll 같은 파일을 함께 배포해야 합니다. 그러나 –onefile 옵션을 지정하면 exe 파일에 python39.dll 라이브러리가 포함되어있기 때문에 파일 크기는 커지지만 배포 파일을 하나로 줄일 수 있습니다.

  1. tree . /F 명령으로 하위 폴더와 파일을 전부 표시해서 test.exe가 작성되었음을 확인합니다.
D:\test>tree . /F
D:\TEST
│  test.py
│  ... 중간 생략 ...
├─dist
│      test.exe... 중간 생략 ...
  1. test.exe 파일이 존재하는 dist 폴더로 이동해서 실행합니다.
D:\test>cd .\dist
D:\test\dist>test.exe
Hello World
Press Enter to exit...

기타

test.exe 같은 실행 파일로 만들면 소스를 공개하지 않고 실행 파일만 제공할 수 있습니다.
실행 파일 작성 추가 패키지에 대한 사용 방법에 대해서는 pyinstaller –help 명령으로 도움말을 참조 하십시오.

제목과 URL을 복사했습니다