我正在尝试使用 clr 模块中的 AddReference('example_file') 方法添加对“example_file.dll”的引用:
>>>import clr
>>>clr.AddReference('example_file')
Run Code Online (Sandbox Code Playgroud)
结果我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
System.IO.FileNotFoundException: Unable to find assembly 'example_file'.
at Python.Runtime.CLRModule.AddReference(String name)
Run Code Online (Sandbox Code Playgroud)
所有文件都位于当前工作目录中,sys.path 如下所示:
>>> sys.path
['', 'C:\\Python27\\lib\\site-packages\\pip-1.2.1-py2.7.egg', 'C:\\Python27', 'C
:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27\\lib\\
site-packages', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\']
Run Code Online (Sandbox Code Playgroud)
此外,由于:
>>>clr.FindAssembly('example_file')
Run Code Online (Sandbox Code Playgroud)
我明白了
u'example_file.dll
Run Code Online (Sandbox Code Playgroud)
问题一天天出现。我很困惑,因为它以前工作得很好 - 我不知道什么会对此产生影响。我正在使用 Windows 7 x64、python 2.73 和 .Net Framework 4
是否可以使用.NET框架和Python scrapy框架来从不同站点抓取数据?我正在做最后一年的项目,我想使用 C# 作为前端语言,使用 Python 来抓取数据。
如何从 Python 中调用 IronPython 函数?有没有一种简单的方法可以将两者连接起来。我需要 IronPython 中不提供的一整套适当的 Python 库的灵活性,以及 Python .net 目前不具备的最新 CLR。
到目前为止,我尝试过编译 IronPython dll,但无法在 Python 中正确加载它。
我尝试让 IronPython n 可从 Python 调用
foo.py
def foo():
print 'hello world'
Run Code Online (Sandbox Code Playgroud)
编译ipy.py
import clr
clr.CompileModules("foo.dll", "foo.py")
Run Code Online (Sandbox Code Playgroud)
我尝试从 Python 调用 Iron Python
call_ipy_from_py1.py
import ctypes
dll = ctypes.WindDLL('foo.dll')
dll.foo() # AttributeError: function 'foo' not found
Run Code Online (Sandbox Code Playgroud)
call_ipy_from_py2.py
import ctypes
dll = ctypes.cdll.LoadLibrary('foo.dll')
dll.foo() # AttributeError: function 'foo' not found
Run Code Online (Sandbox Code Playgroud) 在我的 C# 应用程序中,我有一个文本编辑器,允许用户输入 IronPython 脚本。我已经实现了一组可用于 python 环境的 C# 类。
我现在想实现一个“智能感知”类型的系统,用户输入一个变量名,然后输入一个点,它会提示用户输入可用方法和属性的列表。
例如,这是一个 IronPython 脚本:
foo = MyClass()
foo.
Run Code Online (Sandbox Code Playgroud)
此时光标就位于点之后。C# 中的 MyClass 示例:
public class MyClass {
public void Func1() ...
public void Func2() ...
}
Run Code Online (Sandbox Code Playgroud)
现在我想给用户一个弹出列表,显示 Func1()、Func2() 等。
我需要做的是获取变量名“foo”并获取类 MyClass。
请注意,我无法执行 IronPython 代码来执行此操作,因为它在用户界面中执行操作。
这就是我所能达到的程度:
ScriptSource Source = engine.CreateScriptSourceFromString(pycode, SourceCodeKind.File);
SourceUnit su = HostingHelpers.GetSourceUnit(Source);
Microsoft.Scripting.Runtime.CompilerContext Ctxt = new Microsoft.Scripting.Runtime.CompilerContext(su, new IronPython.Compiler.PythonCompilerOptions(), ErrorSink.Null);
IronPython.Compiler.Parser Parser = IronPython.Compiler.Parser.CreateParser(Ctxt, new IronPython.PythonOptions());
IronPython.Compiler.Ast.PythonAst ast = Parser.ParseFile(false);
if (ast.Body is IronPython.Compiler.Ast.SuiteStatement)
{
IronPython.Compiler.Ast.SuiteStatement ss = ast.Body as …Run Code Online (Sandbox Code Playgroud) 采用这个简单的C#LINQ查询,并想象这db.Numbers是一个包含一列的SQL表Number:
var result =
from n in db.Numbers
where n.Number < 5
select n.Number;
Run Code Online (Sandbox Code Playgroud)
这将在C#中非常有效地运行,因为它会生成类似的SQL查询
select Number from Numbers where Number < 5
Run Code Online (Sandbox Code Playgroud)
它没有做的是从数据库中选择所有数字,然后在C#中过滤它们,因为它可能在最初时看起来像.
Python支持类似的语法:
result = [n.Number for n in Numbers if n.Number < 5]
Run Code Online (Sandbox Code Playgroud)
但是这里的if子句在客户端而不是服务器端进行过滤,效率低得多.
在Python中有没有像LINQ一样高效的东西?(我目前正在评估Python与IronPython和Boo的比较,因此可以使用任何一种语言的答案都可以.)
请考虑以下代码:
ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();
scope.SetVariable("message", "Hello, world!");
string script = @"print message";
ScriptSource source = scope.Engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
source.Execute();
Run Code Online (Sandbox Code Playgroud)
此代码产生以下异常:
Microsoft.Scripting.Runtime.UnboundNameException未处理Message ="name'message'未定义"
我错过了什么?
我有一个列表理解操作.NET数组的元素,如
obj.arr = [f(x) for x in obj.arr]
Run Code Online (Sandbox Code Playgroud)
然而,返回obj.arr的任务失败了.
是否可以在IronPython中将列表转换为.NET数组?
我在IronPython中托管IronPython.我没有找到用相当于命令行参数初始化它的方法:-X:FullFrames.
我的代码有点像:
import clr
clr.AddReference('IronPython')
clr.AddReference('Microsoft.Scripting')
from IronPython.Hosting import PythonCommandLine, PythonConsoleOptions, Python
clr.AddReference('Microsoft.Dynamic')
clr.AddReference('System')
import System
from Microsoft.Scripting.Hosting.Shell import IConsole
engine = Python.CreateEngine()
runtime = engine.Runtime
for assembly in clr.References:
runtime.LoadAssembly(assembly)
cmdLine = PythonCommandLine()
runtime.Globals.SetVariable('cur_cmdLine',cmdLine)
cmdLine.Run(engine, my_console(), PythonConsoleOptions())
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010 + Ironpython for .net4
我想在ironpython中使用numpy,他们说我必须使用帧支持.所以,我应该在命令行上使用-X:Frames或-X:FullFrames运行ipy.
但是,我有两个问题:1.我可以在Ironpython Interactive控制台中使用-X:Frames或-X:FullFrames吗?
2.如果我使用C#4加载包含numpy的py,我该如何使用-X:Frames或-X:FullFrames等extern参数?
非常感谢.
如何将命令行参数从我的C#应用程序传递到IronPython 2.x?Google仅返回有关如何使用Iron Python 1.x执行此操作的结果.
static void Main(string[] args)
{
ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();
// Pass in script file to execute but how to pass in other arguments in args?
ScriptScope scope = scriptRuntime.ExecuteFile(args[0]);
}
Run Code Online (Sandbox Code Playgroud) ironpython ×10
c# ×5
python ×5
.net ×2
boo ×1
command-line ×1
frames ×1
interactive ×1
interop ×1
linq ×1
linq-to-sql ×1
python-2.7 ×1
python.net ×1
scrapy ×1