python - 如何使用 exec 系统调用使用 C++ 的参数调用 Python 脚本

标签 python c++ unix system-calls

我想从 C++ 程序调用 python 脚本作为子进程。

我想运行这个命令:/home/minty99/.virtualenvs/venv_waveglow/bin/python/home/minty99/tacotron2/inference.py mps 1 4

它有 python 的命令行参数。

我试过这些代码:

string pt_arg = "mps "+ to_string(i) + ""+ to_string(fd[i][1]);

[1] execl("bash", "-c", "/home/minty99/.virtualenvs/venv_waveglow/bin/python/home/minty99/tacotron2/inference.py", pt_arg.c_str( ), 空);

[2] execl("/home/minty99/.virtualenvs/venv_waveglow/bin/python", "/home/minty99/tacotron2/inference.py", pt_arg.c_str(), NULL);

但它不起作用。

First one: exec fails with "No such file or directory"

Second one: /home/minty99/tacotron2/inference.py: can't open file 'mps 1 4': [Errno 2] No such file or directory

我怎样才能正确地做到这一点?

最佳答案

我还没有尝试过,但是阅读了 execl 的联机帮助页,它说

The first argument, by convention, should point to the file name associated with the file being executed.

在哪里

int execl(const char *file, const char arg0, ... /, (char *)0 */);

对我来说,这意味着您的第二个版本应该是这样的

execl("/home/minty99/.virtualenvs/venv_waveglow/bin/python", "python", "/home/minty99/tacotron2/inference.py", pt_arg.c_str(), NULL);

这是有道理的,因为如果您获得 python 中的参数列表,sys.argv[0] 将是“python”,而不是您的第一个参数。

关于python - 如何使用 exec 系统调用使用 C++ 的参数调用 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57706495/

相关文章:

Python:遍历几个csv文件

python - pandas.DataFrame.load/python2 和 python3 之间的保存 : pickle protocol issues

python - 当两者都使用时,WebDriverWait 是否会覆盖 ImplicitlyWait?

c++ - 如何覆盖 C++ 中的函数

c++ - Visual Studio 调试 - 局部变量的顺序?

linux - 根据unix中的两列对csv文件进行排序

python - 根据条件为变量赋值

linux - 执行n行shell脚本

unix - 在unix/linux中将远程变量的值赋给本地变量

c++ - 计算紧密正交投影矩阵以进行阴影映射