簡體   English   中英

執行使用 Pyinstaller 構建的 Python 應用程序會出現“無法執行腳本主程序”

[英]Executing Python application built with Pyinstaller gives “Failed to execute script main”

我有一個 python 程序,它使用tkinter作為 GUI。 我的 python 項目是 PyCharm 項目。 它被組織為

Project\main.py
Project\my_class.py
Project\images\favicon.ico
Project\common\util\util1.py
Project\common\util\util2.py
Project\venv

在終端內,我移動到C:\..\Desktop\Project文件夾並運行pyinstaller --windowed main.py
如果有人想知道終端中的 output 是什么,那就是

62 INFO: PyInstaller: 3.5
62 INFO: Python: 3.7.5
62 INFO: Platform: Windows-10-10.0.17763-SP0
62 INFO: wrote C:\Users\User\Desktop\Project\main.spec
62 INFO: UPX is not available.
62 INFO: Extending PYTHONPATH with paths
['C:\\Users\\User\\Desktop\\Project',
 'C:\\Users\\User\\Desktop\\Project']
62 INFO: checking Analysis
78 INFO: Building Analysis because Analysis-00.toc is non existent
78 INFO: Initializing module dependency graph...
78 INFO: Initializing module graph hooks...
78 INFO: Analyzing base_library.zip ...
2124 INFO: running Analysis Analysis-00.toc
2124 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\user\appdata\local\programs\python\python37\python.exe
3900 INFO: Caching module hooks...
3900 INFO: Analyzing C:\Users\User\Desktop\Project\main.py
4306 INFO: Processing pre-find module path hook   distutils
5040 INFO: Processing pre-find module path hook   site
5040 INFO: site: retargeting to fake-dir 'c:\\users\\user\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\fake-modules'
5925 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
7668 INFO: Loading module hooks...
7668 INFO: Loading module hook "hook-distutils.py"...
7671 INFO: Loading module hook "hook-encodings.py"...
7743 INFO: Loading module hook "hook-lib2to3.py"...
7747 INFO: Loading module hook "hook-numpy.core.py"...
7845 INFO: Loading module hook "hook-numpy.py"...
7846 INFO: Loading module hook "hook-pkg_resources.py"...
8092 INFO: Processing pre-safe import module hook   win32com
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
8150 INFO: Processing pre-safe import module hook   win32com
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
8327 INFO: Loading module hook "hook-pydoc.py"...
8328 INFO: Loading module hook "hook-setuptools.py"...
8742 INFO: Loading module hook "hook-sysconfig.py"...
8742 INFO: Loading module hook "hook-xml.dom.domreg.py"...
8742 INFO: Loading module hook "hook-xml.py"...
8742 INFO: Loading module hook "hook-_tkinter.py"...
9133 INFO: checking Tree
9133 INFO: Building Tree because Tree-00.toc is non existent
9148 INFO: Building Tree Tree-00.toc
9211 INFO: checking Tree
9211 INFO: Building Tree because Tree-01.toc is non existent
9211 INFO: Building Tree Tree-01.toc
9258 INFO: Looking for ctypes DLLs
9273 INFO: Analyzing run-time hooks ...
9273 INFO: Including run-time hook 'pyi_rth_pkgres.py'
9289 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
9289 INFO: Including run-time hook 'pyi_rth__tkinter.py'
9289 INFO: Looking for dynamic libraries
18180 INFO: Looking for eggs
18180 INFO: Using Python library c:\users\user\appdata\local\programs\python\python37\python37.dll
18180 INFO: Found binding redirects:
[]
18180 INFO: Warnings written to C:\Users\User\Desktop\Project\build\main\warn-main.txt
18445 INFO: Graph cross-reference written to C:\Users\User\Desktop\Project\build\main\xref-main.html
18492 INFO: checking PYZ
18492 INFO: Building PYZ because PYZ-00.toc is non existent
18492 INFO: Building PYZ (ZlibArchive) C:\Users\User\Desktop\Project\build\main\PYZ-00.pyz
19259 INFO: Building PYZ (ZlibArchive) C:\Users\User\Desktop\Project\build\main\PYZ-00.pyz completed successfully.
19274 INFO: checking PKG
19274 INFO: Building PKG because PKG-00.toc is non existent
19274 INFO: Building PKG (CArchive) PKG-00.pkg
19290 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
19290 INFO: Bootloader c:\users\user\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
19290 INFO: checking EXE
19290 INFO: Building EXE because EXE-00.toc is non existent
19290 INFO: Building EXE from EXE-00.toc
19290 INFO: Appending archive to EXE C:\Users\User\Desktop\Project\build\main\main.exe
19306 INFO: Building EXE from EXE-00.toc completed successfully.
19306 INFO: checking COLLECT
19306 INFO: Building COLLECT because COLLECT-00.toc is non existent
19306 INFO: Building COLLECT COLLECT-00.toc
22581 INFO: Building COLLECT COLLECT-00.toc completed successfully.

這里是錯誤
錯誤圖像

我怎么解決這個問題? 你有什么想法嗎?

嘗試設置 PATH 並包含所有必要的路徑

setlocal
set PATH=C:\Python37;%PATH%

pyinstaller -y -D --name Your_program_name--paths "C:\Windows\System32\downlevel" --windowed -i C:\ICONS\AI-2.ico --clean --distpath="." -p "c:\Python34\Lib\site-packages\whatever_is_needed" main.py

我也總是將 main.py 重命名為 main.pyw 如果仍然出現錯誤或什么都沒有,請嘗試從 cmd 運行 *.exe 並查看是否顯示任何錯誤。

解決了

  • 安裝pywin32修復ModuleNotFoundError: No module named 'win32com' in the Project venv\Lib\site-packages文件夾
  • Projectvenv\Lib\site-packages文件夾中安裝tornado
  • 打開終端並移動到項目文件夾
  • 運行setlocal
  • 運行set PATH=C:\Users\User\AppData\Local\Programs\Python\Python37\;%PATH%
  • 運行pyinstaller -y -D --paths "C:\Windows\System32\downlevel" --paths "C:\Users\User\Desktop\Project\venv\Lib\site-packages" main.py
  • images文件夾從Project復制到由pyinstallerdist文件夾中,以在我的 GUI 中獲取favicon.ico

現在我可以從終端或雙擊運行它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM