【问题标题】:Is any way to run pyinstaller command from anther python script when condition satisfies?当条件满足时,有什么方法可以从另一个 python 脚本运行 pyinstaller 命令?
【发布时间】:2020-09-26 02:16:16
【问题描述】:

我可以使用 pyinstaller 命令将 python 脚本创建为 exe 文件,

pyinstaller my_script.py

现在我尝试在满足条件时从另一个 python 脚本创建 exe 文件,

if conditon == True:
     run pyinstaller command
else:
     pass

【问题讨论】:

    标签: python-3.x pyinstaller


    【解决方案1】:

    要从 python 脚本运行 pyinstaller 命令,您可以使用 PyInstaller.__main__ 模态并通过 .run 函数作为列表传递命令行参数。 代码如下所示。

    import PyInstaller.__main__
    
    PyInstaller.__main__.run([
        '--name=%s' % package_name,
        '--onefile',
        '--windowed',
        '--add-binary=%s' % os.path.join('resource', 'path', '*.png'),
        '--add-data=%s' % os.path.join('resource', 'path', '*.txt'),
        '--icon=%s' % os.path.join('resource', 'path', 'icon.ico'),
        os.path.join('my_package', '__main__.py'),
    ])
    

    更多信息可以在here找到。

    【讨论】:

      【解决方案2】:

      子流程模块的一个有趣概念对我有用

      import subprocess
      
      if create_exe == True:
         subprocess.call(r"python -m PyInstaller -F --name test --add-data 
         templates;templates --add-data static;static --icon=icon.ico path\to\test.py")
      else:
         pass
      

      在哪里,

      --name --> 输入你要创建的exe文件名

      --add-data --> 添加静态文件

      --icon -->为你的exe文件设置图标

      path\to\test.py --> 给出要转换为exe的python文件的路径

      【讨论】:

        猜你喜欢
        • 2021-04-03
        • 1970-01-01
        • 2016-03-07
        • 2021-07-28
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多