Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 541
y_d Avatar asked Oct 31 '14 18:10

y_d


3 Answers

To install clr, you have to install pythonnet library.

You can install pythonnet using following command.

 pip install pythonnet
like image 166
jatinkumar patel Avatar answered Nov 10 '22 21:11

jatinkumar patel


You need to call PySys_SetArgv after Py_Initialize appropriately to change the syspath.

like image 39
mic4ael Avatar answered Nov 10 '22 20:11

mic4ael


I work behind a proxy server with multiple versions of python. I have a bat file I update when I need a new module, or wish to update an existing one. The first line sets the path to the version I wish to update The bat file is run from the scripts directory. Proxy IP with port "9.254.0.1:81"

SET path="C:\Programs\Python\Python3_64"
pip install --proxy="9.254.0.1:81" --upgrade pip
pip install --proxy="9.254.0.1:81" pythonnet
like image 44
Bill Kidd Avatar answered Nov 10 '22 19:11

Bill Kidd