[英]How to run a Python executable(.py) within a C program (for example by using execvp)?
I have this piece of code for running a Python program and I expect my shell to run a python program when I enter something like the following : mysh> hello.py 我有这段用于运行Python程序的代码,当我输入以下内容时,我希望我的shell运行python程序:mysh> hello.py
But hello.py is not passed to /usr/bin/python in execvp and I need help figuring out how to correctly pass arguments to execvp. 但是hello.py没有在execvp中传递给/ usr / bin / python,我需要帮助弄清楚如何正确地将参数传递给execvp。
if (pid==0) // child process
{
if (py_flag==1)
execvp("/usr/bin/python",argv);
else
{
execvp(argv[0],argv);
perror("error");
}
}
And here's the result I am receiving: 这是我收到的结果:
./basic_shell
mysh> hello.py
I am a python program
Python 2.6.6 (r266:84292, May 27 2013, 05:35:12)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
While I expect to receive a result like this: 虽然我希望收到这样的结果:
$ python hello.py
sys.argv[0] = hello.py
Please let me know how can this be fixed. 请让我知道如何解决此问题。
I changed my code to this and now it is working as expected: 我将代码更改为此,现在它可以按预期工作:
if (pid==0) //child process
{
if (py_flag==1)
{
char *new_argv[2];
new_argv[0]="/usr/bin/python";
new_argv[1]=argv[0];
new_argv[2]=0;
//execvp("/usr/bin/python",argv);
execvp(new_argv[0],new_argv);
}
else
{
execvp(argv[0],argv);
perror("error");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.