为什么PyInstaller生成的exe导入失败?

2024-06-16 10:13:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我和PyInstaller有问题。我正在尝试构建主文件(Logical.py -> Logical.exe)。当我以python脚本的形式运行该文件时,它运行良好(终端:python Logical.py examples/led.lgc)。当我运行PyInstaller生成的exe(终端:./Logical examples/led.lgc)时,我收到以下错误消息:

Traceback (most recent call last):
  File "Logical.py", line 2, in <module>
    from pynput import keyboard
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "pynput\__init__.py", line 40, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "pynput\keyboard\__init__.py", line 31, in <module>
  File "pynput\_util\__init__.py", line 76, in backend
ImportError
[10688] Failed to execute script Logical

它似乎对pynput导入感到不安,尽管我不知道为什么。下面是我的源代码的导入loadingui都在项目目录中,SimpleAsi的源代码列在本文的底部

import sys, time, os, ctypes
from pynput import keyboard
from loading.loading import loadElement
from ui import vec2, widget, ansiManager
import simpleANSI as ANSI
import pdb

我运行了这两个存储库,因此我可以保证,在修复此问题之前,它们不会更改

我在Windows10x64、PyInstaller版本4.3、pynput版本1.7.3上使用Python 3.9.5

编辑:我翻阅了Python库文件,找到了回溯的目标。这是...\python\3.9.5\Lib\site-packages\pynput\_util\__init__.py中有问题的函数:

def backend(package):
    """Returns the backend module for a package.

    :param str package: The package for which to load a backend.
    """
    backend_name = os.environ.get(
        'PYNPUT_BACKEND_{}'.format(package.rsplit('.')[-1].upper()),
        os.environ.get('PYNPUT_BACKEND', None))
    if backend_name:
        modules = [backend_name]
    elif sys.platform == 'darwin':
        modules = ['darwin']
    elif sys.platform == 'win32':
        modules = ['win32']
    else:
        modules = ['xorg']

    errors = []
    resolutions = []
    for module in modules:
        try:
            return importlib.import_module('._' + module, package)
        except ImportError as e:
            errors.append(e)
            if module in RESOLUTIONS:
                resolutions.append(RESOLUTIONS[module])

    raise ImportError('this platform is not supported: {}'.format(    # AwesomeCronk: This is the offending line
        '; '.join(str(e) for e in errors)) + ('\n\n'
            'Try one of the following resolutions:\n\n'
            + '\n\n'.join(
                ' * {}'.format(s)
                for s in resolutions))
            if resolutions else '')

Tags: 文件inpyimportmodulesbackendpackagefor