如何从我的 kivy 应用程序(Pyinstaller)获取 Windows 可执行文件? [英] How to get an windows executable from my kivy app (Pyinstaller)?

查看:24
本文介绍了如何从我的 kivy 应用程序(Pyinstaller)获取 Windows 可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个 kivy 应用程序,并使用 buildozer 打包成一个 .apk.事实是,现在我想用 Pyinstaller 为 windows 打包一个 .exe,但我已经意识到这两个程序(buildozer 和 Pyinstaller)的工作方式不同.我一直在寻找可以帮助我获取文件的好教程,但是我看到的所有教程都太简单了,并且没有解释例如如何导入 main.py 的外部文件(例如图像)以及如何导入外部模块(在 buildozer 中,我必须将我想要的库添加到 apk 文件中才能正常工作).我在 Ubuntu 中工作(如果我想获得 Windows 的可执行文件,我应该在 Windows 中工作吗?)并且我有添加到我的 .apk 的列表以正常工作.名单是:

I've done a kivy app and I packaged to an .apk with buildozer. The fact is that now I want to package in a .exe for windows with Pyinstaller but I have realised that this two programs (buildozer and Pyinstaller), don't work in the same way. I've been looking for a good tutorial that could help me to get the file, but all the tutorials I have seen are too simple and don't explain for example how to import external files of the main.py (e.g. images) and how to import external modules (In buildozer I had to add the libraries I wanted to the apk file to work properly). I'm working in Ubuntu ( Should I work in windows if I want to get an executable for windows?) and I have the list I added to my .apk to work properly. The list is:

requirements = kivy,sqlite3,requests,simplejson,icalendar,datetime,pytz,HTMLParser,email,openssl

如果有人能告诉我如何添加其他文件(main.py 是主文件,但我在 main.py 中导入了另外 2 个文件)我会很高兴,因为我已经尝试了很多次,直到它不起作用.

If somebody could tell me how to add the another files (main.py is the master file but I have 2 other files that are imported in main.py) I would be very pleased, because I have tried a lot of times and untill doesn't work.

推荐答案

你必须在 windows 环境下运行 PyInstaller yourfile.spec.我可以分享一个我正在使用的规范文件作为示例.

You have to run PyInstaller yourfile.spec on a windows environment. I can share a spec file I am using just as an example.

# -*- mode: python -*-

import os
from os.path import join

from kivy import kivy_data_dir
from kivy.deps import sdl2, glew
from kivy.tools.packaging import pyinstaller_hooks as hooks

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

datas = [
    (join('common', '*.ini'), 'common')
]

# 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]
bin_tocs = sdl2_bin_tocs + glew_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(['yourmain.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)


exe1 = EXE(pyz,
          a.scripts,
          name='mywindowsapplication',
          exclude_binaries=True,
          icon=join('images', 'mywinapp.ico'),
          debug=False,
          strip=False,
          upx=True,
          console=False)


coll = COLLECT(exe1,
               a.binaries,
               a.zipfiles,
               a.datas,
               *tocs,
               strip=False,
               upx=True,
               name='mywinapp')

将myfile.spec"文件放在yourmain.py"文件所在的同一目录中.然后从此目录运行 PyInstaller myfile.spec.将创建一个 build 和一个 dist 文件夹.在 dist 文件夹中,您可能会找到您的 exe.希望这能让你继续前进.

Put the 'myfile.spec' file in the same directory where your 'yourmain.py' file is present. Then run PyInstaller myfile.spec from this directory. A build and a dist folder will be created. In the dist folder you may find your exe. Hope this gets you going.

这篇关于如何从我的 kivy 应用程序(Pyinstaller)获取 Windows 可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆