【问题标题】:pyinstaller .exe not starting properlypyinstaller .exe 无法正常启动
【发布时间】:2018-03-30 21:45:06
【问题描述】:

您好,我正在学习 Python 以供学习。 现在我正在尝试使用 pyinstaller 来获取 .exe 文件,因为它更易于共享。

我的小程序使用 tkinter、numpy 和 matplotlib,但是当我尝试启动它时,什么都没有发生,这很奇怪,因为我在编译过程中没有出错:

    pyinstaller main.spec
531 INFO: PyInstaller: 3.4.dev0+133d18156
531 INFO: Python: 3.6.2
531 INFO: Platform: Windows-10-10.0.15063-SP0
531 INFO: UPX is not available.
531 INFO: Extending PYTHONPATH with paths
['C:\\Users\\Charles\\eclipse-workspace\\algo2\\src',
 'C:\\Users\\Charles\\eclipse-workspace\\algo2\\src']
531 INFO: checking Analysis
531 INFO: Building Analysis because out00-Analysis.toc is non existent
531 INFO: Initializing module dependency graph...
546 INFO: Initializing module graph hooks...
578 INFO: Analyzing base_library.zip ...
4672 INFO: Analyzing hidden import 'os'
4719 INFO: Analyzing hidden import 'stat'
4719 INFO: Analyzing hidden import 'ntpath'
4781 INFO: Analyzing hidden import 'genericpath'
4797 INFO: running Analysis out00-Analysis.toc
4812 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\charles\anaconda3\python.exe
5906 INFO: Caching module hooks...
5922 INFO: Analyzing main.py
5937 INFO: Loading module hooks...
5937 INFO: Loading module hook "hook-encodings.py"...
6078 INFO: Looking for ctypes DLLs
6078 INFO: Analyzing run-time hooks ...
6078 INFO: Looking for dynamic libraries
6078 INFO: Looking for eggs
6078 INFO: Using Python library c:\users\charles\anaconda3\python36.dll
6078 INFO: Found binding redirects:
[]
6109 INFO: Graph cross-reference written to C:\Users\Charles\eclipse-workspace\algo2\src\build\main\xref-main.html
6141 INFO: checking PYZ
6141 INFO: Building PYZ because out00-PYZ.toc is non existent
6141 INFO: Building PYZ (ZlibArchive) C:\Users\Charles\eclipse-workspace\algo2\src\build\main\out00-PYZ.pyz
6344 INFO: Building PYZ (ZlibArchive) C:\Users\Charles\eclipse-workspace\algo2\src\build\main\out00-PYZ.pyz completed successfully.
6344 INFO: checking PKG
6344 INFO: Building PKG because out00-PKG.toc is non existent
6344 INFO: Building PKG (CArchive) out00-PKG.pkg
6406 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
6422 INFO: Bootloader c:\users\charles\anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run_d.exe
6422 INFO: checking EXE
6422 INFO: Building EXE because out00-EXE.toc is non existent
6422 INFO: Building EXE from out00-EXE.toc
6422 INFO: Appending archive to EXE C:\Users\Charles\eclipse-workspace\algo2\src\build\main\main.exe
6516 INFO: Building EXE from out00-EXE.toc completed successfully.
6531 INFO: checking COLLECT
6531 INFO: Building COLLECT because out00-COLLECT.toc is non existent
6547 INFO: Building COLLECT out00-COLLECT.toc
7359 INFO: Building COLLECT out00-COLLECT.toc completed successfully.

所以我尝试使用调试进行编译,我没有发现有什么问题:

main.exe>log.txt
[3912] PyInstaller Bootloader 3.x
[3912] LOADER: executable is C:\Users\Charles\eclipse-workspace\algo2\src\dist\main\main.exe
[3912] LOADER: homepath is C:\Users\Charles\eclipse-workspace\algo2\src\dist\main
[3912] LOADER: _MEIPASS2 is NULL
[3912] LOADER: archivename is C:\Users\Charles\eclipse-workspace\algo2\src\dist\main\main.exe
[3912] LOADER: No need to extract files to run; setting extractionpath to homepath
[3912] LOADER: SetDllDirectory(C:\Users\Charles\eclipse-workspace\algo2\src\dist\main)
[3912] LOADER: Already in the child - running user's code.
[3912] LOADER: Python library: C:\Users\Charles\eclipse-workspace\algo2\src\dist\main\python36.dll
[3912] LOADER: Loaded functions from Python library.
[3912] LOADER: Manipulating environment (sys.path, sys.prefix)
[3912] LOADER: Pre-init sys.path is C:\Users\Charles\eclipse-workspace\algo2\src\dist\main\base_library.zip;C:\Users\Charles\eclipse-workspace\algo2\src\dist\main
[3912] LOADER: sys.prefix is C:\Users\Charles\eclipse-workspace\algo2\src\dist\main
[3912] LOADER: Setting runtime options
[3912] LOADER: Initializing python
[3912] LOADER: Overriding Python's sys.path
[3912] LOADER: Post-init sys.path is C:\Users\Charles\eclipse-workspace\algo2\src\dist\main\base_library.zip;C:\Users\Charles\eclipse-workspace\algo2\src\dist\main
[3912] LOADER: Setting sys.argv
[3912] LOADER: setting sys._MEIPASS
[3912] LOADER: importing modules from CArchive
[3912] LOADER: extracted struct
[3912] LOADER: callfunction returned...
[3912] LOADER: extracted pyimod01_os_path
[3912] LOADER: callfunction returned...
[3912] LOADER: extracted pyimod02_archive
[3912] LOADER: callfunction returned...
[3912] LOADER: extracted pyimod03_importers
[3912] LOADER: callfunction returned...
[3912] LOADER: Installing PYZ archive with Python modules.
[3912] LOADER: PYZ archive: out00-PYZ.pyz
[3912] LOADER: Running pyiboot01_bootstrap.py
[3912] LOADER: OK.
[3912] LOADER: Cleaning up Python interpreter.

这是我的 .spec 文件:

# -*- mode: python -*-
import sys

block_cipher = None


a = Analysis(['main.py'],
             pathex=['C:\\Users\\Charles\\eclipse-workspace\\algo2\\src'],
             binaries=[],
             datas=[],
             hiddenimports=['os', 'stat', 'ntpath', 'genericpath'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher
             )
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher
             )
exe = EXE(pyz,
          a.binaries + [('msvcp100.dll', 'C:\\Windows\\System32\\msvcp100.dll', 'BINARY'),
                        ('msvcr100.dll', 'C:\\Windows\\System32\\msvcr100.dll', 'BINARY')]
          if sys.platform == 'win32' else a.binaries,
          exclude_binaries=True,
          name='main',
          debug=False,
          strip=False,
          upx=False,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               name='main')

如果我使用 python 解释器运行它,我的程序可以正常工作。 尝试使用和不使用控制台启动 .exe,没有任何反应。

有什么想法吗?

谢谢

谢谢

【问题讨论】:

  • 要在代码中引用文件,您需要在使用 pyinstaller 时使用 sys._MEIPASS 作为基本路径 - 这是常见的错误来源。
  • 我尝试使用您的方法,我没有要添加的文件,所需的一切都在我的 main.py 中。现在我得到:site-package\PyInstaller\loader\pyiboot01_bootstrap.py 第 25 行,ModuleNotFindError:没有名为“os”的模块
  • 你导入操作系统了吗?
  • 不,我的程序不使用操作系统,这就是我尝试将其添加到规范文件的原因。它来自 pyinstaller 的 pyiboot01_boostrap.py 文件试图导入它但失败:文件“site-packages\PyInstaller\loader\pyiboot01_bootstrap.py”,第 25 行,在 ModuleNotFoundError: No module named 'os'
  • 那个模块是否导入了操作系统?

标签: python pyinstaller


【解决方案1】:

你是如何告诉 pyinstaller 编译 .py 文件的?

如果您使用 pip 安装了 pyinstaller,请尝试在与文件相同的目录中运行命令提示符。

pyinstaller mypythonapp.py

.exe 将位于名为 dist 的新目录中。要将 exe 作为一个文件获取,请在编译时在命令提示符中使用 --onefile 参数。

【讨论】:

  • 我尝试了使用和不使用 --onefile。如果我直接使用这些命令编译,我会不断收到错误消息:site-package\PyInstaller\loader\pyiboot01_bootstrap.py 第 25 行,ModuleNotFindError:没有名为“os”的模块
  • 你是在不导入的情况下尝试使用 os 吗?
  • Nop 我没有在我的代码中使用 os,它来自 pyinstaller 的 pyiboot01_boostrap.py 文件试图导入它但失败:文件“site-packages\PyInstaller\loader\pyiboot01_bootstrap.py”,第 25 行, 在 ModuleNotFoundError: No module named 'os'
  • 您是否尝试过编译一个只打印Hello, World! 的基本应用程序来检查 pyinstaller 是否可以在您的计算机上运行?
猜你喜欢
  • 2019-03-14
  • 2017-03-19
  • 1970-01-01
  • 2016-03-25
  • 2021-02-25
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
相关资源
最近更新 更多