开发者

how to reference compiled python code in IronPython?

开发者 https://www.devze.com 2023-01-22 02:30 出处:网络
we need to reference .py code from C#. This was solved using IronPython 2.6 The issue arises from the fact that .py code uses \'import custom开发者_如何学Clib\' which is a library compiled into custo

we need to reference .py code from C#. This was solved using IronPython 2.6

The issue arises from the fact that .py code uses 'import custom开发者_如何学Clib' which is a library compiled into customlib.pyc

IronPython gives error: IronPython.Runtime.Exceptions.ImportException: No module named customlib

attempted solution:

in python code add reference like this:

import sys
sys.path.append('c:\path to customlib')

that seems to work for other .py files but not for .pyc

Questions: 1) how do you reference .pyc in IronPython? 2) If its not possible, what are the alternatives to use .py and .pyc in C#.Net ? (obviously we don't have .py source code form customlib.pyc)

C# code that works for .py but not if .py imports .pyc:

ScriptEngine engine = Python.CreateEngine();
ScriptScope pyScope = null;
ScriptSource ss = null;
...
pyScope = engine.CreateScope();
ss = engine.CreateScriptSourceFromFile(@"C:\test.py");
ss.Execute(pyScope);
...
int result = (int)engine.Operations.InvokeMember(pyScope, "funcName")

thanks!


*.pyc files are CPython specific. You'll have to decompile them or invoke CPython.

For decompiling try:

  • http://sourceforge.net/projects/unpyc/
  • Free Python decompiler that is not an online service?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号