IronPython 設置包路徑


C#中添加對python文件或者對python包的引用時出現"no module .."的問題時的解決辦法。

C# IronPython Import 出錯

對hello.py 做一些簡單的修改

添加

import syssys.path.append(<DIR>)

因為我的hashlib在該目錄"D:\IronPython2.7\Lib"下,<DIR>為"D:\IronPython2.7\Lib

C# IronPython Import 出錯

////

def load_config(keyword_category, config_path = r'./config'):
    #load keywords under designated category from config file
    ###INPUT: str keyword_category -> also serves as name of config file; _
       #str config_path -> location of config files
    ###OUTPUT: list result -> keywords loaded from config file
    import os.path
    config_file = os.path.join(config_path, keyword_category+'.txt')
    f = read(config_file, 'r')
    result = []
    for line in f:
        result.append(line.replace('\n','').decode('utf8'))
    f.close()
    return result

在C#這一端,調用代碼如下(項目已經添加引用ironpython.dll, ironpython.hosting.dll, microsoft):

public string[] GetKeywords(string category, string config_path)
{

       ScriptRuntime pyRunTime = Python.CreateRuntime();
       dynamic pyScript = pyRunTime.UseFile(@".\python scripts\file_utils.py");

       List<string> tmp = pyScript.load_config(category, config_path);
       tmp.TrimExcess();

       string[] result = tmp.ToArray();

       return result;
}

運行時VS2013反饋的錯誤信息如下:

An unhandled exception of type 'IronPython.Runtime.Exceptions.ImportException' occurred in Microsoft.Dynamic.dll

Additional information: No module named os.path


很奇怪的一點是,其他python代碼里引用os等標准庫完全沒有問題,不知道為什么惟獨os.path會出現問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM