Shaman
Shaman

Reputation: 213

IronPython imports modules sometimes but not others

The short version is that this doesn't appear to be a code problem (though if anyone has a programmatic workaround that lets me keep the design structure, that would work too). When I try to import any module under certain circumstances it doesn't work properly.

import sys
sys.path.append('C:\Python26\Lib')
sys.path.append('C:\Users\<user>\Documents\Visual Studio 2010\Projects\<Solution>\Simulation')
import time # errors out
from XMLRPCBridge.python_ClientAPI.AsyncXMLRPCClient import AsyncXMLRPCClient, RPCPriority # if I remove the previous line this one errors instead

the Python file is loaded using the following

public class StateSimBridge
{
    private ScriptScope pythonModule = Python.CreateRuntime().UseFile("..\\..\\..\\Simulation\\AsyncClientPatch.py");

    // errors out before getting any farther than this
    ...
}

when I instantiate the class from a dummy Main thread in the project this all works fine However, when I load it indirectly from another project I get errors about 'no such module' errors.

public sealed class SimulationDriver
{
    private static readonly Lazy<SimulationDriver> lazy = new Lazy<SimulationDriver>(() => new SimulationDriver());
    private StateSimBridge.StateSimBridge simulationBridge = new StateSimBridge.StateSimBridge("Garmsir");

    static SimulationDriver()
    {
    }

    private SimulationDriver()
    {
    }

    public static SimulationDriver Instance
    {
        get { return lazy.Value;  }
    }
    ...
}

I'm not even sure what else to test at this point so any help is appreciated.

Edit: To be clear, I did check the sys.path in both circumstances and both the entries were successfully added. What confuses me is that there would be a difference between the two circumstances as far as IronPython is concerned.

Upvotes: 2

Views: 567

Answers (2)

Shaman
Shaman

Reputation: 213

The problem turned out to be that IronPython is spread across two library files (IronPython.dll and IronPython.Modules.dll). The tests I ran in the project worked fine, but it didn't work in the other projects because (for whatever reason) the build process was only importing IronPython.dll and not the modules library.

Upvotes: 0

Jeff Hardy
Jeff Hardy

Reputation: 7662

This might be a C-P error, but I bet that

sys.path.append('C:\Python26\Lib')
sys.path.append('C:\Users\<user>\Documents\Visual Studio 2010\Projects\<Solution>\Simulation')

is your problem. In Python (like C, C#, etc) '\' is an escape character. Try changing it to (note the r!)

sys.path.append(r'C:\Python26\Lib')
sys.path.append(r'C:\Users\<user>\Documents\Visual Studio 2010\Projects\<Solution>\Simulation')

and see if that works. A simple

print sys.path

might also show if the paths are actually correct.

Upvotes: 1

Related Questions