繁体   English   中英

通过C(Debian)在终端中运行python模块

[英]Running a python module in terminal through c (debian)

所以我试图通过C代码运行python模块。 这是一个播放动画的模块。 要在控制台中运行动画,请输入(导航到其目录/ 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”的模块)

我该如何解决这个错误? 非常感谢! :)

您可以在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;
}

暂无
暂无

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

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