[英]How to create an executable file from python scripts for all windows?
[英]How to convert a python project into an executable file with all additional scripts?
我知道已經有很多關於這個的問題被問到,但沒有一個真正幫助我。 讓我解釋整個項目場景,以便更好地闡明我的問題。 目錄結構有點像下圖所示:
項目目錄布局
我需要將整個基於 GUI 的項目(主文件使用 Tkinter 模塊來創建 GUI)轉換為我可以與他人共享的 main.exe,同時確保所有其他文件的工作方式與我現在的工作方式完全相同通過命令提示符運行這個 main.py。 當我將此命令與 pyinstaller 一起使用時-
"pyinstaller --onefile --noconsole main.py"
它會創建 main.exe,它在運行時顯示“無法執行腳本”。 請詳細解釋我應該怎么做才能實現我上面所說的。 先感謝您。
pyinstaller 使用一些骯臟的技巧將一堆文件壓縮成一個
我建議使用 cx_Freeze 和 inno setup installer maker
在此處執行pip install cx_Freeze
以安裝它,並在此處執行 go 以進行 inno 設置
然后將以下內容復制到與項目相同的文件夾中名為 setup.py 的文件中
from cx_Freeze import setup, Executable
setup(name = "YOUR APP NAME" ,
version = "1.0.0" ,
description = "DESCRIPTION" ,
executables = [Executable("PYTHON FILE", base = "Win32GUI")]
)
最后運行python setup.py build
如果你想作為一個文件在這里下載這個文件
只需稍微編輯文件並使用inno編譯器制作安裝程序
假設我們的項目具有以下結構。
MyApp
|-models
| |-login.kv
|-data
| |-words.json
| |-audio.tar.gz
|-fonts
| |-FredokaOne.ttf
|-images
| |-gb.pngsound.png
| |-icon.ico
|-main.py
|-main.kv
|-draw.py
|-image.py
並取決於以下軟件包:
- kivy
- kivymd
- ffpyplayer
- gtts
cx_Freeze
。pip install cx_Freeze
setup.py
的文件中。# https://cx-freeze.readthedocs.io/en/latest/distutils.html
import sys
from cx_Freeze import setup, Executable
includes = []
# Include your files and folders
includefiles = ['models/','data/','fonts/','images/','main.kv','draw.py','image.py']
# Exclude unnecessary packages
excludes = ['cx_Freeze','pydoc_data','setuptools','distutils','tkinter']
# Dependencies are automatically detected, but some modules need help.
packages = ['kivy','kivymd', 'ffpyplayer','gtts']
base = None
shortcutName = None
shortcutDir = None
if sys.platform == "win32":
base = "Win32GUI"
shortcutName='My App'
shortcutDir="DesktopFolder"
setup(
name = 'MyApp',
version = '0.1',
description = 'Sample python app',
author = 'your name',
author_email = '',
options = {'build_exe': {
'includes': includes,
'excludes': excludes,
'packages': packages,
'include_files': includefiles}
},
executables = [Executable('main.py',
base = base, # "Console", base, # None
icon='images/icon.ico',
shortcutName = shortcutName,
shortcutDir = shortcutDir)]
)
python setup.py build
此命令將創建一個名為build
的子目錄,其中還有一個以字母exe.
並以 distutils 使用的平台的典型標識符結尾。 這允許構建多個平台而不會發生沖突。
在 Windows 上,您可以構建一個簡單的安裝程序,其中包含 cx_Freeze 為您的應用程序包含的所有文件,方法是運行安裝腳本:
python setup.py bdist_msi
步驟 1:將 Python 添加到 Windows 路徑
第 2 步:打開 Windows 命令提示符
第 3 步:安裝 Pyinstaller Package
第 4 步:保存您的 Python 腳本
第 5 步:使用 Pyinstaller 創建可執行文件
現在您將能夠使用 pyinstaller 從 Python 腳本創建可執行文件。
只需 go 到命令提示符,然后鍵入:
cd 后跟 Python 腳本的存儲位置
就我而言,我在命令提示符下輸入了以下內容:
cd C:\Users\Ron\Desktop\MyPython
接下來,使用以下模板創建可執行文件:
pyinstaller --onefile pythonScriptName.py
由於在我們的示例中,pythonScriptName 是“hello”,因此創建可執行文件的命令是:
pyinstaller --onefile hello.py
完成后,最后一次按 Enter。
第 6 步:運行可執行文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.