IronPython有时导入模块,但不导入其他模块

时间:2011-10-05 21:34:18

标签: c# ironpython

简短版本是这似乎不是代码问题(尽管如果有人有一个程序化的解决方法,让我保持设计结构,那也会有效)。当我在某些情况下尝试导入任何模块时,它无法正常工作。

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

使用以下

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

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

当我从项目中的虚拟主线程实例化该类时,这一切都正常 但是,当我从另一个项目间接加载它时,我得到关于“没有这样的模块”错误的错误。

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;  }
    }
    ...
}

我甚至不确定此时还有什么可以测试,所以感谢任何帮助。

编辑:要清楚,我确实检查了两种情况下的sys.path,并且两个条目都已成功添加。令我困惑的是,就IronPython而言,这两种情况会有所区别。

2 个答案:

答案 0 :(得分:1)

这可能是一个C-P错误,但我敢打赌

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

是你的问题。在Python中(如C,C#等)'\'是一个转义字符。尝试将其更改为(注意r!)

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

看看是否有效。一个简单的

print sys.path

也可能显示路径是否实际正确。

答案 1 :(得分:0)

问题是IronPython分布在两个库文件(IronPython.dll和IronPython.Modules.dll)中。我在项目中运行的测试工作正常,但它在其他项目中不起作用,因为(无论出于何种原因)构建过程只导入IronPython.dll而不是模块库。

相关问题