2016-12-25 4 views
0

Ich habe Python2.7/Python3.4m Code in c-Datei eingebettet unter Ubuntu14.4 eingebettet. Der Python-Code importiert Tensorflow, wenn er fehlschlägt.AttributeError wegen sys.argv [0] für 'import Tensorflow als tf' innerhalb c

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

int 
main(int argc, char *argv[]) 
{ 
    Py_SetProgramName(argv[0]); /* optional but recommended */ 
    Py_Initialize(); 
    PyRun_SimpleString("import tensorflow as tf"); 
    Py_Finalize(); 
    return 0; 
} 

Dies ergibt folgende Ausgabe:

Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 24, in <module> 
    from tensorflow.python import * 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 100, in <module> 
    from tensorflow.python.platform import app 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 23, in <module> 
    from tensorflow.python.platform import flags 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/flags.py", line 25, in <module> 
    _global_parser = _argparse.ArgumentParser() 
    File "/usr/lib/python2.7/argparse.py", line 1575, in __init__ 
    prog = _os.path.basename(_sys.argv[0]) 
AttributeError: 'module' object has no attribute 'argv' 

Jede Hilfe in dieser würde sehr geschätzt werden. Ich kompilierte es sowohl mit cmake als auch mit Bazel.

Dieses Problem tritt auch innerhalb von virtualenv Installation auf.

Antwort

4

Ich bin so dumm! Nun, das funktioniert, da Tensorflow sys.argv benötigt.

int 
main(int argc, char *argv[]) 
{ 
    Py_SetProgramName(argv[0]); /* optional but recommended */ 
    Py_Initialize(); 
    PyRun_SimpleString("import sys\n" 
        "sys.argv = ['']"); 
    PyRun_SimpleString("import tensorflow as tf"); 
    Py_Finalize(); 
    return 0; 
} 
Verwandte Themen