我被告知,由C#和F#源组成的汇编文件可以互操作,因为它们被编译成.NET程序集.
任何示例代码都会很棒.
我写了一个小脚本用于工作,当开发它时一切都很好但是当我把它翻过来用于生产时,人们抱怨显示第一个表单所花费的时间.展望它,似乎总是花在导入脚本使用的各种模块上.我创建了一个只包含import语句和打印Hello World的新脚本,然后将它作为python脚本和可执行文件运行,结果如下.发生了什么事,有什么方法可以加快速度吗?
Python文件:
$ for i in {1..10}
> do
> time ./ipy.exe time.py
> done
real 0m1.712s
real 0m1.701s
real 0m1.689s
real 0m1.691s
real 0m1.709s
real 0m1.663s
real 0m1.697s
real 0m1.700s
real 0m1.699s
real 0m1.741s
Run Code Online (Sandbox Code Playgroud)
使用ipy pyc.py /main:time.py/target:exe构建的exe
$ for i in {1..10}
> do
> time ./time.exe | grep -v user | grep -v sys
> done
real 0m22.119s
real 0m22.116s
real 0m22.133s
real 0m21.816s
real 0m21.985s
real 0m21.785s
real 0m22.010s
real 0m21.686s
real 0m21.877s
real 0m21.944s
Run Code Online (Sandbox Code Playgroud)
time.py的内容: …
我对在IronPython中导入模块的两种方法感到困惑.
一方面,IronPython 2.7.4附带的教程文档声明您可以使用常规import语法来完成:
import System
from System import Xml
Run Code Online (Sandbox Code Playgroud)
这可以像我期望的那样工作.
另一方面,互联网上的许多资源都表示导入模块的方法是使用如下clr模块:
import clr
clr.AddReference("System.Xml")
Run Code Online (Sandbox Code Playgroud)
这两种方法有什么区别?
我正在用C#调用IronPython函数.似乎在定义上,此函数捕获其原始范围.当我稍后在没有显式范围的情况下调用它时,它仍然可以访问该原始范围的值.即使我更改范围值,它也会正确读取新值.看看这个例子:
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace IronPythonFuncTest {
class Program {
static void Main() {
ScriptEngine scriptEngine = Python.CreateEngine();
// Create scope with a global value for the script to use
ScriptScope scriptScope = scriptEngine.CreateScope();
scriptScope.SetVariable("someGlobalValue", 10);
// Execute script defining function foo(x)
string script = "def foo(): print(someGlobalValue)";
ScriptSource scriptSource = scriptEngine.
CreateScriptSourceFromString(script, SourceCodeKind.Statements);
scriptSource.Execute(scriptScope);
// Extract foo from the scope
PythonFunction foo = scriptScope.GetVariable<PythonFunction>("foo");
// Change someGlobalValue
scriptScope.SetVariable("someGlobalValue", 42);
// Call foo. …Run Code Online (Sandbox Code Playgroud) 我在VS2012中使用IronPython并尝试调用.Net函数,该函数接受Ref参数,
Lib.dll
public int GetValue(ref double value)
{
...
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
import clr
clr.AddReference('Lib.dll')
from LibDll import *
value =0.0
x = GetValue(value)
Run Code Online (Sandbox Code Playgroud)
我错过了什么,在C#中我们ref一起使用变量名,在Python中这是什么?
我是Python的全新手.我想试着看看Pyomo(用于数学建模的Python包)是否可以在IronPython下工作,因为我生成Pyomo所需数据的所有代码都在C#中.
我安装了IronPython 2.7.5,然后作为管理员尝试了http://blog.ironpython.net/2014/12/pip-in-ironpython-275.html#disqus_thread中有关如何为IronPython运行"pip"的建议.我使用了他们给出的确切示例(安装html5lib):
ipy -X:Frames -m ensure pip
ipy -X:Frames -m pip install html5lib
Run Code Online (Sandbox Code Playgroud)
第一行工作,我在Lib/site-packages目录中看到一个文件夹,里面pip-6.1.1-py2.7.egg有很多python代码.
对于第二行,我收到了错误:
Unhandled exception:
Traceback (most recent call last):
File "C:\Program Files (x86)\IronPython 2.7\Lib\runpy.py", line 170, in run_module
File "C:\Program Files (x86)\IronPython 2.7\Lib\runpy.py", line 111, in _get_module_details
ImportError: No module named urllib; 'pip' is a package and cannot be directly executed
Run Code Online (Sandbox Code Playgroud)
此时,我被困住了.任何帮助,将不胜感激.
为了防止这种情况有所帮助,打印出来sys.version:
2.7.5 (IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.34209 (32-bit))
Run Code Online (Sandbox Code Playgroud) 我的目标是将Python连接到.NET以进行处理.我正在使用IronPython.Python中的部分涉及一些我使用Pandas库的计算.
有没有办法连接IronPython和Pandas?
我看到使用IronPython,我可以访问所有基本的Python功能.如何扩展这一点以访问第三方功能/包(如Pandas这里)?
我有一个脚本,我想在Python3.5和IronPython2.7中执行.
该脚本最初是用Python3编写的,所以我有一些类似于下面代码的嵌套循环:
myIter0 = iter(['foo','foo','bar','foo','spam','spam'])
myIter1 = iter(['foo','bar','spam','foo','spam','bar'])
myIter2 = iter([1,2,3,4,5,6])
for a in myIter0:
for b, c in zip(myIter1, myIter2):
if a + b == 'foobar':
print(c)
break
Run Code Online (Sandbox Code Playgroud)
现在,如果我在IronPython2.7中运行它,我得不到相同的结果,因为zip返回一个列表而不是迭代器.
为了避免这个问题,我想我会这样做:
import sys
if sys.version_info.major == 2:
from itertools import izip as _zip
else:
_zip = zip
myIter0 = iter(['foo','foo','bar','foo','spam','spam'])
myIter1 = iter(['foo','bar','spam','foo','spam','bar'])
myIter2 = iter([1,2,3,4,5,6])
for a in myIter0:
for b, c in _zip(myIter1, myIter2):
if a + b == 'foobar':
print(c)
break
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
这是对以下问题的扩展:
如何为IronPython 2.7安装numpy和scipy
我已经成功安装了两个软件包,并且可以从命令行导入ipy.exe,但是当我尝试将numpy导入嵌入另一个.NET应用程序的引擎时,我收到一个错误.跟踪VS中的错误,我很确定导入多阵列时会引发错误.具体来说,在调用ModuleMethods.__init__()multiarray.py时.错误消息是:
init()只需1个参数(给定0)
我能够添加对NumpyDotNet的引用并从NumpyDotNet导入执行*
当我启动引擎时,我确实在我的范围内有这些路径:C:\ Program Files(x86)\ IronPython 2.7\Lib; C:\ Program Files(x86)\ IronPython 2.7\Lib\site-packages; C:\ Program Files(x86)\ IronPython 2.7\DLLs
因此,我的嵌入式环境的其他内容不能正确设置(并且它没有在选项字典中设置"Frames = True").我已经看到其他问题,人们有类似的问题,但没有他们似乎在这里得到这个远,看到这个错误信息.在我的情况下,找到numpy包并加载NumpyDotNet.dll.
所以我的dockerfile是:
FROM iron/python:2.7
WORKDIR /app
ADD . /app
RUN pip install --upgrade pip
RUN pip install -r ./requirements.txt
Run Code Online (Sandbox Code Playgroud)
但是最近,当我用以下方法构建图像时:
docker build --no-cache -t <image name>:<tag>
我遇到了以下问题:
Step 4/6 : RUN pip install --upgrade pip
---> Running in 00c781a53487
/bin/sh: pip: not found
The command '/bin/sh -c pip install --upgrade pip' returned a non-zero code: 127
Run Code Online (Sandbox Code Playgroud)
Docker是否有任何可能引起此变化的变化?因为上周一切都很好,并且使用完全相同的代码构建映像没有问题。