← Back to team overview

yade-dev team mailing list archive

Re: [Yade-users] [Question #234513]: import an external library

 

Hi Bruno, 
Even with your trick :
$ yade-daily
Yade [1]: from nsmp1d_yade import *

It is not working for me and I get the same error :
$ yade-daily
Welcome to Yade 3+3698+47~precise1
TCP python prompt on localhost:9000, auth cookie `syacdu'
XMLRPC info provider on http://localhost:21000
[[ ^L clears screen, ^U kills line. F12 controller, F11 3d view (use h-key for showing help), F10 both, F9 generator, F8 plot. ]]

Yade [1]: from nsmp1d_yade import *
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/usr/bin/yade-daily in <module>()
----> 1 from nsmp1d_yade import *

ImportError: No module named nsmp1d_yade

I also did it with your simplified script Bruno (which consisted in importing a very simple python function and use it),  and I got the same error whatever the way I am trying to import it.

Raphael

(I checked carefully that I am in the right folder and that it is the exact name.)


-----Original Message-----
From: Bruno Chareyre [mailto:bruno.chareyre@xxxxxxxxxxx]
Sent: Fri 8/23/2013 6:47 PM
To: Yade Development Group; Raphaël Maurin
Subject: 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
________________



References