Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IronPython: No module named json

Tags:

c#

ironpython

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?

like image 204
Murdock Avatar asked Dec 27 '16 16:12

Murdock


1 Answers

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);
like image 63
Murdock Avatar answered Nov 17 '22 18:11

Murdock