Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python subaccess.run是';t使用Pyinstaller_Python_Python 3.x_Subprocess_Pyinstaller - Fatal编程技术网

Python subaccess.run是';t使用Pyinstaller

Python subaccess.run是';t使用Pyinstaller,python,python-3.x,subprocess,pyinstaller,Python,Python 3.x,Subprocess,Pyinstaller,考虑以下名为test.py的python(3.9)脚本: import sys import os import subprocess from pathlib import Path # This is for pyinstaller (as main_dir = sys.path[0] won't work) if getattr(sys, 'frozen', False): main_dir = os.path.dirname(sys.executable) else:

考虑以下名为test.py的python(3.9)脚本:

import sys
import os
import subprocess
from pathlib import Path

# This is for pyinstaller (as main_dir = sys.path[0] won't work)
if getattr(sys, 'frozen', False):
    main_dir = os.path.dirname(sys.executable)
else:
    main_dir = os.path.dirname(os.path.abspath(__file__))

processes_dir = Path(main_dir, "processes")
outfile = Path(main_dir, "output.txt")

# Initialize the output text file
with open(outfile, 'w') as f:
    f.write('')

# This calls A1.py (see below)
result = subprocess.run([sys.executable, Path(processes_dir, "A1.py")], input="1\n2", capture_output=True, text=True)

# If an error is raised, it's written to output.txt; stdout is written to output.txt
if result.stderr:
    with open(outfile, 'a') as f:
        f.write("{0}\n\n".format(result.stderr))
else:
    with open(outfile, 'a') as f:
        f.write("{0}\n\n".format(result.stdout))
subprocess.run调用以下简单脚本:

x1 = int(input())
x2 = int(input())
print(x1+x2)
这车开得很好。我正在尝试解决如何使用Pyinstaller将其转换为可执行文件(.exe)。在相应的目录中,我运行:

pyinstaller --onefile test.py

这将成功生成test.exe。当我运行test.exe(无论是从cmd还是双击文件)时,它会毫无错误地打开,生成一个空的output.txt,然后无限期地挂起。subprocess.run似乎无法与pyinstaller一起正常工作。让test.exe与pyinstaller一起工作有什么想法/建议吗?

这里发生的是,当脚本未编译时,
sys.executable
返回python可执行文件(
C:\Users\randomuser\AppData\Local\Programs\Python38
),但当代码编译时,sys.executable返回您制作的.exe文件。所以,你的.exe文件调用自己,再次调用自己无限次,hainging

您可以用两种不同(简单)的方法解决此问题:

  • (如果要分发exe文件,则不建议这样做,因为它依赖于python安装):
    sys.executable
    替换为
    'python'
    。这将确保脚本使用Python执行,而不是使用您自己的.exe文件(如果编译):

     result = subprocess.run(['python', Path(processes_dir, "A1.py")], input="1\n2", capture_output=True, text=True)
    
  • 您可以导入
    A1.py
    脚本(确保脚本与可执行文件位于同一文件夹中,并使主代码位于名为main的函数中,以字符串形式返回结果):

  • 然后,您将能够运行
    importa1
    ,并调用运行
    A1.main()的主过程

    可以考虑用TrimeButter子句和回溯模块

    捕获错误回溯。
    pyinstaller命令应该是:
    pyinstaller--onefile test.py--add data“A1.py;”

    这里发生的是,当脚本未编译时,
    sys.executable
    返回python可执行文件(
    C:\Users\randomuser\AppData\Local\Programs\Python38
    ),但当代码编译时,sys.executable返回您创建的.exe文件。所以,你的.exe文件调用自己,再次调用自己无限次,hainging

    您可以用两种不同(简单)的方法解决此问题:

  • (如果要分发exe文件,则不建议这样做,因为它依赖于python安装):
    sys.executable
    替换为
    'python'
    。这将确保脚本使用Python执行,而不是使用您自己的.exe文件(如果编译):

     result = subprocess.run(['python', Path(processes_dir, "A1.py")], input="1\n2", capture_output=True, text=True)
    
  • 您可以导入
    A1.py
    脚本(确保脚本与可执行文件位于同一文件夹中,并使主代码位于名为main的函数中,以字符串形式返回结果):

  • 然后,您将能够运行
    importa1
    ,并调用运行
    A1.main()的主过程

    可以考虑用TrimeButter子句和回溯模块

    捕获错误回溯。
    pyinstaller命令应该是:
    pyinstaller--onefile test.py--add data“A1.py;”

    您是否从
    PyPi
    安装了
    pyinstaller
    ?如果是这样,请尝试卸载它并从github
    pip安装中安装它https://github.com/pyinstaller/pyinstaller/archive/develop.zip
    根据pyinstaller文档
    sys.executable
    不像普通Python代码那样是Python解释器,因此使用它来运行Python脚本可能不会像您希望的那样工作。试着打印出来看看是什么@IceBear是的,我使用
    pip安装pyinstaller
    PyPi
    安装了它。我相信它与github上提供的安装相同。@barny它打印
    C:\Users\Daniel\Desktop\Pyinstaller\u test\test.exe
    ,这确实是test.exe的位置,所以这似乎是问题所在。PyInstaller应该绑定一个python应用程序(以及所有必需的依赖项),那么如何引导test.exe调用这个绑定的python呢?我可能会将此指向我的python安装,但这首先破坏了将其捆绑为exe的全部目的,因为我打算与未安装python的用户共享此内容。您的代码可以导入python代码并从中执行某些操作,或者您可以对其进行求值以在其中执行某些操作。您是否已从
    PyPi
    安装了
    pyinstaller
    ?如果是这样,请尝试卸载它并从github
    pip安装中安装它https://github.com/pyinstaller/pyinstaller/archive/develop.zip
    根据pyinstaller文档
    sys.executable
    不像普通Python代码那样是Python解释器,因此使用它来运行Python脚本可能不会像您希望的那样工作。试着打印出来看看是什么@IceBear是的,我使用
    pip安装pyinstaller
    PyPi
    安装了它。我相信它与github上提供的安装相同。@barny它打印
    C:\Users\Daniel\Desktop\Pyinstaller\u test\test.exe
    ,这确实是test.exe的位置,所以这似乎是问题所在。PyInstaller应该绑定一个python应用程序(以及所有必需的依赖项),那么如何引导test.exe调用这个绑定的python呢?我可能可以将其指向我的python安装,但这首先破坏了将其捆绑为exe的全部目的,因为我打算与未安装python的用户共享。您的代码可以导入python代码并从中执行某些内容,也可以对其进行求值以执行其中的某些内容。