IronPython: No module named json
Asked Answered
R

1

7

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?

Reciprocity answered 27/12, 2016 at 16:0 Comment(0)
R
11

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);
Reciprocity answered 27/12, 2016 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.