diff --git a/torch/csrc/distributed/autograd/init.cpp b/torch/csrc/distributed/autograd/init.cpp index 02c5f277a921..ad6dfa7d8f46 100644 --- a/torch/csrc/distributed/autograd/init.cpp +++ b/torch/csrc/distributed/autograd/init.cpp @@ -23,8 +23,10 @@ PyObject* dist_autograd_init(PyObject* _unused, PyObject* noargs) { } auto torch_C_module = THPObjectPtr(PyImport_ImportModule("torch._C")); - if (!torch_C_module) - return nullptr; + if (!torch_C_module) { + throw python_error(); + } + auto torch_C_m = py::handle(torch_C_module).cast(); auto m = torch_C_m.def_submodule("_distributed_autograd", "distributed autograd bindings");