无法pyinstall Kivy python程序

2024-06-26 14:32:47 发布

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

我无法让pyinstaller用python代码创建.exe。在

我想这一定和

A:我用Kivy做这个项目,而且

B:我想用Python做我的python解释器。在

在一段时间内,它会生成.exe文件,但当我启动它时,它会给出一堆关于没有可用窗口的错误。 我尝试安装/卸载软件包,在编辑器(PyCharm)上重新设置python解释器,最后重新安装了anaconda。现在当我尝试运行它时,它甚至不会创建.exe,它只会给我一个错误。在

这是我在终端执行的命令

(venv) C:\Users\Luc\PycharmProjects\GoogleCommandCenter>pyinstaller --onefile main.spec

这是我收到的错误信息

^{pr2}$

我也试过弄乱我的.spec文件。在这里

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=[
                'C:\\Users\\Luc\\PycharmProjects\\GoogleCommandCenter',
                'C:\\Users\\Luc\\PycharmProjects\\GoogleCommandCenter\\venv',
                'C:\\Users\\Luc\\PycharmProjects\\GoogleCommandCenter\\venv\\Scripts\\python.exe'
             ],
             binaries=[],
             datas=[],
             hiddenimports=[],
             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.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='main',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

Tags: 文件falsevenvmain错误blockexe解释器
1条回答
网友
1楼 · 发布于 2024-06-26 14:32:47

所以基本上这是一个完全的混乱,许多东西丢失或损坏。不过,万一有人需要帮助来解决这个问题,我会和大家分享我解决问题的方法。在

这是我现在的规格

# -*- mode: python -*-

"""
    First of make sure that you actually import whatever you need for your 
    app
"""

import os
from os.path import join

import sqlite3
import argparse
import pdfkit
import json
import datetime

from googleapiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

from kivy import kivy_data_dir
from kivy.deps import sdl2, glew, gstreamer
from kivy.tools.packaging import pyinstaller_hooks as hooks
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, NoTransition
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox

""" 
   Next make sure you have all of kivys dependencies 
"""

block_cipher = None
kivy_deps_all = hooks.get_deps_all()
kivy_factory_modules = hooks.get_factory_modules()

"""
    Then include any files that you may need for your app
"""

datas = [
    ('analyticsreporting\\analyticsreporting.dat', 'analyticsreporting'),
    ('classes\\DataManagement.py', 'classes'),
    ('classes\\GoogleAnalytics.py', 'classes'),
    ('classes\\OtherFunctions.py', 'classes'),
    ('classes\\ReportManagement.py', 'classes'),
    ('classes\\UserManagement.py', 'classes'),
    ('client_secrets\\client_secrets.json', 'client_secrets'),
    ('database\\lite.db', 'database'),
    ('screens\\ClientsScreen.py', 'screens'),
    ('screens\\HomeScreen.py', 'screens'),
    ('screens\\MyScreenManager.py', 'screens'),
    ('screens\\ScreenManagerApp.py', 'screens'),
    ('wkhtmltopdf\\wkhtmltopdf.exe', 'wkhtmltopdf')
]

# list of modules to exclude from analysis
excludes_a = ['Tkinter', '_tkinter', 'twisted', 'docutils', 'pygments']

# list of hiddenimports
hiddenimports = kivy_deps_all['hiddenimports'] + kivy_factory_modules

# binary data
sdl2_bin_tocs = [Tree(p) for p in sdl2.dep_bins]
glew_bin_tocs = [Tree(p) for p in glew.dep_bins]
gstreamer_bin_tocs = [Tree(p) for p in gstreamer.dep_bins]
bin_tocs = sdl2_bin_tocs + glew_bin_tocs + gstreamer_bin_tocs

# assets
kivy_assets_toc = Tree(kivy_data_dir, prefix=join('kivy_install', 'data'))
source_assets_toc = Tree('images', prefix='images')
assets_toc = [kivy_assets_toc, source_assets_toc]

tocs = bin_tocs + assets_toc

a = Analysis(['main.py'],
             pathex=[os.getcwd()],
             binaries=None,
             datas=datas,
             hiddenimports=hiddenimports,
             hookspath=[],
             runtime_hooks=[],
             excludes=excludes_a,
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)


pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

"""
    Put your *tocs underneath the a.data (this would go under the collect if 
    we wanted the app to be a dir and not a onefile)
"""    

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          *tocs,
          name='Google Analytics Command Center',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

其次,确保使用相对路径在python中引入文件 如图所示https://stackoverflow.com/a/13790741/8689803

最后,如果您要导入KIVY代码中的任何内容,那么请确保您首先在KIVY代码之外导入它,如下所示

进口

^{pr2}$

KIVY代码

main_widget = Builder.load_string('''
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import GoogleAnalytics classes.GoogleAnalytics.GoogleAnalytics
#:import MyScreenManager screens.MyScreenManager.MyScreenManager
#:import HomeScreen screens.HomeScreen.HomeScreen
#:import ClientsScreen screens.ClientsScreen.ClientsScreen
#:import resource_path classes.OtherFunctions.resource_path
""")

我还推荐了上面的代码

https://stackoverflow.com/a/37823174/8689803

谢谢大家的帮助。在

相关问题 更多 >