嵌入C时发生Python SyntaxError [英] Python SyntaxError when embedding in C

查看:99
本文介绍了嵌入C时发生Python SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python文件,当我对自己的python解释器执行该文件时,该文件运行正常.

I have a python file which runs fine when I execute it against my python interpreter.

我正在尝试使用python C API从C程序调用同一文件:

I'm trying to call the same file from a C program using the python C API:

#include <Python.h>
#include <stdio.h>

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

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    fp = fopen("floatcli.py", "r");
    PyRun_SimpleFile(fp, "floatcli.py");    

    Py_Finalize();

}

但是,当我运行它时,我得到一个python语法错误:

However, when I run this I get a python syntax error:

  File "floatcli.py", line 1
    üBa
     ^
SyntaxError: invalid syntax

(?üBa周围的框中也有?的负载,此处未显示).

(there is also a load of ? in boxes surrounding the üBa which isn't shown here).

floatcli.py的第一行只是一个导入语句...知道发生了什么吗?

The first line of floatcli.py is just an import statement...any idea what is going on?

推荐答案

对您的Python文件进行十六进制转储,它可能以一种Unicode格式编码,您所看到的可能是指示编码的标题.

Do a hex dump of your Python file, it's probably encoded in one of the Unicode formats and what you're seeing may be the header indicating that encoding.

如果是,则有两个选择.首先是将其转换为ASCII,以便您的嵌入式Python解释器可以读取它,或了解如何修改嵌入式Python解释器以处理编码.

If it is, you have two options. The first is to convert it to ASCII so your embedded Python interpreter can read it, or find out how to modify your embedded Python interpreter to handle the encoding.

这篇关于嵌入C时发生Python SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆