IronPython: No module named json

微笑、不失礼 提交于 2020-12-03 07:39:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!