Development/파이썬 [Python]

[Python] 리눅스 환경에서 파이썬 배포파일 만들기

요체크다 2023. 11. 4. 00:37
반응형

최근 리눅스 환경의 경우 기본적으로 파이썬이 설치가 되어 있어 파이썬 소스파일을 바로 실행시킬 수 있습니다.

다만, 이 경우 배포가 필요한 경우 소스파일을 공유해야 하는 문제가 발생하게 됩니다.

 

이 전에 공유해 드린 Windows 환경에서와 같이 리눅스 환경에서도 pyinstaller 를 통해 배포파일을 생성하는 방법을 알아 보도록 하겠습니다.

 

우선 저의 리눅스 테스트 환경은 아래아 같습니다.

[root@instance1 dist]# uname -a
Linux instance1 4.18.0-408.el8.x86_64 #1 SMP Mon Jul 18 17:42:52 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

[root@instance1 tmp]# cat /etc/*release
CentOS Stream release 8
NAME="CentOS Stream"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Stream 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"
CentOS Stream release 8
CentOS Stream release 8

 

가장 먼저 pip3 를 통해 pyinstaller 를 설치해 줍니다.

[root@instance1 tmp]# pip3 install pyinstaller==3.6
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pyinstaller==3.6
Downloading https://files.pythonhosted.org/packages/3c/c9/c3f9bc64eb11eee6a824686deba6129884c8cbdf70e750661773b9865ee0/PyInstaller-3.6.tar.gz (3.5MB)
100% |████████████████████████████████| 3.5MB 91kB/s
Requirement already satisfied: setuptools in /usr/lib/python3.6/site-packages (from pyinstaller==3.6)
Requirement already satisfied: altgraph in /usr/local/lib/python3.6/site-packages (from pyinstaller==3.6)
Building wheels for collected packages: pyinstaller
Running setup.py bdist_wheel for pyinstaller ... done
Stored in directory: /root/.cache/pip/wheels/62/fe/62/4c0f196d1e0dd689e097449bc81d7d585a7de7dd86b081b80b
Successfully built pyinstaller
Installing collected packages: pyinstaller
Successfully installed pyinstaller-3.6

 

그리고 pyinstaller 를 통해 배포 파일을 만들어 주면 됩니다.

 # pyinstaller -F .py파일 (-F : 하나의 파일로 생성해 주는 옵션, --onefile 옵션 동일.)

[root@instance1 tmp]# pyinstaller -F test.py
171 INFO: PyInstaller: 3.6
172 INFO: Python: 3.6.8
172 INFO: Platform: Linux-4.18.0-408.el8.x86_64-x86_64-with-centos-8
173 INFO: wrote /tmp/test.spec
175 INFO: UPX is not available.
176 INFO: Extending PYTHONPATH with paths
['/tmp', '/tmp']
176 INFO: checking Analysis
177 INFO: Building Analysis because Analysis-00.toc is non existent
177 INFO: Initializing module dependency graph...
179 INFO: Caching module graph hooks...
184 INFO: Analyzing base_library.zip ...
9885 INFO: Caching module dependency graph...
10288 INFO: running Analysis Analysis-00.toc
10478 INFO: Analyzing /tmp/test.py
10483 INFO: Processing module hooks...
10483 INFO: Loading module hook "hook-encodings.py"...
10780 INFO: Loading module hook "hook-pydoc.py"...
10782 INFO: Loading module hook "hook-xml.py"...
12070 INFO: Looking for ctypes DLLs
12070 INFO: Analyzing run-time hooks ...
12078 INFO: Looking for dynamic libraries
13693 INFO: Looking for eggs
13693 INFO: Using Python library /lib64/libpython3.6m.so.1.0
13697 INFO: Warnings written to /tmp/build/test/warn-test.txt
13782 INFO: Graph cross-reference written to /tmp/build/test/xref-test.html
13888 INFO: checking PYZ
13888 INFO: Building PYZ because PYZ-00.toc is non existent
13888 INFO: Building PYZ (ZlibArchive) /tmp/build/test/PYZ-00.pyz
15681 INFO: Building PYZ (ZlibArchive) /tmp/build/test/PYZ-00.pyz completed successfully.
15684 INFO: checking PKG
15684 INFO: Building PKG because PKG-00.toc is non existent
15684 INFO: Building PKG (CArchive) PKG-00.pkg
26294 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
26297 INFO: Bootloader /usr/local/lib64/python3.6/site-packages/PyInstaller/bootloader/Linux-64bit/run
26297 INFO: checking EXE
26297 INFO: Building EXE because EXE-00.toc is non existent
26297 INFO: Building EXE from EXE-00.toc
26297 INFO: Appending archive to ELF section in EXE /tmp/dist/test
26445 INFO: Building EXE from EXE-00.toc completed successfully.

 

마지막으로 배포파일의 경우 Windows와 동일하게 dist 디렉토리에 생성되며, 배포파일을 실행시켜보면 정상적으로 실행 되는 것을 확인할 수 있습니다.

[root@instance1 tmp]# cd ./dist/
[root@instance1 dist]# ls -l
total 6152
-rwxr-xr-x. 1 root root 6298912 Nov 4 00:19 test
[root@instance1 dist]# ./test
CentOS Stream release 8

반응형