2017-07-19 111 views
0

我已经在python中创建了一个GUI(使用Tkinter),它使用os.system('python_file.py')从GUI上单击按钮来运行python文件。 。我想通过保持Tkinter文件为主,使用pyinstaller将所有这些python文件捆绑到一个.exe文件中。如何使用pyinstaller将多个python文件编译为单个.exe文件

我做创建的.exe文件中的命令行:

pyinstaller --debug --onefile --noupx tkinter_app.py

目前我的.spec文件看起来像这样:

# -*- mode: python -*- 

block_cipher = None 

a = Analysis(['tkinter_app.py'],pathex=['C:\\test'],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='tkinter_app', debug=True, strip=False, upx=False,console=True) 

我不知道如何在上面的.spec文件中包含其他python文件,以便整个应用程序工作。有人可以帮忙吗?

+0

您可以按照下列步骤操作:https://stackoverflow.com/a/37140662/5114581 –

回答

0

最好的办法是使用数组DATAS

例如像这样:

a = Analysis(['.\\main.py'], 
      pathex=['.'], 
      binaries=None, 
      datas=[ ('.\\Ressources\\i18n', 'i18n'), 
      ('.\\Ressources\\file1.py', '.')], 
      hiddenimports=[], 
      hookspath=[], 
      runtime_hooks=[], 
      excludes=[], 
      win_no_prefer_redirects=False, 
      win_private_assemblies=False, 
      cipher=block_cipher) 

注意:确保把它放在正确的相对路径,以便你的程序就能够访问它

编辑:鉴于您的错误消息,问题不在与PyInstaller打包,而是在os.system命令。

使用os.system相当于打开一个DOS命令窗口,typping你的命令python_file.py

要访问您的Python文件,你必须知道:

  • PyInstaller解开你的压缩文件在一个临时文件夹,你可以在sys._MEIPASS中访问(只适用于.exe)
  • os.system可用于启动python,给出文件的完整路径,如下所示: os.system("python " + os.path.join(sys._MEIPASS, "python_file.py"))

    但要小心,这只会在python安装在系统上(并包含在syspath中)和exe中时才起作用。直接执行你的python文件会发送异常。

+0

感谢您的回答。我仍然无法通过建议更改来从GUI运行支持python文件。我得到一个错误,如'file1.py不被识别为内部或外部命令,可操作程序或批处理文件'。 SPEC FILE:a = Analysis(['C:\\ Python27 \\ Scripts \\ tkinter_app.py'],pathex = ['。'],binaries = None,datas = [('C:\\ Python27 \\ Tools \'i18n','i18n'),('C:\\ Python27 \\ Scripts \\ file1.py','。'), ('C:\\ Python27 \\ Scripts \\ file2.py', '','('C:\\ Python27 \\ Scripts \\ file3.py','。'), ('C:\\ Python27 \\ Scripts \\ file4.py','。'), ('C:\\ Python27 \\ Scripts \\ file5.py','。'),..... – sar678

+0

当我的电脑中安装了python时,这个功能完美无缺。没有安装? – sar678

+0

将你的python_file.py脚本打包成一个函数(例如称为python_file()),并从你的主脚本而不是os.system中调用它。 – DFE

相关问题