yade-dev team mailing list archive
-
yade-dev team
-
Mailing list archive
-
Message #09817
Re: [Yade-users] [Question #234513]: import an external library
> Jan Stránský proposed the following answer:
> or inside the script (in the beginning)
>
> import sys
> sys.path.append('.')
>
> the solution is the same as Anton's one
> Jan
That's it (conclusion for Raphaël: ask the mailing list instead of me ;) )
In fact Yade is a bit inconsistent, because this is ok:
$ yade-daily
Yade [1]: from mylib import *
Depending if the import is done in a script or typed within the session,
it will work or not.
This context switch is not really intuitive. One would expect that
typing in script or terminal is exactly the same.
It is not the first time I have trouble with that.
Could you guys try if any of the attached patches are working for you (I
only tested with IPython 0.10)?
The first one is trying to make the behavior the same for shell commands
and script by moving arguments handling after shell config.
The second is simply adding "sys.path.append('.')" in the startup
scripts, it is less general.
Naive tries, let me know if it works and what you think.
Bruno
>
> 2013/8/23 Anton Gladky <question234513@xxxxxxxxxxxxxxxxxxxxx>
>
>> Question #234513 on Yade changed:
>> https://answers.launchpad.net/yade/+question/234513
>>
>> Status: Open => Answered
>>
>> Anton Gladky proposed the following answer:
>> PYTHONPATH=. yade-daily Simple.py
>>
>> should fix your problem, if nsmp1d_yade.so is in the current folder.
>>
>> Anton
>>
>>
>> 2013/8/23 Raphaël Maurin <question234513@xxxxxxxxxxxxxxxxxxxxx>:
>>> New question #234513 on Yade:
>>> https://answers.launchpad.net/yade/+question/234513
>>>
>>> Hi all,
>>>
>>> I am trying to import an external library nsmp1d_yade.so in my script
>> (see below) and I don't succeed to do it.
>>> When I am using my script with python, doing :
>>> python Simple.py
>>> everything is working and the script is using the content of the library.
>>> However, when I am running the same script with yade :
>>> yade-daily Simple.py
>>>
>>> I get an error :
>>> Welcome to Yade 3+3698+47~precise1
>>> TCP python prompt on localhost:9000, auth cookie `ucdsea'
>>> XMLRPC info provider on http://localhost:21000
>>> Running script Simple.py
>>> Traceback (most recent call last):
>>> File "/usr/bin/yade-daily", line 168, in runScript
>>> execfile(script,globals())
>>> File "Simple.py", line 2, in <module>
>>> from nsmp1d_yade import *
>>> ImportError: No module named nsmp1d_yade
>>>
>>>
>>> I don't understand why it is working with python and not with Yade.
>>> Do you have any idea of how I can solve this problem?
>>>
>>> Thank you
>>>
>>> Raphael
>>>
>>>
>>>
>>> Here is my script Simple.py :
>>>
>>> from nsmp1d_yade import *
>>>
>>> from pylab import *
>>> from matplotlib import pyplot
>>> import matplotlib.gridspec as gridspec
>>> import numpy as np
>>>
>>> ## Define the situation
>>> rho_f = 1000
>>> rho_s = 2500
>>> slope = 1e-4
>>> visco_f = 1e-3
>>> dp=6e-3
>>> ndimz=301
>>> h=1.
>>> dsig=np.zeros(ndimz)
>>> dsig[0:ndimz-1]=h/(double(ndimz-1))
>>> sig=linspace(0e0,1e0,ndimz)
>>>
>>> ufnp = np.zeros(ndimz)
>>> ufn = np.zeros(ndimz)
>>> alphaf = np.zeros(ndimz)
>>> us = np.zeros(ndimz)
>>> alphas = np.zeros(ndimz)
>>> alphaf=1e0-alphas
>>> tfin=2000.
>>> dt=1e-1
>>>
>>> ufnp=nsmp1d_yade(h,sig,dsig,dp,ufn,alphaf,us,alphas,tfin,dt)
>>>
>>> ##
>>> ## plots
>>> ##
>>>
>>> zmin=0
>>> zmax=h
>>>
>>> umin=min(ufnp)
>>> umax=max(ufnp)
>>>
>>> figure(1)
>>>
>>> plot(ufnp,sig*h,'ob',label='u_f')
>>> title('U_f')
>>> #axis([1200, 1900, zmin, zmax])
>>> xlabel('U_f (m/s)')
>>> ylabel('z (m)')
>>> axis([umin, umax, zmin, zmax])
>>>
>>> show()
>>>
>>> --
>>> You received this question notification because you are a member of
>>> yade-users, which is an answer contact for Yade.
>>>
>>> _______________________________________________
>>> Mailing list: https://launchpad.net/~yade-users
>>> Post to : yade-users@xxxxxxxxxxxxxxxxxxx
>>> Unsubscribe : https://launchpad.net/~yade-users
>>> More help : https://help.launchpad.net/ListHelp
>> --
>> You received this question notification because you are a member of
>> yade-users, which is an answer contact for Yade.
>>
>> _______________________________________________
>> Mailing list: https://launchpad.net/~yade-users
>> Post to : yade-users@xxxxxxxxxxxxxxxxxxx
>> Unsubscribe : https://launchpad.net/~yade-users
>> More help : https://help.launchpad.net/ListHelp
>>
--
_______________
Bruno Chareyre
Associate Professor
ENSE³ - Grenoble INP
Lab. 3SR
BP 53
38041 Grenoble cedex 9
Tél : +33 4 56 52 86 21
Fax : +33 4 76 82 70 43
________________
--- ./trunk/core/main/main.py.in 2013-08-23 18:34:48.198523639 +0200
+++ ./trunk/core/main/main2.py.in 2013-08-23 18:35:08.124100832 +0200
@@ -155,25 +155,26 @@
#if qt4 and yade.runtime.ipython_version==11:
# import IPython
# IPython.appstart_qt4(qapp)
- if len(sys.argv)>0:
- arg0=sys.argv[0]
- if qt4: yade.qt.Controller();
- if sum(bool(arg0.endswith(ext)) for ext in ('.xml','.xml.bz2','.xml.gz','.yade','.yade.gz','.yade.bz2','.bin','.bin.gz','.bin.bz2'))>0:
- if len(sys.argv)>1: raise RuntimeError('Extra arguments to saved simulation to run: '+' '.join(sys.argv[1:]))
- sys.stderr.write("Running simulation "+arg0+'\n')
- if arg0.endswith('.py'):
- def runScript(script):
- sys.stderr.write("Running script "+arg0+'\n')
- try:
- execfile(script,globals())
- except SystemExit: raise
- except: # all other exceptions
- import traceback
- traceback.print_exc()
- if yade.runtime.opts.exitAfter: sys.exit(1)
- if yade.runtime.opts.exitAfter: sys.exit(0)
- runScript(arg0)
- if yade.runtime.opts.exitAfter: sys.exit(0)
+ def handleArgs():
+ if len(sys.argv)>0:
+ arg0=sys.argv[0]
+ if qt4: yade.qt.Controller();
+ if sum(bool(arg0.endswith(ext)) for ext in ('.xml','.xml.bz2','.xml.gz','.yade','.yade.gz','.yade.bz2','.bin','.bin.gz','.bin.bz2'))>0:
+ if len(sys.argv)>1: raise RuntimeError('Extra arguments to saved simulation to run: '+' '.join(sys.argv[1:]))
+ sys.stderr.write("Running simulation "+arg0+'\n')
+ if arg0.endswith('.py'):
+ def runScript(script):
+ sys.stderr.write("Running script "+arg0+'\n')
+ try:
+ execfile(script,globals())
+ except SystemExit: raise
+ except: # all other exceptions
+ import traceback
+ traceback.print_exc()
+ if yade.runtime.opts.exitAfter: sys.exit(1)
+ if yade.runtime.opts.exitAfter: sys.exit(0)
+ runScript(arg0)
+ if yade.runtime.opts.exitAfter: sys.exit(0)
# common ipython configuration
banner='[[ ^L clears screen, ^U kills line. '+', '.join((['F12 controller','F11 3d view (use h-key for showing help)','F10 both','F9 generator'] if (qt4) else [])+['F8 plot'])+'. ]]'
ipconfig=dict( # ipython options, see e.g. http://www.cv.nrao.edu/~rreid/casa/tips/ipy_user_conf.py
@@ -197,6 +198,7 @@
if yade.runtime.ipython_version==10:
from IPython.Shell import IPShellEmbed
ipshell=IPShellEmbed(banner=banner,rc_override=ipconfig)
+ handleArgs()
ipshell()
# save history -- a workaround for atexit handlers not being run (why?)
# http://lists.ipython.scipy.org/pipermail/ipython-user/2008-September/005839.html
@@ -208,6 +210,7 @@
for k in ipconfig: setattr(InteractiveShellEmbed,k,ipconfig[k])
InteractiveShellEmbed.banner1=banner+'\n' # called banner1 here, not banner anymore
ipshell=InteractiveShellEmbed()
+ handleArgs()
ipshell()
elif yade.runtime.ipython_version>=12:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
@@ -222,6 +225,7 @@
InteractiveShellEmbed.config=cfg
InteractiveShellEmbed.banner1=banner+'\n'
ipshell=InteractiveShellEmbed()
+ handleArgs()
ipshell()
## run userSession in a way corresponding to the features we use:
--- ./trunk/core/main/main.py.in 2013-08-23 18:34:48.198523639 +0200
+++ ./trunk/core/main/main3.py.in 2013-08-23 18:36:52.714100946 +0200
@@ -172,6 +172,7 @@
traceback.print_exc()
if yade.runtime.opts.exitAfter: sys.exit(1)
if yade.runtime.opts.exitAfter: sys.exit(0)
+ sys.path.append('.')
runScript(arg0)
if yade.runtime.opts.exitAfter: sys.exit(0)
# common ipython configuration
Follow ups