首页 > 解决方案 > 在 Win10 上安装 Kivy 应用似乎没有从 Virtualenv 获取 python

问题描述

我已经使用带有 python3.7 的虚拟环境编写了一个 Kivy 应用程序(如下所示),这是 Kivy 的要求。然而,我的系统使用 python3.8。Kivy 应用程序工作正常,并根据需要使用 python3.7。

我按照此处的说明使用了 Pyinstaller ,一切运行良好。

我的问题是,一旦构建了应用程序并双击 .exe,一个窗口会短暂打开和关闭,然后什么也没有发生。

我不知道这是否是问题所在,但是当我查看/dist目录时,我发现python38.dll这让我认为 Pyinstaller 选择了错误版本的 python。如果是这样的话,那么我认为 Pyinstaller 可能没有从我的 Virtualenv 中获取任何其他内容。我不够专业,不知道这是否是问题所在,或者如何让 Pyinstaller 使用我的 Virtualenv 或不同的 python 解释器。

这是供参考的代码等。

基维应用

from kivy.app import App
from kivy.uix.label import Label

class Play(App):

    def build(self):
        l = Label(text="Hello World")
        return l

if __name__ == '__main__':
    Play().run()

Pyinstaller 规范文件

# -*- mode: python ; coding: utf-8 -*-

# byx start
from kivy_deps import sdl2, glew
# byx end

block_cipher = None


a = Analysis(['C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test\\simple.py'],
             pathex=['C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='winkivy',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True, 
          icon='C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test\\logo.ico')

# byx start
# Add the Tree statements for all dependencies. Everything in the directory gets added.
# 1st -- The home directory with scripts, byx, icon.    
# 2nd -- Kivy dependencies, a list of directories. 
coll = COLLECT(exe,
                Tree('C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test\\'),
               a.binaries,
               a.zipfiles,
               a.datas,
                *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               upx_exclude=[],
               name='winkivy')
# byx end

这是 Pyinstaller 构建的日志。

.venv) c:\Users\mark\OneDrive\dev\babyclix\_staging\_test>python -m PyInstaller main.spec  
122 INFO: PyInstaller: 4.0
122 INFO: Python: 3.8.0
123 INFO: Platform: Windows-10-10.0.19041-SP0
126 INFO: UPX is not available.
142 INFO: Extending PYTHONPATH with paths
['C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test',
 'C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test']
176 INFO: checking Analysis
177 INFO: Building Analysis because Analysis-00.toc is non existent
177 INFO: Initializing module dependency graph...
183 INFO: Caching module graph hooks...
213 INFO: Analyzing base_library.zip ...
6077 INFO: Caching module dependency graph...
6219 INFO: running Analysis Analysis-00.toc
6224 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\mark\AppData\Local\Programs\Python\Python38\python.exe
6609 INFO: Analyzing C:\Users\mark\OneDrive\dev\babyclix\_staging\_test\main.py
6711 INFO: Processing module hooks...
6712 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\mark\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\PyInstaller\\hooks'...
6829 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\mark\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\PyInstaller\\hooks'...
7171 INFO: Looking for ctypes DLLs
7177 INFO: Analyzing run-time hooks ...
7185 INFO: Including run-time hook 'C:\\Users\\mark\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
7192 INFO: Looking for dynamic libraries
7398 INFO: Looking for eggs
7399 INFO: Using Python library C:\Users\mark\AppData\Local\Programs\Python\Python38\python38.dll
7400 INFO: Found binding redirects:
[]
7413 INFO: Warnings written to c:\Users\mark\OneDrive\dev\babyclix\_staging\_test\build\main\warn-main.txt
7464 INFO: Graph cross-reference written to c:\Users\mark\OneDrive\dev\babyclix\_staging\_test\build\main\xref-main.html
7485 INFO: checking PYZ
7486 INFO: Building PYZ because PYZ-00.toc is non existent
7486 INFO: Building PYZ (ZlibArchive) c:\Users\mark\OneDrive\dev\babyclix\_staging\_test\build\main\PYZ-00.pyz
8171 INFO: Building PYZ (ZlibArchive) c:\Users\mark\OneDrive\dev\babyclix\_staging\_test\build\main\PYZ-00.pyz completed successfully.
8189 INFO: checking PKG
8189 INFO: Building PKG because PKG-00.toc is non existent
8190 INFO: Building PKG (CArchive) PKG-00.pkg
8221 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
8223 INFO: Bootloader C:\Users\mark\AppData\Local\Programs\Python\Python38\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
8224 INFO: checking EXE
8224 INFO: Building EXE because EXE-00.toc is non existent
8227 INFO: Building EXE from EXE-00.toc
8239 INFO: Copying icons from ['C:\\Users\\mark\\OneDrive\\dev\\babyclix\\_staging\\_test\\logo.ico']
8327 INFO: Writing RT_GROUP_ICON 0 resource with 90 bytes
8327 INFO: Writing RT_ICON 1 resource with 17255 bytes
8328 INFO: Writing RT_ICON 2 resource with 67624 bytes
8328 INFO: Writing RT_ICON 3 resource with 16936 bytes
8329 INFO: Writing RT_ICON 4 resource with 9640 bytes
8334 INFO: Writing RT_ICON 5 resource with 4264 bytes
8335 INFO: Writing RT_ICON 6 resource with 1128 bytes
8339 INFO: Appending archive to EXE c:\Users\mark\OneDrive\dev\babyclix\_staging\_test\build\main\babyclix.exe
8421 INFO: Building EXE from EXE-00.toc completed successfully.
8425 INFO: checking Tree
8429 INFO: Building Tree because Tree-00.toc is non existent
8429 INFO: Building Tree Tree-00.toc
8435 INFO: checking Tree
8435 INFO: Building Tree because Tree-01.toc is non existent
8435 INFO: Building Tree Tree-01.toc
8437 INFO: checking Tree
8438 INFO: Building Tree because Tree-02.toc is non existent
8438 INFO: Building Tree Tree-02.toc
8442 INFO: checking Tree
8448 INFO: Building Tree because Tree-03.toc is non existent
8448 INFO: Building Tree Tree-03.toc
8452 INFO: checking COLLECT
8452 INFO: Building COLLECT because COLLECT-00.toc is non existent
8453 INFO: Building COLLECT COLLECT-00.toc
8990 INFO: Building COLLECT COLLECT-00.toc completed successfully.

标签: pythonkivyvirtualenvpyinstaller

解决方案


事实证明,问题不在于 Kivy 或 PyInstaller,而在于 VSCode 以及它选择 python 解释器的方式。

基本上,VSCode 中的终端窗口不一定使用您在调色板或激活的虚拟环境中选择的 python 解释器。因此,如果您的应用程序对版本敏感,或者您希望在终端窗口中使用 venv,您可能会遇到问题。

这个问题已经在很多地方提出过,但从来没有得到正确的回答,人们直接得出结论,提出问题的人是在询问如何“选择 python 解释器”并通过说“使用命令调色板”来回答问题。这并不能解决“终端窗口”问题。

另请注意,状态栏中显示的 python 解释器也不是答案,即用于运行您的应用程序的解释器。您的应用程序可能运行良好,但您仍然遇到终端窗口问题。

这里这里

我将结束这个问题并提出一个更具体的问题。见这里


推荐阅读