简体   繁体   English

在 Windows 上使用 ubuntu 终端运行 python 子进程

[英]Running python subprocess with ubuntu terminal on windows

Is there a way to run the python module subprocess under Windows, but using the Ubuntu terminal, that is installed in wsl.有没有办法在Windows下运行python模块subprocess进程,但是使用安装在wsl中的Ubuntu终端。

My first guess was to promt the ubuntu terminal via the excutable keyword in the function:我的第一个猜测是通过函数中的 excutable 关键字提示 ubuntu 终端:

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)

I need the functionality to be able to run OpenFOAM commands with my python programm without immigrating my python code to the Linux system.我需要能够使用我的 python 程序运行OpenFOAM命令的功能,而无需将我的 python 代码迁移到 Linux 系统。

EDIT: To clarify my issue a bit further:编辑:进一步澄清我的问题:

I have a GUI with diverse python codes running.我有一个运行不同 python 代码的 GUI。 From there i need to run OpenFOAM, which is only available under Linux.从那里我需要运行 OpenFOAM,它只在 Linux 下可用。 To overcome that limit, I use Ubuntu (WSL).为了克服这个限制,我使用 Ubuntu (WSL)。 I would need a WSL shell to run in the background, to execute the linux commands and set up the enviroment needed by OpenFOAM.我需要一个 WSL shell 在后台运行,执行 linux 命令并设置 OpenFOAM 所需的环境。

Using Ubuntu manually, I start the Terminal and source to the a .bashrc file, where all the enviroment variables are set.手动使用 Ubuntu,我启动终端并获取 .bashrc 文件的源,其中设置了所有环境变量。 From there I can use the OpenFoam commands to run simulations.从那里我可以使用 OpenFoam 命令来运行模拟。 Those steps would be great to execute from a python code directly.直接从 python 代码执行这些步骤会很棒。

Hope this helps someone else on that problem:希望这可以帮助其他人解决这个问题:

My workaround for the problem is, that i create a .sh file in my wsl file that i compile with wsl chmod -x filename.sh and then run it with source filename.sh .我解决这个问题的方法是,我在我的 wsl 文件中创建一个 .sh 文件,我用wsl chmod -x filename.sh编译,然后用source filename.sh运行它。

Inside my .sh file i load the enviromental variables of OpenFoam by sourcing to the bashrc file of the OpenFoam version that i want to use in my script.在我的 .sh 文件中,我通过获取我想在我的脚本中使用的 OpenFoam 版本的 bashrc 文件来加载 OpenFoam 的环境变量。 From there, I can define each step, that would i normally do in the terminal.从那里,我可以定义每个步骤,这通常是我在终端中所做的。 (Similar to the Allrun files in the OpenFoam examples) (类似于 OpenFoam 示例中的 Allrun 文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM