Iron Python - 没有名为"os"的模块

-3 投票
3 回答
15463 浏览
提问于 2025-04-16 19:45

我一直在尝试调试这段示例代码,但遇到了一些困难。

这是代码:

import System
import get_events_devices
from System.Windows.Forms import *
from System.ComponentModel import *
from System.Drawing import *
from clr import *
clr.AddReference("IronPython") 

class Check: # namespace

    class Form1(System.Windows.Forms.Form):
        """type(_label1) == System.Windows.Forms.Label, type(_button1) == System.Windows.Forms.Button"""
        __slots__ = ['_label1', '_button1']
        def __init__(self):

            self.InitializeComponent()

        @accepts(Self(), bool)
        @returns(None)
        def Dispose(self, disposing):

            super(type(self), self).Dispose(disposing)

        @returns(None)
        def InitializeComponent(self):
            self._button1 = System.Windows.Forms.Button()
            self._label1 = System.Windows.Forms.Label()
            self.SuspendLayout()
            # 
            # button1
            # 
            self._button1.Location = System.Drawing.Point(58, 61)
            self._button1.Name = 'button1'
            self._button1.Size = System.Drawing.Size(75, 23)
            self._button1.TabIndex = 0
            self._button1.Text = 'Click'
            self._button1.UseVisualStyleBackColor = True
            self._button1.Click += self._button1_Click
            # 
            # label1
            # 
            self._label1.AutoSize = True
            self._label1.Location = System.Drawing.Point(190, 70)
            self._label1.Name = 'label1'
            self._label1.Size = System.Drawing.Size(0, 13)
            self._label1.TabIndex = 1
            # 
            # Form1
            # 
            self.ClientSize = System.Drawing.Size(292, 273)
            self.Controls.Add(self._label1)
            self.Controls.Add(self._button1)
            self.Name = 'Form1'
            self.Text = 'Form1'
            self.ResumeLayout(False)
            self.PerformLayout()

        @accepts(Self(), System.Object, System.EventArgs)
        @returns(None)
        def _button1_Click(self, sender, e):

            # Snippet Statement
            pass
            # End Snippet Statement
            self._label1.Text = 'mohsin'
            get_events_devices.units()

我刚刚添加了optparse.py、get_events_devices.py和iron python.dll。

我已经安装了Iron Python,但我找不到ironpython_module.dll这个文件,应该从哪里获取呢?

有没有什么建议?

3 个回答

0

试着加上这段代码:

import sys

sys.path.append("c:\python27\lib")

这段代码对我有效,解决了错误。

2

在Python脚本中,你需要明确地导入os.py的库路径。

import sys
sys.path.append("C:\\Program Files (x86)\\IronPython 2.7\\Lib\\")
4

如果你在使用Visual Studio通过IronPython工具运行和调试Python代码,可能会发现你的sys.path没有包含所有IronPython模块的引用,比如os模块。

在你的代码中,可以尝试添加以下内容来指明IronPython的安装目录:

import sys
sys.path.append(r"C:\Program Files\IronPython 2.7\Lib")

以上是针对这个已报告的bug的一个临时解决办法。

撰写回答