2016-12-25 38 views
0

我在Ubuntu14.4上運行的c文件內嵌入了python2.7/python3.4m代碼。 python代碼是導入tensorflow,當它失敗時。AttributeError由於sys.argv [0]'導入tensorflow爲tf'在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; 
} 

這給了下面的輸出:

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' 

任何幫助,在此將不勝感激。我編譯它使用cmake以及bazel。

即使在virtualenv安裝中也會出現此問題。

回答

4

我好蠢!以及這工作,因爲張量流需要sys.argv。

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; 
} 
相關問題