0

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

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();

}

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

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

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

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

1 Answer 1

1

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks...it sort of worked. After changing encoding to ANSI, I now get similar errors but related to python standard library files (e.g. optparse).. :S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.