2011-10-05 115 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