zoukankan      html  css  js  c++  java
  • IronPython 设置包路径

    C#中添加对python文件或者对python包的引用时出现"no module .."的问题时的解决办法。

    C# IronPython Import 出错

    对hello.py 做一些简单的修改

    添加

    import syssys.path.append(<DIR>)

    因为我的hashlib在该目录"D:IronPython2.7Lib"下,<DIR>为"D:IronPython2.7Lib

    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('
    ','').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 scriptsfile_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会出现问题

  • 相关阅读:
    java 的 线程池应用和实践
    拦截信息短信息并转发到指定手机
    分享 UC优视 的android程序员面试题
    解释通讯协议中的xml
    设计模式工厂模式
    MongoDB基础教程系列第一篇 进入MongoDB世界
    Docx组件读写Word文档介绍
    [转]Oracle数据库逻辑增量备份之exp/imp
    JSON文件读取
    JAVA综合--如何掌握JDK1.5枚举类型[转]
  • 原文地址:https://www.cnblogs.com/zeroone/p/5448260.html
Copyright © 2011-2022 走看看