问题
I have IronPython installed
My python file looks like this:
import sys
print(sys.version)
import json
The code that runs it:
var p = Python.CreateEngine();
var scope = p.CreateScope();
p.ExecuteFile("Test.py", scope);
It prints out:
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:
No module named json
As I understand the json module should be included in this version of IronPython.
Why do I get this error?
回答1:
I soon discovered that the interactive python window in Visual Studio did not throw this error.
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);
来源:https://stackoverflow.com/questions/41348288/ironpython-no-module-named-json