IronPython Visual Studio控制台应用程序:没有名为clr的模块。

3
我在Visual Studio 2017 Community中使用Iron-Python控制台应用程序遇到了问题。当我尝试通过单击F5或在Python Interactive中执行程序时,Visual Studio会调用一个存在的消息:

Traceback (most recent call last): File "d:\userdata\myAccount\my documents\visual studio 2017\Projects\IronPythonApplication3\IronPythonApplication3\ IronPythonApplication3.py", line 6, in import clr ImportError: No module named clr

这是我正在运行的代码:

import sys;
print("testMsg");
print("anotherMsg");
import clr;
clr.AddReference('System.Threading')
from System.Threading import Thread
world = "world"
print('Hello ' + world)
Thread.CurrentThread.Sleep(5000)

前两个打印函数正确调用。

Intelisense还会弹出一个窗口,可能会在该模块中缺失。

我在 stack-overflow 上看到了其他话题,并尝试刷新数据库和删除 Python 环境,但仍然无法使其工作。

有谁能帮助我找到解决这个问题的方法?


当在stackoverflow上搜索“no module named clr”时,以下内容将作为顶部结果显示:https://dev59.com/52nWa4cB1Zd3GeqP2qNc#13183325 ...这有帮助吗?您是否已安装IronPython运行时+解释器,并以一种被VS捕获的方式安装? - Simon Opelt
是的,当我安装Visual Studio 2017时,我也安装了Python的完整支持。 - Robert Pawlak
我还将其放入系统环境变量中,准确地说,放入了PATH:C:\Program Files (x86)\Python36-32。但仍然无法工作。 - Robert Pawlak
2
你无法在CPython 3.6上运行IronPython特定的代码。IronPython的路径应该类似于C:\Program Files (x86)\IronPython 2.7 - Simon Opelt
@SimonOpelt,在我的Program Files(x86)目录中只有一个名为“Python36-32”的文件夹。但是在系统目录中,我有“Python27”和“Python27amd64”两个文件夹。 - Robert Pawlak
显示剩余2条评论
1个回答

4

解决方案

按照以下步骤操作:

  • 此网站下载IronPython-2.7.7.msi文件。

并安装该文件。

  • 在系统环境变量PATH中添加

    C:\Program Files (x86)\IronPython 2.7

然后重新启动Visual Studio。

  • 在“解决方案”->“IronPythonProject”->“Python Environments”->“添加/删除Python环境”中标记“IronPython 2.7(64位)”,最后运行程序。

  • 更改解释器:在“解决方案”->“IronPythonProject”->“属性”->“解释器”中设置为“IronPython 2.7(64位)”。

  • 刷新IntelliSense数据库:在“解决方案”->“IronPythonProject”->“PythonEnvironments”->“查看所有Python环境”->“IntelliSense”中点击“刷新DB”按钮。

结论

在Visual Studio 2017中进行基本的IronPython安装(和IronPython应用程序模板)似乎是不正确的,因为我们需要从外部来源安装模块。Visual Studio的安装是不足够的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接