Skip to content

[BUG] Pybind11 exception with tensorflow 2.2 env in python cpp communicatio #2454

@spratik

Description

@spratik

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): window 10
  • TensorFlow installed from (source or binary): pip install tensorflow-gpu==2.2
  • TensorFlow version (use command below):2.2
  • Python version:3.7.6
  • CUDA/cuDNN version: 10, 10.1,8.0 / 7.6.5
  • GPU model and memory: NVIDIA Geforce GTX 1050 Ti & 12 GB

Issue description

Describe the current behavior
Below code is working fine while running through tensorflow2.0 env but same code if I run through tensorflow2.2 env then it's give below exception at PyImport_Import(pName)

Error : Microsoft c++ exception : pybind11::error_already_set at memory location XXXXXX

Describe the expected behavior
should not through pybind11::error_already_set while just importing tensorflow library

Reproducible example code

C++ code

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include
using namespace std;
int main(int argc, char *argv[])
{
PyObject *pName, *pModule;
std::string pythonFile = "sample"
std::string tensoflow_env = "tensorflow_env22_path";
wchar *env = Py_DecodeLocale(tensoflow_env.c_str(),NULL);
Py_SetPythonHome(env);
Py_Initialize();
pName = PyUnicode_DecodeFSDefault(pythonFile.c_str());
pModule = PyImport_Import(pName);

Py_DECREF(pName);
Py_DECREF(pModule);

if (Py_FinalizeEx() < 0) {
return 120;
}
return 0;
}

Python Code :
Below is sample.py file for a reference.

import tensorflow as tf

Note that you have to set below path in system environment path variable. then only above code will work.
<Tensorflow_flow_env22>
<Tensorflow_flow_env22>\Scripts
<Tensorflow_flow_env22>\Library\bin

I have used Visual studio 2015 with v140 compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions