简体   繁体   English

运行带有参数的 python 脚本

[英]Run a python script with arguments

I want to call a Python script from C, passing some arguments that are needed in the script.我想从 C 调用 Python 脚本,传递脚本中需要的一些参数。

The script I want to use is mrsync, or multicast remote sync .我想使用的脚本是 mrsync 或多播远程同步 I got this working from command line, by calling:我从命令行开始工作,通过调用:

python mrsync.py -m /tmp/targets.list -s /tmp/sourcedata -t /tmp/targetdata

-m is the list containing the target ip-addresses. -m 是包含目标 IP 地址的列表。 -s is the directory that contains the files to be synced. -s 是包含要同步的文件的目录。 -t is the directory on the target machines where the files will be put. -t 是目标机器上将放置文件的目录。

So far I managed to run a Python script without parameters, by using the following C program:到目前为止,我通过使用以下 C 程序设法运行了一个没有参数的 Python 脚本:

Py_Initialize();
FILE* file = fopen("/tmp/myfile.py", "r");
PyRun_SimpleFile(file, "/tmp/myfile.py");
Py_Finalize();

This works fine.这工作正常。 However, I can't find how I can pass these argument to the PyRun_SimpleFile(..) method.但是,我找不到如何将这些参数传递给PyRun_SimpleFile(..)方法。

Seems like you're looking for an answer using the python development APIs from Python.h.似乎您正在使用 Python.h 中的 Python 开发 API 寻找答案。 Here's an example for you that should work:这是一个应该有效的示例:

#My python script called mypy.py
import sys

if len(sys.argv) != 2:
  sys.exit("Not enough args")
ca_one = str(sys.argv[1])
ca_two = str(sys.argv[2])

print "My command line args are " + ca_one + " and " + ca_two

And then the C code to pass these args:然后是传递这些参数的 C 代码:

//My code file
#include <stdio.h>
#include <python2.7/Python.h>

void main()
{
    FILE* file;
    int argc;
    char * argv[3];

    argc = 3;
    argv[0] = "mypy.py";
    argv[1] = "-m";
    argv[2] = "/tmp/targets.list";

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

If you can pass the arguments into your C function this task becomes even easier:如果您可以将参数传递到 C 函数中,则此任务变得更加容易:

void main(int argc, char *argv[])
{
    FILE* file;

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

You can just pass those straight through.你可以直接通过这些。 Now my solutions only used 2 command line args for the sake of time, but you can use the same concept for all 6 that you need to pass... and of course there's cleaner ways to capture the args on the python side too, but that's just the basic idea.现在,为了节省时间,我的解决方案只使用了 2 个命令行参数,但是您可以对需要传递的所有 6 个参数使用相同的概念……当然还有更简洁的方法可以在 python 端捕获参数,但是这只是基本的想法。

Hope it helps!希望能帮助到你!

You have two options.你有两个选择。

  1. Call称呼

    system("python mrsync.py -m /tmp/targets.list -s /tmp/sourcedata -t /tmp/targetdata")

    in your C code.在你的 C 代码中。

  2. Actually use the API that mrsync (hopefully) defines.实际上使用mrsync (希望)定义的API。 This is more flexible, but much more complicated.这更灵活,但要复杂得多。 The first step would be to work out how you would perform the above operation as a Python function call.第一步是弄清楚如何将上述操作作为 Python 函数调用执行。 If mrsync has been written nicely, there will be a function mrsync.sync (say) that you call as如果mrsync写得很好,就会有一个函数mrsync.sync (比如说)你称之为

    mrsync.sync("/tmp/targets.list", "/tmp/sourcedata", "/tmp/targetdata")

    Once you've worked out how to do that, you can call the function directly from the C code using the Python API.一旦确定了如何执行此操作,就可以使用 Python API 从 C 代码直接调用该函数。

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

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