Cannot import torch via c++ interpreter

I currently have this .py file working in both the c++ interpreter and python itself

import string
#import torch
def PyMain():
print(“Hello from Python!”)

But if i uncomment the “import torch” i get this error:

Traceback (most recent call last):
File “C:\Users\J\Desktop\Game17\Game17\x64\Debug\”, line 24, in
import torch
File “C:\Users\J\AppData\Local\Programs\Python\Python310\lib\site-packages\”, line 122, in
raise err
OSError: [WinError 127] The specified procedure could not be found. Error loading “C:\Users\J\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\lib\c10_cuda.dll” or one of its dependencies.

the code calling python is:

void CallPython(string PythonModuleName, string PythonFunctionName)
	char* funcname = new char[PythonFunctionName.length() + 1];
	strcpy_s(funcname, PythonFunctionName.length() + 1, PythonFunctionName.c_str());

	char* modname = new char[PythonModuleName.length() + 1];
	strcpy_s(modname, PythonModuleName.length() + 1, PythonModuleName.c_str());

	std::wstring pythonHome = L"C:/Users/J/AppData/Local/Programs/Python/Python310";
	//std::wstring pythonHome = L"C:/Users/J/AppData/Local/Microsoft/WindowsApps";

	std::wstring pythonExecutablePath = L"C:/Users/J/AppData/Local/Programs/Python/Python310/python.exe";


	TCHAR cwd[2048];
	GetCurrentDirectory(sizeof(cwd), cwd);

	//separated with the platform’s search path delimiter (: on Unix, ; on Windows).
	//No spaces before/after ';'
	wstring Finalpath;
	//Finalpath += L"D:\\Gamedev Shared\\Source\\Python;";
	Finalpath += L"C:\\Users\\J\\Desktop\\Game17\\Game17\\x64\\Debug;"; 
	Finalpath += L"C:\\Users\\J\\AppData\\Local\\Programs\\Python\\Python310\\;"; 
	Finalpath += L"C:\\Users\\J\\AppData\\Local\\Programs\\Python\\Python310\\DLLs;"; 
	Finalpath += L"C:\\Users\\J\\AppData\\Local\\Programs\\Python\\Python310\\Lib;";
	Finalpath += L"C:\\Users\\J\\AppData\\Local\\Programs\\Python\\Python310;";
	Finalpath += L"C:\\Users\\J\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages;";
	//Finalpath += L"C:\\\\Users\\\\J\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python310\\\\lib\\\\site-packages\\\\;";

	PyObject* my_module = PyImport_ImportModule(modname);

	// Print a standard traceback to sys.stderr and clear the error indicator

	// Get the address of the particular Python function in the imported module
	//printf("Getting address of %s in Python module\\n", funcname);
	PyObject* my_function = PyObject_GetAttrString(my_module, funcname);

	// Call a callable Python object callable, with arguments given by the tuple args. 
	// If no arguments are needed, then args can be NULL.
	PyObject* my_result = PyObject_CallObject(my_function, NULL);

	//printf("Your function has been called\\n");

	// Undo all initializations made by Py_Initialize() and subsequent use of Python/C API functions, 
	// and destroy all sub-interpreters (see Py_NewInterpreter() below) that were created and not yet 
	// destroyed since the last call to Py_Initialize(). Ideally, this frees all memory allocated by the Python interpreter.

	delete[] funcname;
	delete[] modname;

I honestly don’t get why the plain .py file would work but not when called via the interpreter.

I found this guy having the exact same problem:
The python module i am importing in C++ with the Python/C API can’t import torch - Stack Overflow.

print(“Python Version:”, sys.version)
print(“Python Path:”, sys.path)

I have the exact same version and path in both the .py version and the c++ interpreter version.
If I navigate to : “C:\Users\J\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\lib\c10_cuda.dll” I can see it in explorer but for some reason python can’t.

Figured it out mostly…
If you have libtorch included in you c++ project, Libtorch loads a conflicting .dll file.

I would figure that libtorch loading a .dll in c++ would just allow “import torch” via interpreter to use that same dll, but as long as I got it working I guess I don’t need both.