gpt4 book ai didi

python - 导入错误 : No module named clr when using CPython of python. 组织

转载 作者:太空狗 更新时间:2023-10-29 22:15:52 26 4
gpt4 key购买 nike

我正在编写应该在下面调用 python 脚本的 C++ 代码。为此,我使用 python.org 的 cpython .一些 python 脚本在 python for .net 的帮助下执行 .net 代码,当涉及到 .net 时,所有这些都失败了。我尝试构建测试应用程序以验证它失败的地方,发现它无法导入 clr 模块。

当我运行这段代码时,它给我 ImportError: No module named clr

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

如果我转到 python 控制台并输入“import clr”,一切正常。我还检查了 sys.path,它包含 clr.pyd 位于“C:\Python27\DLLs”的文件夹。我还尝试通过以下方式在 C++ 中设置此路径:

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

但这并没有帮助。此外,我不明白为什么 clr 模块显示为 clr.pyd 而不是 .pyc ,就像其他已编译的模块一样。

有人可以解释为什么通过 CPython 导入 clr 失败吗?有可能让它发挥作用吗?

最佳答案

要安装 clr,你必须安装 pythonnet 库。

您可以使用以下命令安装 pythonnet。

 pip install pythonnet

关于python - 导入错误 : No module named clr when using CPython of python. 组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26681641/

26 4 0