【问题标题】:How to create standalone executable when using subprocess in python?在python中使用子进程时如何创建独立的可执行文件?
【发布时间】:2021-04-22 09:43:06
【问题描述】:

我使用子进程在另一个脚本(名为 first_script.py)中调用一个脚本(second_script.py)。 有没有办法将 first_script.py 转换为可执行文件,而不需要始终将 second_script.py 放在同一个文件夹中?

我在第一个脚本中使用这一行来执行第二个:

out = subprocess.call(['python', 'second_script.py'])

这一行将其转换为 exe 但在创建可执行文件后,始终需要在同一目录中有第二个脚本

pyinstaller --onefile first_script.py

【问题讨论】:

    标签: python subprocess executable


    【解决方案1】:

    在我的情况下可行的解决方案是为第二个脚本创建一个 .exe 并将上面的行替换为

            def cmd(cmd: str = "ls -al", assertfail=True):
            up = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT,
                                  close_fds=False)
            str = [o.rstrip().decode() for o in up.stdout]
            exitcode = up.wait()
            return exitcode, str
    
        e, out = cmd("second_script.exe")
    

    然后运行

    pyinstaller --onefile first_script.py
    

    这样,即使我将 second_script.exe 与 first_script.exe 放在同一目录下,至少也可以为其他人运行,而无需安装任何库/包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2017-04-17
      相关资源
      最近更新 更多