quaquel / pyNetLogo

BSD 3-Clause "New" or "Revised" License
82 stars 22 forks source link

pynetlogo calling a model which uses py extension #79

Open SebasMeza opened 1 year ago

SebasMeza commented 1 year ago

Hi,

I hope to receive any help here.

I'm trying to run an old model that I initialized in Python through pynetlogo and that model has inside as well usage of the py extension. I know it is a strange usage to call a NetLogo model from Python and then use Python from the model but it is something I did some years ago and worked properly.

Nowadays I'm trying to set up again the model (for research purposes) but I'm getting the error shown in the image and the following message:

`java.lang.RuntimeException at netLogoLink.NetLogoLink.loadModel(NetLogoLink.java:121)

Traceback (most recent call last):

  File NetLogoLink.java:121 in netLogoLink.NetLogoLink.loadModel

Exception: Java Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File ~\anaconda3\lib\site-packages\pynetlogo\core.py:246 in load_model
    self.link.loadModel(path)

java.lang.RuntimeException: java.lang.RuntimeException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  Cell In[4], line 1
    model.load_model('C:/Users/sebas/Downloads/AGuerra/Artículo/models/simulation.nlogo')

  File ~\anaconda3\lib\site-packages\pynetlogo\core.py:249 in load_model
    raise NetLogoException(str(ex))

NetLogoException: java.lang.RuntimeException`

I guess the error raises because I'm also calling Python from the models called by Python. I guess it is because I call other models without the Python extension and it works fine.

Let me know please what the cause could be and if I making any mistake or I'm guessing well. Just trying to find a solution but I'm still not there.

@quaquel

image

bussayii commented 1 year ago

Hi!

I have been trying to fix this problem for weeks too. I have this exact error and my model also uses py extension.

At first, my environment was like this ...

I have used pynetlogo in 2021 successfully. Thus, I tried downgrading everything to older versions as follows...

Both of my ubuntu 22.04 and windows 10 face this same problem.

Please let me know if i had done anything wrong or if there is a solution. Thank you in advance.

bussayii commented 1 year ago

I have tried this too. Running my model gives the same error. I didn't run the simple model though.

And I have been alternating between openjdk 11 and openjdk 8 too.

quaquel commented 1 year ago

There is no immediate recent change in pynetlogo that comes to mind that might explain this problem. Can either of you test a model with some other extension?

bussayii commented 1 year ago

Hello! Thank you for the answer!

I have tried opting in one extension at a time and opting out one at a time to this simple model. The model with that exact list of extensions works fine on NetLogo 6.3 standalone, but gives error when loading though pynetlogo.

The model

> extensions [ cf csv gis matrix py table vid] > > to setup > clear-all > file-close-all ; Close any files open from last run > reset-ticks > end > > ; procedure to generate 100 random turtles > to generate-turtles > create-turtles 100 [ > set xcor random-xcor > set ycor random-ycor > set size 1 + random 5 > ] > end

I have ruled out that only the py extension is a part of this problem. The error for my windows 10 looks like this.

The error

> --------------------------------------------------------------------------- > Exception Traceback (most recent call last) > File NetLogoLink.java:121, in netLogoLink.NetLogoLink.loadModel() > > Exception: Java Exception > > The above exception was the direct cause of the following exception: > > java.lang.RuntimeException Traceback (most recent call last) > File ~\.conda\envs\pynl051-env38\lib\site-packages\pynetlogo\core.py:246, in NetLogoLink.load_model(self, path) > 245 try: > --> 246 self.link.loadModel(path) > 247 except jpype.JException as ex: > > java.lang.RuntimeException: java.lang.RuntimeException > > During handling of the above exception, another exception occurred: > > NetLogoException Traceback (most recent call last) > Cell In[2], line 1 > ----> 1 netlogo.load_model('C:\yiyi\CSVExample.nlogo') > > File ~\.conda\envs\pynl051-env38\lib\site-packages\pynetlogo\core.py:249, in NetLogoLink.load_model(self, path) > 247 except jpype.JException as ex: > 248 print(ex.stacktrace()) > --> 249 raise NetLogoException(str(ex)) > > NetLogoException: java.lang.RuntimeException ![image](https://github.com/quaquel/pyNetLogo/assets/137624461/26ab0467-85d3-481a-9987-55b008a184ed)

My environment is like this ...

python 3.8, netlogo 6.3.0, pynetlogo 0.5.1 & 0.5, jpype1 1.4.1 & 1.4.0, netlogo's py extension 0.5.3 & 0.5.4

all of the version combinations yield similar results (the errors when loading the py extension through pynetlogo)

What can I do next to help?

quaquel commented 1 year ago

Sorry, but I am not sure I fully understand. To be clear: do all extensions fail, or just the py extension?

bussayii commented 1 year ago

Oh, it is just the py extension that raises the error.

quaquel commented 1 year ago

Thanks for the clarification and the provided simple example. Since all other extensions work, this rules out anything with search paths and the loading of the various jars associated with each extension. Unfortunately, the Java InvocationTargetException is not a really clear exception which makes it harder to figure out what is going on. I presently, unfortunately, don't have the time to dig into this more deeply.

I tried to reproduce the error, but I am on an m1 mac and have so far failed to replicate it. I do get a slightly different error (java.lang.ClassCastException: java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')). I'll try once I have time on linux or windows.

bussayii commented 1 year ago

Thanks again for the support :) I am new here and super happy to help. Please take your time.

imantha-das commented 2 months ago

Just curious has anyone managed to find work around for this problem or fixed it? I am also getting the same issue (Netlogo py extension). I am using an M1 Mac.

java.lang.ExceptionInInitializerError
        at org.nlogo.extensions.py.PythonExtension.runOnce(PythonExtension.scala:69)
        at org.nlogo.workspace.ExtensionManager.initializedClassManager(ExtensionManager.scala:198)
quaquel commented 2 months ago

To my knowledge, there has been no further work on this issue. I would need a minimum example to start taking a look myself.

imantha-das commented 1 month ago

Hi Jan Sorry for the late response. But here is a minimum example if you wish to have a look. Thank youKind RegardsImantha On Monday, August 26, 2024 at 03:11:57 PM GMT+8, Jan Kwakkel @.***> wrote:

To my knowledge, there has been no further work on this issue. I would need a minimum example to start taking a look myself.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

Kejian2023 commented 4 weeks ago

I think meet the same problem when I use the py extension.

java.lang.NoClassDefFoundError: Could not initialize class org.nlogo.extensions.py.PythonExtension$ at org.nlogo.extensions.py.PythonExtension.runOnce(PythonExtension.scala:69) at org.nlogo.workspace.ExtensionManager.initializedClassManager(ExtensionManager.scala:198) at org.nlogo.workspace.ExtensionManager.$anonfun$importExtension$8(ExtensionManager.scala:161) at scala.Option.getOrElse(Option.scala:189) at org.nlogo.workspace.ExtensionManager.importExtension(ExtensionManager.scala:159) at org.nlogo.parse.StructureParser$.$anonfun$parsingWithExtensions$1(StructureParser.scala:74) at org.nlogo.parse.StructureParser$.$anonfun$parsingWithExtensions$1$adapted(StructureParser.scala:68) at scala.collection.immutable.List.foreach(List.scala:431) at org.nlogo.parse.StructureParser$.parsingWithExtensions(StructureParser.scala:68) at org.nlogo.parse.StructureParser$.parseSources(StructureParser.scala:33) at org.nlogo.parse.NetLogoParser.basicParse(NetLogoParser.scala:17) at org.nlogo.parse.NetLogoParser.basicParse$(NetLogoParser.scala:15) at org.nlogo.parse.FrontEnd$.basicParse(FrontEnd.scala:10) at org.nlogo.parse.FrontEndMain.frontEnd(FrontEnd.scala:26) at org.nlogo.parse.FrontEndMain.frontEnd$(FrontEnd.scala:25) at org.nlogo.parse.FrontEnd$.frontEnd(FrontEnd.scala:10) at org.nlogo.compile.CompilerMain$.compile(CompilerMain.scala:47) at org.nlogo.compile.Compiler.compileProgram(Compiler.scala:54) at org.nlogo.headless.HeadlessModelOpener.openFromModel(HeadlessModelOpener.scala:51) at org.nlogo.headless.HeadlessWorkspace.openModel(HeadlessWorkspace.scala:535) at org.nlogo.headless.HeadlessWorkspace.open(HeadlessWorkspace.scala:502) at netLogoLink.NetLogoLink.loadModel(NetLogoLink.java:132) Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap') [in thread "main"] at org.nlogo.languagelibrary.config.Config$.getExtensionRuntimeDirectory(Config.scala:13) ... at org.nlogo.extensions.py.PythonExtension$.(PythonExtension.scala:26) at org.nlogo.extensions.py.PythonExtension$.(PythonExtension.scala) ... 22 more

quaquel commented 4 weeks ago

Looks like it indeed. Again, if I have a minimum example, I might be able to help.

imantha-das commented 4 weeks ago

Hi Jan,

I believe I sent you an email with a minimum example before. Didn't you recieve it ?

quaquel commented 4 weeks ago

there was no attachment. Please just attach the file here.

imantha-das commented 4 weeks ago

Hi Jan

Please find the test script attached in this email. Alternatively find them here CRTT/test_pynetlogo at master · imantha-das/CRTT Thanks RegardsImantha

|

CRTT/test_pynetlogo at master · imantha-das/CRTT

Casualty Rescue Transport and Treatment using Agent Based Simulation - imantha-das/CRTT |

|

|

On Tuesday, October 8, 2024 at 01:53:05 PM GMT+8, Jan Kwakkel @.***> wrote:

there was no attachment. Please just attach the file here.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>