这个问题有点冗长,但我试图为您提供我认为有必要找到答案的细节。如何使用IronPython内部引用独立的C#类库项目(Visual Studio 2010)
我有一个C#WPF解决方案(.Net 4)包含一个主项目,构建一个WPF窗口应用程序,该应用程序依赖于驻留在同一Visual Studio 2010解决方案中的一些类库项目。
其中一个类库项目封装了一些先前开发的python代码,我想通过IronPython和Microsoft动态语言运行库使用该代码。 我希望类库项目是自包含的,而不依赖于IronPython的完整安装。
问题是,我不知道如何引用封装库项目持有的Python代码,总是工作。
正常情况下,我只是添加一个对此问题讨论的类库项目的引用:Visual Studio 2010: How refer to a C# .Net class library project with third part dependencies。但它没有帮助。
的解决方案是如何设置在Visual Studio中:
该解决方案是这样的:
- MainApp(窗口WPF应用程序项目)
- ...
- ClassLib1(C#类库项目)
- ...
- ClassLibWithPython(与IronPython的C#类库项目)
- C#类
- LIB(目录)
- IronPython.dll
- IronPython的。 Modules.dll
- Microsoft.Dy namic.dll
- Microsoft.Scripting.dll
- Microsoft.Scripting.Metadata.dll
- pylib(有一些使用Python模块目录)
- os.py
- ...。PY
- ctypes的(目录中有一些使用Python模块)
- 我pyton类(目录)
ClassLibWithPython必须居住在本地的lib文件夹中的IronPython的DLL的引用(Copy Local
属性True
)。 MainApp项目引用了ClassLib1项目和ClassLibWithPython项目(也有Copy Local属性为True)。
在编译解决方案时,所有DLL和MainApp.exe文件都出现在MainApp/bin/Debug中,并且它在某些机器(XP和Win 7)上正常工作,但是在其他一些机器(XP)上失败。在做了一些调试之后,我发现内置的IronPython模块没有正确加载。当导入os模块(pylib/os.py,像这样一个http://pydoc.org/get.cgi/usr/local/lib/python2.5/os.py)时,由于缺少模块名称'nt'
,我得到一个python异常(ImportError, no os specific module found
)。
当比较它在哪里工作和不在哪里时发生了什么,我发现sys.builtin_module_names
只是返回了一些项目,与我在其他某些机器上运行相同的代码时得到的结果相比。
有问题的机器有:
sys.builtin_module_names = ['clr', 'future_builtins', 'imp', 'sys', '__builtin__', 'exceptions']
计算机这里的一切作品有:
sys.builtin_module_names: ['clr', 'future_builtins', 'imp', 'sys', '__builtin__', 'exceptions', '_codecs', 'cmath', '_sha512', 'msvcrt', 'array', '_winreg', '_weakref', '_warnings', '_subprocess', '_ssl', '_sre', '_random', '_functools', 'xxsubtype', 'time', 'thread', '_struct', '_heapq', '_ctypes_test', '_ctypes', 'socket', '_sha256', '_sha', 'select', 're', 'operator', 'nt', '_md5', 'math', 'marshal', '_locale', '_io', 'itertools', 'gc', 'errno', 'datetime', 'cStringIO', 'cPickle', 'copy_reg', '_collections', 'binascii', 'zlib', 'signal', 'mmap']
工作周围没有帮助
我试着添加using
报表ClassLibWithPython的C#代码确保即使是隐式引用的程序集也是链接的,但没有区别。
变通办法,帮助
我找到了两种解决方法提供了一个可行的解决方案,但是他们都打破了封装原则,并公开ClassLibWithPython的实施细则:将所有
- 代替来自MainApp项目中ClassLibWithPython的代码。
- 将ClassLibWithPython保存在单独的项目中,但同时也将对IronPython.dll和IronPython.Modules.dll的引用添加到MainApp项目中。
这是什么使解决方案#2工作?
任何建议如何使这项工作在一个干净的方式?
感谢的阅读这远远;-)
我还没有真正理解我的部署计算机上发生了什么,但有时我收到异常,表明IronPython.Modules.dll未加载,尽管它与exe文件和其他dll位于相同的路径:s 。 – mstahlberg