[英]AttributeError with Scipy and Cython while converting Python to exe with cx_Freeze
[英]Problem while converting Python code to exe via cx_freeze
我正在嘗試將一個非常簡單的代碼轉換為 exe。 代碼使用 Kivy 如下;
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
#from kivy import Config
#Config.set('graphics', 'multisamples', '0')
class MyLayout(Widget):
def press(self, var_ex):
name = self.ids.name_input.text
self.ids.name_label.text = f"Hello {name} !" * var_ex
class MyApp(App):
def build(self):
return MyLayout()
if __name__.endswith('__main__'):from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_string("""
<MyLayout>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
spacing: 20
padding: 50
Button:
text: "English"
Button:
text: "Italian"
size_hint: ( None, None)
width: 100
height: 50
pos_hint: { "center_x": 0.5}
Button:
text: "German"
size_hint: ( 0.5, 0.5)
""")
class MyLayout(Widget):
pass
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
MyApp().run()
此代碼在 python 中運行良好,沒有錯誤。 但是當我嘗試通過此 setup.py 文件和 cx_freeze 將其轉換為 exe 時;
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["kivy","pygame","kivy_deps.gstreamer"], "includes": ["kivy_deps.glew","kivy_deps.sdl2","wheel","setuptools","pygments","docutils","win32api","PIL"], "excludes": []}
#base = ""
base = "Win32GUI"
setup(
name="guifoo",
version="0.1",
description="My GUI application!",
options={"build_exe": build_exe_options},
executables=[Executable("test.py", base=base)],
)
運行創建的 exe 文件時出現以下錯誤(exe 文件創建時沒有錯誤);
Traceback (most recent call last):
File "D:\Python310\kivy_venv\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 113, in run
module_init.run(name + "__main__")
File "D:\Python310\kivy_venv\Lib\site-packages\cx_Freeze\initscripts\Console.py", line 15, in run
exec(code, module_main.__dict__)
File "test.py", line 1, in <module>
File "D:\Python310\kivy_venv\lib\site-packages\kivy\__init__.py", line 317, in <module>
mod = importer.find_module(modname).load_module(modname)
File "D:\Python310\kivy_venv\lib\site-packages\kivy_deps\glew\__init__.py", line 23, in <module>
p = join(d, 'share', 'glew', 'bin')
File "D:\Python310\lib\ntpath.py", line 78, in join
path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not NoneType
有人可以幫我嗎? 提前致謝...
PS 如果需要,我可以提供更多信息。
所以我仍然無法弄清楚 cx_freeze 的問題是什么,但是我能夠讓它與 PyInstaller 一起工作。 這些是我采取的步驟。
pip install pyinstaller
cp test.py test
在test
文件中, if __name__ ==
如下所示,我必須同時刪除兩者。
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
#from kivy import Config
#Config.set('graphics', 'multisamples', '0')
class MyLayout(Widget):
def press(self, var_ex):
name = self.ids.name_input.text
self.ids.name_label.text = f"Hello {name} !" * var_ex
class MyApp(App):
def build(self):
return MyLayout()
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_string("""
<MyLayout>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
spacing: 20
padding: 50
Button:
text: "English"
Button:
text: "Italian"
size_hint: ( None, None)
width: 100
height: 50
pos_hint: { "center_x": 0.5}
Button:
text: "German"
size_hint: ( 0.5, 0.5)
""")
class MyLayout(Widget):
pass
class MyApp(App):
def build(self):
return MyLayout()
MyApp().run()
然后
pyinstaller -F --noconsole test
這將在dist
目錄中創建可執行文件。 我測試過,這很有效。 我仍然不知道為什么 cx_freeze 不起作用。
如果您的目標是將程序轉換為 .exe,我建議使用PyInstaller 。 您可以在命令行中使用命令pip install pyinstaller
輕松安裝它。 而且也不需要任何 setup.py 文件。
安裝后,只需導航到要轉換為 .exe 的文件的目錄(我們稱之為myFile.py
)並在命令行中鍵入以下內容:
pyinstaller myFile.py
這將在該目錄中創建一些東西:
myFile.spec
的文件build
的文件夾dist
的文件夾其中唯一重要的是dist
文件夾。 在其中,您將找到可以作為 .exe 運行的 Python 腳本的 .exe。 但是,如果您要從與myFile.py
相關的目錄中導入運行.py
腳本所需的文件,只需將myFile.exe
移動到與myFile.py
相同的目錄即可。
在轉換為 .exe 時,您還可以添加其他參數,您可以在 pyinstaller 網站上看到這些參數(即,您可以通過在命令末尾添加--noconsole
來刪除運行 .exe 時出現的控制台)。
希望這能回答你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.