1

Recently, I needed to use my tensorflow2 code which is coded by python in C++ with Qt. When I was testing the code, the error arose:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\__init__.py", line 49, in <module>
    from ._api.v2 import __internal__
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\_api\v2\__internal__\__init__.py", line 13, in <module>
    from . import distribute
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\_api\v2\__internal__\distribute\__init__.py", line 10, in <module>
    from . import combinations
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\_api\v2\__internal__\distribute\combinations\__init__.py", line 10, in <module>
    from tensorflow.python.distribute.combinations import generate
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\python\distribute\combinations.py", line 37, in <module>
    from tensorflow.python.distribute import multi_process_runner
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\python\distribute\multi_process_runner.py", line 39, in <module>
    from tensorflow.python.distribute import multi_process_lib
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\python\distribute\multi_process_lib.py", line 60, in <module>
    if _is_enabled():
  File "C:\programSoft\anaconda3\envs\newTensorflow2\lib\site-packages\tensorflow\python\distribute\multi_process_lib.py", line 39, in _is_enabled
    tpu_args = [arg for arg in sys.argv if arg.startswith('--tpu')]
AttributeError: module 'sys' has no attribute 'argv'

This is my C++ code:

void MainWindow::pythonTest()
{
    Py_SetPythonHome((wchar_t*)L"C:\\programSoft\\anaconda3\\envs\\newTensorflow2");
    Py_Initialize();
    if(!Py_IsInitialized()){
        qDebug()<<"python initialized failed";
        return;
    }

    PyRun_SimpleString("import tensorflow");
    Py_Finalize();
}
codeling
  • 11,056
  • 4
  • 42
  • 71
Joyce Ji
  • 11
  • 1
  • On an unrelated note, whenever you feel the need to do a C-style cast (like in `(wchar_t*) L"..."`) you should take that as a sign that you're doing something wrong. In this specific case the cast shouldn't be needed (and is in fact *wrong*) – Some programmer dude Apr 08 '22 at 06:46

0 Answers0