IronPython:没有名为json的模块 [英] IronPython: No module named json
问题描述
我已经安装了IronPython
I have IronPython installed
我的python文件看起来像这样:
My python file looks like this:
import sys
print(sys.version)
import json
运行它的代码:
var p = Python.CreateEngine();
var scope = p.CreateScope();
p.ExecuteFile("Test.py", scope);
它打印出:
2.7.7(.NET 4.0.30319.42000(32位)上的IronPython 2.7.7(2.7.7.0))
2.7.7 (IronPython 2.7.7 (2.7.7.0) on .NET 4.0.30319.42000 (32-bit))
但随后失败,但出现以下异常:
But then fails with the exception:
没有名为json的模块
No module named json
据我了解,此版本的IronPython中应包含json模块.
As I understand the json module should be included in this version of IronPython.
为什么会出现此错误?
推荐答案
我很快发现Visual Studio中的交互式python窗口没有引发此错误.
I soon discovered that the interactive python window in Visual Studio did not throw this error.
print sys.path还为交互式窗口和相关文件显示了不同的值.它仅包含bin/Debug文件夹中的路径.
print sys.path also showed different values for the interactive window and the file in question. It only included paths from the bin/Debug folder.
一个人可以轻松添加正确的路径:
One can easily add the correct paths:
var p = Python.CreateEngine();
var scope = p.CreateScope();
var libs = new[] {
"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\Extensions\\Microsoft\\Python Tools for Visual Studio\\2.2",
"C:\\Program Files (x86)\\IronPython 2.7\\Lib",
"C:\\Program Files (x86)\\IronPython 2.7\\DLLs",
"C:\\Program Files (x86)\\IronPython 2.7",
"C:\\Program Files (x86)\\IronPython 2.7\\lib\\site-packages"
};
p.SetSearchPaths(libs);
p.ExecuteFile("Test.py", scope);
这篇关于IronPython:没有名为json的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!