Load python module from within YADE

Asked by Hotei

Hello. I have a file anyModule.py which I import with the line "import anyModule" from my YADE script bouncingSphereForum.py, but the following error appears: "ImportError: No module named anyModule". If I place the code of the module inside the YADE script it gives no error. Could anyone tell me how to import python modules from within YADE? Thank you very much. The scripts are the following:

YADE script:

import numpy as np
import anyModule

# basic simulation showing sphere falling ball gravity,
# bouncing against another sphere representing the support
# DATA COMPONENTS
# add 2 particles to the simulation
# they the default material (utils.defaultMat)
O.bodies.append([
# fixed: particle's position in space will not change (support)
utils.sphere(center=(0,0,0),radius=.5,fixed=True),
# this particles is free, subject to dynamics
utils.sphere((0,0,2),.5)
])

# Prints in file the center coordinates of the bouncing sphere
#def printFile():
    #h = [O.bodies[1].state.pos[2]]
    #np.savetxt("height" + str(O.iter) + ".txt", h)

# FUNCTIONAL COMPONENTS
# simulation loop -- see presentation for the explanation
O.engines=[
ForceResetter(),
InsertionSortCollider([Bo1_Sphere_Aabb()]),
InteractionLoop(
[Ig2_Sphere_Sphere_L3Geom()],
 # collision geometry
[Ip2_FrictMat_FrictMat_FrictPhys()], # collision "physics"
[Law2_L3Geom_FrictPhys_ElPerfPl()]
 # contact law -- apply forces
),

# damping: numerical dissipation of energy
NewtonIntegrator(damping=0.1, gravity=(0,0,-9.81)),
PyRunner(command = "anyModule.printFile()", iterPeriod = 500000)
]
# set timestep to a fraction of the critical timestep
# the fraction is very small, so that the simulation is not too fast
# and the motion can be observed
O.dt=.5e-4*utils.PWaveTimeStep()
# save the simulation, so that it can be reloaded later, for experimentation
O.saveTmp()

#print "it was here"

O.run(2000000)

*****************************************************************

Python module:

# Prints in file the center coordinates of the bouncing sphere
def printFile():
    #h = [O.bodies[1].state.pos[2]]
    #np.savetxt("height" + str(O.iter) + ".txt", h)

Question information

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

Hello,

importing in Yade is the same as importing to normal Python [1,2]

 "ImportError: No module named anyModule".

it means, that Python could not find the file anyModule.py. See [2] for
some details. You have several options:

1) The easiest way, have anyModule.py in the same directory as your main
script (bouncingSphereForum.py)

If you have the module in another place (to be used by more scripts for
instance), it is convenient to place it to some "standard" place (e.g.
myModules directory in your home directory), than you have 2 other options

2) using sys.path
#######
import sys
sys.path.append("/home/myname/path/of/the/directory/where/anyModule/is/saved")
import anyModule
#######

3) using PYTHONPATH, see [2].

cheers
Jan

[1] https://docs.python.org/2.7/tutorial/modules.html
[2] https://docs.python.org/2.7/tutorial/modules.html#the-module-search-path

Revision history for this message
Hotei (hotei-mat) said :
#2

Dear Jan: the second option using sys.path solved my problem. I posted my question because option 1 hadn't worked for me. Thank you very much!

Revision history for this message
Hotei (hotei-mat) said :
#3

Thanks Jan Stránský, that solved my question.