2011-10-05 117 views
2

简短的说法是,这看起来不是代码问题(尽管如果任何人有一个编程方法可以让我保持设计结构,那也可以)。当我在某些情况下尝试导入任何模块时,它无法正常工作。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而言,这两种情况之间会有所不同。

回答

0

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

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 

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

+0

输入是赞赏,我试图确保(没有区别)。但正如我所提到的,在一种情况下在IronPython下运行时,它可以正常工作,而不是在另一种情况下。当以纯Python运行时,它也可以正常工作。我也尝试在两种情况下打印路径,并且它们也是一样的。这两次他们仍然提出了相关的路径。 – Shaman