在PyInstaller中打包多个脚本 [英] Packaging multiple scripts in PyInstaller

查看:282
本文介绍了在PyInstaller中打包多个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyInstaller将两个脚本转换为一个可执行文件,其中一个调用另一个.我遇到的问题是我无法弄清楚如何将这两个脚本捆绑在一起,仍然让它们彼此引用:

I'm using PyInstaller to turn two scripts into one executable file, one of which calls the other. The issue I'm having is I can't figure out how to bundle the two scripts and still let them reference each other:

导致此问题的代码是一个脚本script1.py包含:

The code that causes the issue is that one script, script1.py contains:

subprocess.call(['gksudo','python script2.py'])

当我正常运行脚本时,这很好用,但是一旦将它们打包在PyInstaller中,我便不知道如何使调用正常进行.

This works fine when I run the scripts normally, but once they're packaged in PyInstaller I don't know how to make the call work.

推荐答案

我不认为pyinstaller可以自行处理这种捆绑,至少我没有设法配置它.我还有一个相当大的应用程序,其中有一些对

I'm don't think pyinstaller can handle this kind of bundling on it's own, at least I didn't manage to configure it if possible. I also have a rather large application where some calls to

subprocess.Popen('python'...)

subprocess.Popen('python ' ... )

完成.我终于使它起作用的方式是:

are done. The way I finally made it work was:

  1. 将子流程调用修改为不同的python,例如subprocess.call(['gksudo','./python script2.py']).根据您的情况,创建两个单独的分析,一个用于入口点,另一个用于其余脚本:

  1. Modify your subprocess calls to a different python, like subprocess.call(['gksudo','./python script2.py']). Create two separate analysis, one for the entry point, and one for the rest of the scripts, in your case:

a1-script1.py的分析 a2-对script2.py

a1 - analysis of script1.py a2 - analysis of script2.py

仅从入口点脚本创建exe:

Create the exe only from the entry point scripts:

pyz = PYZ(a1.pure)
exe = EXE(pyz,
  a1.scripts,
  exclude_binaries=1,
  name={name here},
  debug=False,
  strip=False,
  upx=True,
  console=1 )

  • 从所有脚本中收集

  • Collect from all scripts

        coll = COLLECT( exe,
           a1.binaries,
           a1.zipfiles,
           a1.datas,
           a2.binaries,
           a2.zipfiles,
           a2.datas,
       python_tree, 
           *additional_trees,
           strip=False,
           upx=True,
           name={})
    

  • 将python复制到所有子进程调用中指定的位置的python发行版中,并安装pyinstaller找不到的任何其他要求(我有一些类似matplotlib,pylab等)

  • Copy python in your distribution at location that was specified in all the subprocess calls with any additional requirements that were not found by pyinstaller (i had a few like matplotlib, pylab etc)

    创建一个启动脚本,该脚本首先更改所有必需的环境变量以指向您的程序包,然后启动该应用程序.在我的情况下,需要的是从调用目录:

    Create a start script that first changes any required enviromental variables to point to your package and then start the app. In my case what was needed was, from calling directory:

     export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH
     export LD_RUN_PATH=`pwd`:$LD_RUN_PATH
    

  • 现在,如果我希望应用程序在未安装python的计算机上运行,​​或者如果它们已安装python,则所有这些都是必需的,请确保该应用程序仍使用分发包中的所有库而不是任何本地库.如果在您的情况下,python已经安装在目标计算机上,那么我认为不需要这样做,前三步就足够了.

    Now all of these was required if I wanted the application to run on machines that either had no python installed, or if they have python installed, make sure the app still uses all the libraries from the distribution package instead of any local libraries. If in your case python is already installed on target machines I don't think anything like this would be necessary and first 3 steps would suffice.

    这篇关于在PyInstaller中打包多个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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