ImportError: No module named clr when using CPython of python.org

y_d picture y_d · Oct 31, 2014 · Viewed 17.7k times · Source

I'm writing C++ code which should invoke python scripts underneath. For this purpose I use cpython of python.org. Some of the python scripts execute .net code with help of python for .net and when it comes to .net all this fails. I tried to build test app to verify where it fails and found that it can't import clr module.

When I run this code it give me ImportError: No module named clr

Py_Initialize();
PyRun_SimpleString("import clr");

If I go to python console and type "import clr" everything works fine. I also checked sys.path and it contains the folder where clr.pyd located 'C:\Python27\DLLs'. I also tried setting this path in C++ by:

char* path = "C:\\Python27\\dlls";
Py_Initialize();
PySys_SetArgv(1, &path);
char* phome = Py_GetPythonHome();

But it didn't helped. In addition I don't understand why clr module is appears as clr.pyd and not .pyc like other compiled modules.

Could someone can explain why import clr fails through CPython? Is it possible to make it work?

Answer

jatinkumar patel picture jatinkumar patel · Feb 4, 2015

To install clr, you have to install pythonnet library.

You can install pythonnet using following command.

 pip install pythonnet