Ironpython和C#/VS 2010调试错误“未命名模块"尝试运行时选择 [英] Ironpython and C#/ VS 2010 debug error "No module named" optparse when trying to run
问题描述
大家好,
我正在尝试从C#表单应用程序中调用python脚本中的方法.我已经安装了IronPython 2.7,并且正在使用Visual Studio2010.(仅供参考,我尝试了一个简单版本,没有从脚本中导入任何内容,并且可以正常工作,我能够调用该方法)
该应用程序编译得很好,但是当我尝试运行它时,出现如下IronPyhton运行时错误:没有名为optparse的模块.这是我从python脚本文件导入的模块.似乎某处缺少python dll或错误的dll,因为如果我注释掉导入optparse行,则错误将移至下一个模块,即"os"模块.如果我注释掉该行,则错误将移至下一个导入,依此类推,我什至尝试重新排列导入模块的顺序.
我在VS项目中添加了对ipy,ironpython,ironpython.module和几个Microsoft建议的DLL的引用,例如Microsoft.Dynamic.
无济于事.可能有路径问题吗?我可以引用错误的DLL吗?
我在C:\ WINDOWS \ Microsoft.NET \ assembly \ GAC_MSIL下找到了这些DLL
所以这是它们被引用的地方.
其他人以前有类似的问题吗?
这是我包含在表单文件中的内容:
Hi all,
I am trying to make calls to methods in a python script from within my C# form app. I have installed IronPython 2.7, and I am using visual studio 2010. (FYI, I tried a simple version of this with no imports from my script, and it worked, I was able to make calls to the method)
The application compiles just fine, however when I try to run it, I get a IronPyhton runtime error like this: No module named optparse. This is a module I am importing from my python script file. It appears that there is a missing python dll somewhere, or the wrong dlls, because if I comment out the import optparse line, then the error moves to the next module which is the "os" module. If I comment out that line, the error moves to the next import, and so on,,,, I even tried re-arranging the order of the imported modules.
I have added references in my VS project to the ipy, ironpython, ironpython.module, and several Microsoft suggested DLLs, like Microsoft.Dynamic.
To no avail. Could there be a path issue? Could I be referencing the wrong DLLs?
I found these DLLs under the C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL
so this is where they are referenced to.
Anybody else have a similar issue like this before?
Here is what I am including in my form file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
using IronPython.Modules;
using IronPython.Runtime;
using System.IO;
这是我实现此功能的一小段代码,
Here is the little snippet of code where I implement this,
public Form1()
{
InitializeComponent();
var ipy = Python.CreateRuntime();
try
{
dynamic logfile = ipy.UseFile("logparser.py");
}
catch (FileNotFoundException)<pre>
{
MessageBox.Show("unable to open logparser.py");
}
}
这是我要在脚本中导入的模块:
And here are the modules I am importing in my script:
import System
import os
from datetime import datetime, timedelta
import sys
import re
import thread
import time
from optparse import OptionParser
推荐答案
我遇到了类似的问题,并找到了一种解决方法,即将带有导入模块的目录附加到路径中.
即
I had a similar issue and found a workaround by putting appending the directory with the imported module to the path.
i.e.
sys.path.append(r''c:\python\mylibrary'')
即使目录与主脚本文件所在的目录相同,我也必须这样做.
我还发现,如果我将带有导入模块的文件复制到正在加载并运行脚本的C#程序的工作目录中,则问题就消失了.
但是,我不太喜欢这两种解决方案,并且仍在寻找更好的解决方案.
I had to do this even if the directory was the same directory that the main script file lived in.
I also found that the issue went away if I copied the the file with the imported module to the working directory of the C# program that is loading and running the script.
I don''t like either solution particularly well, however, and am still trying to find a better one.
这篇关于Ironpython和C#/VS 2010调试错误“未命名模块"尝试运行时选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!