subprocess.run如果从Python推出,则会产生不同的结果,而MATLAB

发布于 2025-01-25 09:02:39 字数 910 浏览 2 评论 0原文

我有一个Python 3类,可以创建输入以发送到Fortran程序(XFOIL)。我正在使用subprocess.run来启动fortran二进制文件并将命令输入发送到程序,如果我从python内部进行操作,这很好。命令发送到Fortran程序,捕获输出,一切都很好。

不幸的是,我正在尝试使MATLAB可用的Python代码可用,并且在以这种方式启动时似乎有所不同。是的,我已经设法正确设置了路径,以便MATLAB加载Python类,并且可以调用一些方法(不依赖于Fortran运行)从MATLAB内部使用。

我来自Python的呼叫指定UTF8编码(修复了从PC到MAC迁移的问题),我认为


    # connect the subprocess for xfoil
    p = subprocess.run(binfile, input=cmds,
                       shell=True, encoding='utf8',
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)

发送到程序的命令是非常普通的Vanilla:使其创建输出文件。我知道我正在拨打XFOIL之前在正确的工作目录中工作,因为我管理目录并在较早的调试语句中打印它们。无论我从Python还是从MATLAB启动,此位置都是正确的。不同之处在于,当我从Python运行时,Xfoil会创建预期的输出文件,但是如果我从Matlab运行,则Xfoil似乎永远不会运行,也不会创建输出文件。

关于为什么会有区别的想法? 我在Macos Bigsur,Python 3.8.10和Matlab R2021A上,

我意识到这对于Matlab论坛来说可能是一个更好的问题,因为它可能高度依赖Matlab的Python实现功能。

提前致谢!

I have a Python 3 class that creates input to send to a fortran program (xfoil). I am using subprocess.run to launch the fortran binary and send the command inputs to the program, and this works just fine if I do it from within Python. Commands are sent to the fortran program, output is captured, all is well.

Unfortunately, I am trying to make the Python code available from Matlab and it seems to work differently when launched that way. And yes, I've managed to get the paths set up correctly so Matlab will load the Python class and I can call some methods (that don't depend on the fortran run) attached to that class from within Matlab.

My call from Python specifies utf8 encoding (fixed an issue migrating from PC to Mac), and I would assume is pretty plain-vanilla:


    # connect the subprocess for xfoil
    p = subprocess.run(binfile, input=cmds,
                       shell=True, encoding='utf8',
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)

The commands that get sent to the program make it create an output file. I know I am working in the correct working directory before the call to xfoil because I manage the directories and print them on a debug statement earlier. This location is correct whether I run launching from Python or from Matlab. The difference is that when I run from Python, xfoil creates the expected output file, but if I run from Matlab, xfoil never appears to run at all, and no output file is created.

Thoughts on why there would be a difference?
I am on macOS BigSur, Python 3.8.10, and Matlab R2021a

I realize this may be a better question for the Matlab forum because it may be highly dependent on the way Matlab's Python implementation functions.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文