'PyRunner' module is unable to call self-defined function

Asked by Samarian Zhao

I have installed the trunk version of Yade and I met two weird things when I tried to run example cases.

The first thing is that the example scripts cannot run directly if I don add "from yade import wrapper" to the beginning and "wrapper." to relevant instances. Since I installed yade totally according to the installation guide, I can't figure out why the example scripts cannot run without modification.

The second thing is that the self-defined function added to O.engines by "PyRunner" module cannot run propperly. It simply shows "name 'XXXXX' is not define" on screen. It seems OK if the command of PyRunner is a simple sentence such as command="print 'Hello World' ". I don't know why the selfdefined function cannot work here.

Hope you can help find out the reason!

Thank you very much

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Samarian Zhao
Solved:
Last query:
Last reply:
Revision history for this message
Jan Stránský (honzik) said :
#1

Hello,

concerning wrapper problem, how do you run yade scripts?

concerning a PyRunner error, please provide a MWE [1].

What operating system and yade version do you use?

cheers
Jan

[1] https://yade-dem.org/wiki/Howtoask

Revision history for this message
Jérôme Duriez (jduriez) said :
#2

Hello,

Regarding the PyRunner issue, a usual method is to just define command='myPythonFunction()' in the PyRunner engine definition, with myPythonFunction() encompassing the Python code you would like to run, and being defined elsewhere in the script in the classical Python way. Which gives:

O.engines =[...., PyRunner(command='myPythonFunction()')]

def myPythonFunction():
       # your "long" Python code here

Maybe this could be a "reason", a MWE would anyway help.

Jérôme

Revision history for this message
Samarian Zhao (samarian) said :
#3

Really thank everyone for your attention!
After trying different ways, I have found a solution. The reason for my previous error is that I executed the script by typing "run XXX.py" in the yade interface. It seems that the different modules of yade will not run automatically in this way. If I type "yade XXX.py" in the shell, everything works. I do need to spend more time on understanding the architecture of yade.
Thanks honzik and jduriez again!

Revision history for this message
Samarian Zhao (samarian) said :
#4

What'more, I installed the trunk version of yade from source code instead of by typing "sudo apt-get install". And I did't add the blas in the bashrc file. So I did all my tests in the yade interface.

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#5

Hi,
execfile("XXX.py") should be ok.
There are differences between "run" and "execfile" (mainly in namespaces I think) but I can't explain the details.
Bruno