通过c(debian)在终端中运行python模块

时间:2018-06-05 13:03:55

标签: python c

所以我试图通过c代码运行python模块。它是一个播放动画的模块。要在控制台I中运行动画(在导航到其目录/ home / pi / scrawler之后):

python3 -m scrawler.examples.animation  

然而,当我尝试将它放在我的c代码中时:

system ("sudo /home/pi/scrawler")
system ("sudo python3 -m scrawler.examples.animation")

它给了我这个错误:
/ usr / bin / python3:查找" scrawler.examples.animation"的模块规范时出错。 (ImportError:没有名为' scrawler'的模块)

如何解决此错误? 非常感谢! :)

1 个答案:

答案 0 :(得分:0)

您可以在C程序中更改目录:

#include <limits.h>

int main (int argc, char *argv[]) {

    int result;
    char curr_dir[PATH_MAX+1] = {0};

    // Get the current directory
    char * wd = getcwd(curr_dir, PATH_MAX);
    if ( wd == NULL ) {
         perror("Unable to getcwd");
         exit(1);
    }

    // Change to the required directory
    result = chdir("/home/pi/scrawler");
    if ( result == -1 ) {
         perror ("Unable to chdir");
         exit(1);
    }

    result = system ("sudo python3 -m scrawler.examples.animation");
    if ( result != 0 ) {
         fprintf(stderr, "Returned error %d\n", WEXITSTATUS(result));
         exit(1);
    }

    // Change back to the original directory
    result = chdir(curr_dir);
    if ( result == -1 ) {
         perror ("Unable to chdir back");
         exit(1);
    }

    // Any other code

    return 0;
}