แนะนำวิธีการทำไฟล์ .py ให้เป็นไฟล .exe โดยใช้ module pyinstaller

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

tatiya
PHP VIP Members
PHP VIP Members
โพสต์: 2845
ลงทะเบียนเมื่อ: 09/07/2018 9:35 am

แนะนำวิธีการทำไฟล์ .py ให้เป็นไฟล .exe โดยใช้ module pyinstaller

โพสต์ที่ยังไม่ได้อ่าน โดย tatiya »

PyInstaller คือ เครื่องมือที่ช่วยการแปลงโปรแกรมที่เขียนด้วยไพทอนเป็น execute binary file ที่สามารถนำไปรันได้โดยที่เครื่องคอมพิวเตอร์ปลายทางไม่ต้องติดตั้งไพทอน สำหรับ PyInstaller เป็น cross-platform สามารถใช้งานได้บนวินโดส์ แมค และลีนุกซ์ สนับสนุนไพทอนรุ่น 2.7 และ ไพทอน รุ่น 3.3 ถึง 3.6 จุดมุ่งหมายของ PyInstaller คือต้องการช่วยผู้ใช้ในการแปลงโปรแกรมไพทอน ที่ใช้มูดูลไลบรารีภายนอกเช่น Matplotlib, DJango, wxPython, PyQt เป็นต้น ให้สามารถทำได้ง่ายสะดวก

วันนี้ผมจะมาแนะนำวิธีง่ายสำหรับการเปลียนไฟล์ .py ให้เป็นไฟล์ .exe นะครับ เอ่ออส่วนตัวผมใช้ระบบปฎิบัติการ Utuntu 14.04 LTS นะครับ
หมายเหตุ : อย่าพึ่งไปสนใจว่าผมจะใช้ OS อะไร เพราะวันนี้ผมจะนำแนะวิธีทำโดยผ่าน ตัว terminal ของ Pycharm

อันดับแรกให้เรา ติดตั้งตัว pyinstaller ก่อนนะครับ

โค้ด: เลือกทั้งหมด

pip install pyinstaller
teminal ของ Pycharm จะอยู่ตรงนี้
Selection_049.png
Selection_049.png (89.15 KiB) Viewed 2093 times
ถ้าไม่ได้ให้ sudo อัตเดฟ pip ลองดูนะครับ

โอเครพอติดตั้งเสร็จแล้วให้มาเริ่มกันเลย
ลองสร้างไฟล์ pythone ขึ้นมา 1 ไฟล์ ผมยกตัวอย่างของผมจะเป็น text_exe.py
แล้วเปิด terminal ของ pycharm ขึ้นมา

โค้ด: เลือกทั้งหมด

pyinstaller text_exe.py
ผลลัพท์

โค้ด: เลือกทั้งหมด

(venv) com008@com008-K45A:~/PycharmProjects/test_exe$ pyinstaller test_exe.py
62 INFO: PyInstaller: 3.3.1
62 INFO: Python: 3.4.3
63 INFO: Platform: Linux-3.16.0-77-generic-x86_64-with-Ubuntu-14.04-trusty
63 INFO: wrote /home/com008/PycharmProjects/test_exe/test_exe.spec
65 INFO: UPX is not available.
68 INFO: Extending PYTHONPATH with paths
['/home/com008/PycharmProjects/test_exe',
 '/home/com008/PycharmProjects/test_exe']
68 INFO: checking Analysis
69 INFO: Building Analysis because out00-Analysis.toc is non existent
69 INFO: Initializing module dependency graph...
70 INFO: Initializing module graph hooks...
73 INFO: Analyzing base_library.zip ...
2326 INFO: Processing pre-find module path hook   distutils
4238 INFO: running Analysis out00-Analysis.toc
4276 INFO: Caching module hooks...
4280 INFO: Analyzing /home/com008/PycharmProjects/test_exe/test_exe.py
4843 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
5800 INFO: Loading module hooks...
5801 INFO: Loading module hook "hook-distutils.py"...
5802 INFO: Loading module hook "hook-pydoc.py"...
5802 INFO: Loading module hook "hook-selenium.py"...
5829 INFO: Loading module hook "hook-sysconfig.py"...
5831 INFO: Loading module hook "hook-xml.dom.domreg.py"...
5832 INFO: Loading module hook "hook-encodings.py"...
5911 INFO: Loading module hook "hook-xml.py"...
5928 INFO: Looking for ctypes DLLs
6020 INFO: Analyzing run-time hooks ...
6036 INFO: Looking for dynamic libraries
6302 INFO: Looking for eggs
6303 INFO: Python library not in binary dependencies. Doing additional searching...
6357 INFO: Using Python library /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0
6363 INFO: Warnings written to /home/com008/PycharmProjects/test_exe/build/test_exe/warntest_exe.txt
6414 INFO: Graph cross-reference written to /home/com008/PycharmProjects/test_exe/build/test_exe/xref-test_exe.html
6469 INFO: checking PYZ
6469 INFO: Building PYZ because out00-PYZ.toc is non existent
6469 INFO: Building PYZ (ZlibArchive) /home/com008/PycharmProjects/test_exe/build/test_exe/out00-PYZ.pyz
6950 INFO: Building PYZ (ZlibArchive) /home/com008/PycharmProjects/test_exe/build/test_exe/out00-PYZ.pyz completed successfully.
6987 INFO: checking PKG
6987 INFO: Building PKG because out00-PKG.toc is non existent
6987 INFO: Building PKG (CArchive) out00-PKG.pkg
7012 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
7014 INFO: Bootloader /home/com008/PycharmProjects/test_exe/venv/lib/python3.4/site-packages/PyInstaller/bootloader/Linux-64bit/run
7015 INFO: checking EXE
7015 INFO: Building EXE because out00-EXE.toc is non existent
7015 INFO: Building EXE from out00-EXE.toc
7015 INFO: Appending archive to ELF section in EXE /home/com008/PycharmProjects/test_exe/build/test_exe/test_exe
7033 INFO: Building EXE from out00-EXE.toc completed successfully.
7036 INFO: checking COLLECT
7036 INFO: Building COLLECT because out00-COLLECT.toc is non existent
7036 INFO: Building COLLECT out00-COLLECT.toc
7083 INFO: Building COLLECT out00-COLLECT.toc completed successfully.
(venv) com008@com008-K45A:~/PycharmProjects/test_exe$ 
ไฟล์ที่ได้ทั้งหมด
Selection_050.png
Selection_050.png (56.27 KiB) Viewed 2093 times
ให้เราคลิกเข้าไปดูที่ dist>>test_exe เลื่อนๆดูจะเห็น
Selection_052.png
Selection_052.png (76.97 KiB) Viewed 2093 times
ถือว่าสำเร็จครับ

:!: :?: แต่เราจะทำให้มันเป็นไฟล์ .exe ให้เป็นไฟล์เดียวได้อย่างไรล่ะ มาดูต่อเลยครับ

ผมสร้างไฟล์ .py ขึ้นมาใหม่ชื่อ test_exe_onefile.py
แล้วเปิด terminal ของ pycharm ขึ้นมา

โค้ด: เลือกทั้งหมด

pyinstaller test_exe_onefile.py --onefile
ผลลัพท์

โค้ด: เลือกทั้งหมด

(venv) com008@com008-K45A:~/PycharmProjects/test_exe$ pyinstaller test_exe_onefile.py --onefile
128 INFO: PyInstaller: 3.3.1
128 INFO: Python: 3.4.3
129 INFO: Platform: Linux-3.16.0-77-generic-x86_64-with-Ubuntu-14.04-trusty
129 INFO: wrote /home/com008/PycharmProjects/test_exe/test_exe_onefile.spec
131 INFO: UPX is not available.
133 INFO: Extending PYTHONPATH with paths
['/home/com008/PycharmProjects/test_exe',
 '/home/com008/PycharmProjects/test_exe']
133 INFO: checking Analysis
133 INFO: Building Analysis because out00-Analysis.toc is non existent
133 INFO: Initializing module dependency graph...
135 INFO: Initializing module graph hooks...
146 INFO: Analyzing base_library.zip ...
2940 INFO: Processing pre-find module path hook   distutils
4959 INFO: running Analysis out00-Analysis.toc
5019 INFO: Caching module hooks...
5047 INFO: Analyzing /home/com008/PycharmProjects/test_exe/test_exe_onefile.py
5780 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
6789 INFO: Loading module hooks...
6789 INFO: Loading module hook "hook-distutils.py"...
6790 INFO: Loading module hook "hook-sysconfig.py"...
6791 INFO: Loading module hook "hook-xml.py"...
6792 INFO: Loading module hook "hook-pydoc.py"...
6793 INFO: Loading module hook "hook-encodings.py"...
6863 INFO: Loading module hook "hook-xml.dom.domreg.py"...
6863 INFO: Loading module hook "hook-selenium.py"...
6884 INFO: Looking for ctypes DLLs
6928 INFO: Analyzing run-time hooks ...
6939 INFO: Looking for dynamic libraries
7173 INFO: Looking for eggs
7173 INFO: Python library not in binary dependencies. Doing additional searching...
7188 INFO: Using Python library /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0
7194 INFO: Warnings written to /home/com008/PycharmProjects/test_exe/build/test_exe_onefile/warntest_exe_onefile.txt
7247 INFO: Graph cross-reference written to /home/com008/PycharmProjects/test_exe/build/test_exe_onefile/xref-test_exe_onefile.html
7289 INFO: checking PYZ
7289 INFO: Building PYZ because out00-PYZ.toc is non existent
7289 INFO: Building PYZ (ZlibArchive) /home/com008/PycharmProjects/test_exe/build/test_exe_onefile/out00-PYZ.pyz
7766 INFO: Building PYZ (ZlibArchive) /home/com008/PycharmProjects/test_exe/build/test_exe_onefile/out00-PYZ.pyz completed successfully.
7802 INFO: checking PKG
7802 INFO: Building PKG because out00-PKG.toc is non existent
7803 INFO: Building PKG (CArchive) out00-PKG.pkg
10191 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
10199 INFO: Bootloader /home/com008/PycharmProjects/test_exe/venv/lib/python3.4/site-packages/PyInstaller/bootloader/Linux-64bit/run
10199 INFO: checking EXE
10199 INFO: Building EXE because out00-EXE.toc is non existent
10199 INFO: Building EXE from out00-EXE.toc
10200 INFO: Appending archive to ELF section in EXE /home/com008/PycharmProjects/test_exe/dist/test_exe_onefile
10218 INFO: Building EXE from out00-EXE.toc completed successfully.
(venv) com008@com008-K45A:~/PycharmProjects/test_exe$ 
ไฟล์ที่ได้ ให้เข้าไปดูที่ dist
Selection_053.png
Selection_053.png (59.21 KiB) Viewed 2093 times
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 42