为什么这段代码编译后会抛出错误

Why is this code throwing error after compiling

我制作了一个脚本,它运行得很好,但是当我用 pyinstaller 编译它时

pyinstaller --onefile --icon='icon.ico' dmi.py

它会抛出错误,但如果我不编译它就可以工作

我知道 Github 模块抛出错误。这是抛出错误的代码:

import PySimpleGUIQt as Sg
from github import Github
from urllib import parse

g = Github("ghp_**********************************")
img_data = (g.get_user().get_repo('python-files')).get_contents(
    parse.quote('create.ico'), ref="main").content.replace('\n', '').encode()

layout = [
    [Sg.Text('                                                     ')]
]
window = Sg.Window("Add Tags", layout, icon=img_data, resizable=False, finalize=True)
while True:
    event, values = window.read()
    if event == Sg.WINDOW_CLOSED:
        window.close()
        break

下面的代码即使在编译后也能工作:

import base64
import PySimpleGUIQt as Sg
import requests
header = {"Authorization": "token ghp_*******************************",
          "Accept": "application/vnd.github.v4.raw"}
encoded = base64.b64encode((requests.get('https://raw.githubusercontent.com/WoLvES-2x0/python-files/main/create.ico', headers=header)).content)
layout = [
    [Sg.Text('                                                     ')]
]
window = Sg.Window("Add Tags", layout, icon=encoded, resizable=False, finalize=True)
while True:
    event, values = window.read()
    if event == Sg.WINDOW_CLOSED:
        window.close()
        break

虽然我可以用其他方式做到这一点,但没有这个我将面临的主要问题是

Get file name of all files in a repo

g_f_name = [str(i).replace('ContentFile(path="', '').replace('")', '') for i in (g.get_user().get_repo('node-files')).get_contents('') if i]

并且 commit changes to repo

repo.update_file(repo.get_contents('tags.txt').path, "committing files", tags_string, repo.get_contents('tags.txt').sha, branch="main")

错误是

C:\pythoncharm\pythonProject4\dist>dmi.exe
Traceback (most recent call last):
  File "dmi.py", line 2, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\__init__.py", line 56, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\MainClass.py", line 59, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\Event.py", line 32, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\NamedUser.py", line 44, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\Organization.py", line 50, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\Repository.py", line 125, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "github\PublicKey.py", line 34, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "nacl\public.py", line 17, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "nacl\bindings\__init__.py", line 17, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "nacl\bindings\crypto_aead.py", line 18, in <module>
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
  File "C:\Users\WoLvES\AppData\Local\Temp\embedded.rtin1sym.zip\shibokensupport\__feature__.py", line 142, in _import
ModuleNotFoundError: No module named '_cffi_backend'
[33116] Failed to execute script 'dmi' due to unhandled exception!

正在导入

import _cffi_backend

解决了这个问题,我认为 Pyinstaller 在编译时不知何故错过了这个模块,而 PyGithub 需要这个模块,虽然在编辑器中它变灰了但是它有效