IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.43k stars 284 forks source link

Error with running IronPython 3.4.1 on existing Windows .NET4.7.2 project #1802

Open diazjacob opened 1 month ago

diazjacob commented 1 month ago

Prerequisites

I'm working to add the latest stable version of IronPython to a large existing project. This project runs .NET 4.7.2, built for Windows x64 and built on Windows 10 with Visual Studio 2022. IronPython was added via NuGet.

I've had this issue for quite a while and have done a significant amount of research, but there is not much information on this topic.

Description

When running IronPython the initialization of an engine with Python.CreateEngine(); functions properly, but when I attempt to execute engine.Execute(code, scope); with the correct relevant code string and scope object I get a PlatformNotSupportedException. I have had a great deal of trouble trying to find the cause of this issue. This is the specific error:

PlatformNotSupportedException: Operation is not supported on this platform.
  at Microsoft.Scripting.Interpreter.LightLambda.MakeRunDelegateCtor (System.Type delegateType) [0x001f4] in <9784853821ea47dbb6bb1f3e03091049>:0 
  at Microsoft.Scripting.Interpreter.LightLambda.GetRunDelegateCtor (System.Type delegateType) [0x00023] in <9784853821ea47dbb6bb1f3e03091049>:0 
  at Microsoft.Scripting.Interpreter.LightLambda.MakeDelegate (System.Type delegateType) [0x00000] in <9784853821ea47dbb6bb1f3e03091049>:0 
  at Microsoft.Scripting.Interpreter.LightDelegateCreator.CreateDelegate (System.Runtime.CompilerServices.StrongBox`1[System.Object][] closure) [0x00047] in <9784853821ea47dbb6bb1f3e03091049>:0 
  at Microsoft.Scripting.Interpreter.LightDelegateCreator.CreateDelegate () [0x00000] in <9784853821ea47dbb6bb1f3e03091049>:0 
  at Microsoft.Scripting.Ast.LightExpression`1[T].Compile (System.Int32 compilationThreshold) [0x0000c] in <9784853821ea47dbb6bb1f3e03091049>:0 
  at IronPython.Compiler.PythonScriptCode.CompileBody (Microsoft.Scripting.Ast.LightExpression`1[T] lambda) [0x00057] in <57a5bd653356491bba8a61ecf9211193>:0 
  at IronPython.Compiler.PythonScriptCode.EnsureTarget (System.Boolean register) [0x00015] in <57a5bd653356491bba8a61ecf9211193>:0 
  at IronPython.Compiler.PythonScriptCode.GetTarget (System.Boolean register) [0x00021] in <57a5bd653356491bba8a61ecf9211193>:0 
  at IronPython.Compiler.PythonScriptCode.RunWorker (IronPython.Runtime.CodeContext ctx) [0x00000] in <57a5bd653356491bba8a61ecf9211193>:0 
  at IronPython.Compiler.PythonScriptCode.Run (Microsoft.Scripting.Runtime.Scope scope) [0x00023] in <57a5bd653356491bba8a61ecf9211193>:0 
  at IronPython.Compiler.RuntimeScriptCode.InvokeTarget (Microsoft.Scripting.Runtime.Scope scope) [0x000c6] in <57a5bd653356491bba8a61ecf9211193>:0 
  at IronPython.Compiler.RuntimeScriptCode.Run (Microsoft.Scripting.Runtime.Scope scope) [0x00000] in <57a5bd653356491bba8a61ecf9211193>:0 
  at Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope scope, Microsoft.Scripting.ErrorSink errorSink) [0x0001e] in <0d164baeace648b2930cfd25249cf9f3>:0 
  at Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope scope) [0x00000] in <0d164baeace648b2930cfd25249cf9f3>:0 
  at Microsoft.Scripting.Hosting.ScriptSource.Execute (Microsoft.Scripting.Hosting.ScriptScope scope) [0x00017] in <0d164baeace648b2930cfd25249cf9f3>:0 
  at (wrapper remoting-invoke-with-check) Microsoft.Scripting.Hosting.ScriptSource.Execute(Microsoft.Scripting.Hosting.ScriptScope)
  at **[CALLING OBJECT]**..ctor () [0x00020] in . . .

Any help or recommendations on project details I need to verify would be greatly appreciated. Thanks!

iankona commented 10 hours ago

运行环境:戴森球计划dspgame BepInEx5.4.23.1 + IronPython3.4.1+net4.6.2 [Message: BepInEx] BepInEx 5.4.23.1 - DSPGAME (2024/6/4 11:31:23) [Info : BepInEx] Running under Unity v2018.4.12.5889476 [Info : BepInEx] CLR runtime version: 4.0.30319.17020

i meet the same wrong i use import deal with it, not use scope: pyfile global a =0 // print(a) --> 0 void Start() engine.Execute("import manage; manage.Start()"); // a +=1 print(a) --> 1 void Update() engine.Execute("import manage; manage.Update()"); // a +=1 print(a) --> 2 void OnGUI() engine.Execute("import manage; manage.OnGUI()"); // a +=1 print(a) --> 3 使用python模块脚本的全局单一特性,存储数据,从而保证数据的一致性。 (不管import多少次,只有第1次import是生效的,而其他import则是调用)

===============================================

using System; using System.Collections;

using UnityEngine;

using BepInEx; using HarmonyLib;

using IronPython.Hosting; using IronPython.Runtime;

using System.IO; using Microsoft.Scripting.Runtime; using Microsoft.Scripting; using System.Collections.Generic; using System.Collections.ObjectModel; using Microsoft.Scripting.Hosting; using System.Reflection; using System.Runtime.Remoting.Contexts;

namespace IronPython3Mod { [BepInPlugin(GUID, NAME, VERSION)] [BepInProcess(GAME_PROCESS)] public class Plugin : BaseUnityPlugin { public const string GUID = "cn.zhufile.dsp.zhu_ironpython3_mod"; public const string NAME = "IronPython3Mod"; public const string VERSION = "1.0.7"; private const string GAME_PROCESS = "DSPGAME.exe";

    public static ScriptEngine engine = null;
    public static float time = 0;

    public void Start()
    {
        engine = Python.CreateEngine();// Create a new IronPython engine
        // engine.Execute("print('111111111111111111')"); // OK, it work!!!

        // Set up the search paths for the Python script
        var searchPaths = engine.GetSearchPaths();
        searchPaths.Add("BepInEx\\plugins\\IronPython3\\net462\\Lib");
        searchPaths.Add("BepInEx\\plugins\\IronPython3\\mod");
        searchPaths.Add("BepInEx\\plugins\\IronPython3\\modmanage");
        engine.SetSearchPaths(searchPaths);

        // 运行python脚本文件,加载mod
        engine.Execute("import manage; manage.Start()");

    }

    public void Update()
    {
        time += Time.deltaTime; // 每帧的时间间隔,单位秒
        if (time > 30)
        {
            time = 0;
            engine.Execute("import manage; manage.Update()");
        }

    }

    public void OnGUI()
    {
        engine.Execute("import manage; manage.OnGUI()");
    }

}

============================================== anather, there are many different in ipy.exe and bepinex‘s ironpython3 engine for example, a = os.listdir(".") do it in ipy.exe, but in bepinex‘s ironpython3 engine is wrong !!!