我喜欢 PyCharm,并且之前在我的 Python 项目中使用过它,但我刚刚开始使用 IronPython,我不知道如何使 PyCharm 或任何其他 IDE(除了工作正常的 VS)识别 .NET图书馆。
例如,我有以下代码:
from System.Environment import *
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
上面的代码工作正常,但“系统”部分到处都有灰色下划线,并显示消息“未解析的参考系统”。 IronPython 文档解释说,System 不是一个模块,而是一个命名空间。如果我执行type(system)
,我会得到
。那么有没有办法让 PyCharm/PyDev 也识别命名空间呢?顺便说一句,路径很好,一切都很好。
I like PyCharm and have used it before for my Python projects, but I just started messing with IronPython and I can't figure out how to make PyCharm or any other IDE for that matter (except for VS which works fine) recognize the .NET libraries.
For example, I have the code:
from System.Environment import *
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
The above code works fine, but the "System" part is underlined grey everywhere with the message "Unresolved reference System". The IronPython documentation explains that System is not a module but rather a namespace. If I do type(system)
I get <type 'namespace#'>
. So is there a way to make PyCharm/PyDev recognize the namespaces as well? On a side note, the PATH is fine, everything is fine.
发布评论
评论(3)
出于性能原因,PyCharm 默认情况下不会为 .NET 程序集生成 Python 存根。您可以通过将插入符号放在导入语句中未解析的引用上,按 Alt-Enter 并选择“为二进制模块生成存根...”快速修复来触发生成。
For performance reasons, PyCharm does not generate Python stubs for .NET assemblies by default. You can trigger the generation by putting the caret on an unresolved reference in an import statement, pressing Alt-Enter and selecting the "Generate stubs for binary module ..." quickfix.
请注意,这应该在 PyDev 中正常工作(只需确保配置 IronPython 解释器并将项目配置为 IronPython 项目)。
另一个注意事项是上面的代码实际上应该是:
Just to note, this should work properly in PyDev (just make sure that you configure an IronPython interpreter and configure your project as an IronPython project).
Another note is that the code above should actually be:
不是 PyCharm,而是 Michael Foord 有关于如何使用 Wing 执行此操作的信息: http ://www.voidspace.org.uk/ironpython/wing-how-to.shtml - PyCharm 可能有一些类似的机制可用。
Not PyCharm but Michael Foord has info on how to do this w/ Wing: http://www.voidspace.org.uk/ironpython/wing-how-to.shtml - PyCharm may have some similar mechanism available.