我想用C#构造一个IronPython元组.这些是PythonTuple的公共构造函数:
public PythonTuple();
public PythonTuple(object o);
Run Code Online (Sandbox Code Playgroud)
例如,我将如何构造元组(1,2,3)?
我有一些嵌入在C#项目中的IronPython脚本,能够在VS编辑器中编辑它们会很方便.VS显然知道Python,因为它为它提供了语法着色.不幸的是,编辑器使用制表符进行缩进,而我想要空格.有没有改变这个的设置?我没有在Tools/Options/TextEditor下看到Python的标题.
我正试图在我的游戏中嵌入脚本引擎.由于我是用C#编写的,我认为IronPython非常合适,但我能够找到的例子都集中在用C#调用IronPython方法而不是IronPython脚本中的C#方法.
更复杂的是,我在Windows 7 64位上使用Visual Studio 2010 RC1.
IronRuby的工作方式与我预期的一样,但我对Ruby或Python语法不是很熟悉.
我在做什么:
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
//Test class with a method that prints to the screen.
scope.SetVariable("test", this);
ScriptSource source =
engine.CreateScriptSourceFromString("test.SayHello()", Microsoft.Scripting.SourceCodeKind.Statements);
source.Execute(scope);
Run Code Online (Sandbox Code Playgroud)
这会产生错误,"'TestClass'对象没有属性'SayHello'"
使用"self.test.SayHello()"时,这个确切的设置适用于IronRuby
我使用IronRuby时很谨慎,因为它看起来并不像IronPython那样成熟.如果它足够接近,我可能会接受它.
有任何想法吗?我知道这必须是简单的事情.
有人可以推荐这个ironpython bug的解决方法吗?
我有一个包含在外部类库中的类.我在嵌入式ironpython实例中使用了这个类.当我的c#app从范围中检索到类时,这些类似乎不匹配!
我的python脚本:
import sys
import clr
from ExternalAssembly import *
from IronPythonBug import *
internalClass = InternalClass("internal")
externalClass = ExternalClass("external")
Run Code Online (Sandbox Code Playgroud)
我的c#app:
internalClass = scope.GetVariable("internalClass");
externalClass = scope.GetVariable("externalClass");
if (internalClass is InternalClass)
Console.WriteLine("IternalClass matches");
else
Console.WriteLine("Error: InternalClass does not match");
if (externalClass is ExternalClass)
Console.WriteLine("ExternalClass matches");
else
Console.WriteLine("Error: ExternalClass does not match");
Run Code Online (Sandbox Code Playgroud)
控制台输出:
IternalClass matches
Error: ExternalClass does not match
Run Code Online (Sandbox Code Playgroud)
随意下载一个说明此错误的项目:http: //www.virtual-chaos.net/zip/IronPythonBug.zip
一直试图将这个IronPython程序编译成一个工作的.exe,过去3-4个小时没有运气.
我正在使用"IronPython 2.6 for .NET 4.0"附带的pyc.py
D:\IronTestCompile>ipy pyc.py file1.py file2.py /out:Program /main:program.py /target:exe
该程序编译为Program.dll和Program.exe但Program.exe无法运行.
我用它作为指南:
我开始工作的非常简单的'HelloWorld'程序,但是我的更复杂的程序失败了.我认为我的复杂程序无法按编译运行,因为它缺少DLL?
从IronPython目录中,我复制了编译所需的所有必需的DLL(Microsoft*.dll,IronPython*.dll)以及运行编译的HelloWorld.exe的运行时先决条件.
我怎么能弄清楚为什么我的.exe不能运行?或者,如何确定需要将哪些.NET DLL文件包含在目录中?(假设我必须将所有.NET DLL放在.exe目录示例中C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll -> D:\IronTestCompile?)
我在程序目录中唯一的DLL是:
Microsoft.Scripting.dll
IronPython.dll
IronPython.Modules.dll
Microsoft.Dynamic.dll
Run Code Online (Sandbox Code Playgroud)
我没有Microsoft.Scripting.Core.dll或Microsoft.Scripting.ExtensionAttribute.dll 导致我运行IronPython 2.6 for .NET 4.0
我应该有更多.dll吗?我的IronPython程序使用System.Windows.Forms以及其他一些...
谢谢你的帮助.
目标是编译这个程序并获取zip文件中的所有.DLL,以便我的兄弟可以解压缩并在他的comp上运行它,但截至目前我甚至无法在我自己的comp中运行它!有任何想法吗?
编辑:所以我弄清楚出了什么问题(请参阅下面的答案)但是现在我尝试在另一台计算机上运行应用程序时会得到这个可爱的弹出窗口:
Dang,现在我在尝试在另一台机器上运行app时遇到此错误.
要运行此应用程序,首先必须安装以下版本的.NET Framework之一:
v4.0.30319
不知道有没有办法解决这个问题.
编辑:我的修复是,我的兄弟有.NET 3.5所以我在另一台运行.NET 3.5的IronPython机器上重新编译它
我有以下IronPython代码.
class Hello:
def __init__(self):
pass
def add(self, x, y):
return (x+y)
Run Code Online (Sandbox Code Playgroud)
我需要从C#调用它,我想出了以下代码.
using System;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;
class Hello {
public static void Main()
{
ScriptEngine engine = Python.CreateEngine();
ScriptSource script = engine.CreateScriptFromSourceFile("myPythonScript.py");
ScriptScope scope = engine.CreateScope();
script.Execute(scope);
}
}
Run Code Online (Sandbox Code Playgroud)
复制IronPython.dll后,我运行以下命令.(我试图运行gacutil,但是我遇到了一些错误.
dmcs /r:IronPython.dll callipy.cs
但是我得到了一些错误消息,如下所示.
Could not load file or assembly 'Microsoft.Scripting.ExtensionAttribute, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Missing method .ctor in assembly /Users/smcho/Desktop/cs/namespace/IronPython.dll, type System.Runtime.CompilerServices.ExtensionAttribute Can't find …
我即将开始一个.NET项目:我们需要创建一个利用一些现有C#代码的创建程序,我希望使用IronPython.(我是一位经验丰富的Python程序员,但有点像.NET新手).
我的问题是:IronPython是否适合使用大多数C#代码,或者仅仅使用C#会更好吗?如果IronPython很合适,是否有任何陷阱需要注意?
我需要打印'x/y'的结果,但它总是返回'0'.当我打印'x'时,它正确地告诉我,当我打印'y'时它正确告诉我但是当我打印'x/y'时它表示0.
这是我的代码:
import random
y = 0
x = 0
p = 1
while True:
i = [random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100), random.randint(1,100)]
if len(set(i)) < len(i):
print "Match!"
x += 1
y += 1
print(x/y)
else:
print "No Match!"
y += 1
print y
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,它应该打印得y很好,但是在需要打印x/y它的事件中打印0.我也尝试过打印x/y和打印,x//y但它们也不起作用.
我究竟做错了什么?
提前致谢
在C#中,我有一个声明为的属性:
public fixed byte foo[10]
Run Code Online (Sandbox Code Playgroud)
在客户端代码中,我看到它使用此函数转换为字符串:
public static unsafe string GetString(byte* byteArray)
{
return new String((sbyte*)byteArray);
}
Run Code Online (Sandbox Code Playgroud)
在IronPython打印中,它给了我一个字符串类型:
>>> print obj.foo
Baz+<foo>e__FixedBuffer1
Run Code Online (Sandbox Code Playgroud)
尝试使用转换函数会出错.
>>> print GetString(obj.foo)
expected Byte*, got <Foo>e__FixedBuffer1
Run Code Online (Sandbox Code Playgroud)
在IronPython中读取此属性的正确方法是什么?
ironpython ×10
c# ×5
python ×4
.net ×3
ironruby ×2
asp.net ×1
asp.net-mvc ×1
compilation ×1
division ×1
dll ×1
executable ×1
mono ×1
printing ×1
random ×1