Topic - python modules
Topic - python modules Topic - python modules
from forum General Support
 forum index   my profile   search 
 new topic  post reply 
moderators: pjr tab
python modules
Joined: 5-December-2007
Posts: 28
Posted: 29-July-2008 20:16
Can anyone give me a hint how to load python modules from another python module which doesn't happen to be above it in the directory structure (ie not a script-relative uri)?

I've seen Peter's example at http://1060.org/forum/search/3450/3, but the absolute unix paths aren't working for me. Also I've seen in the change notes since then that NK now interprets the uris itself, so I'm not sure what to add to the sys.path anymore.

So say from my script ffcpl:/testing/test.py I want to load another python module at at ffcpl:/lib/py/fibo.py


In test.py I've tried all of the following to no avail.
# test.py
import sys
sys.path.append("/")
sys.path.append("lib/py")
sys.path.append("/lib/py")
sys.path.append("ffcpl:/lib/py")
sys.path.append("lib/py/fibo.py")
sys.path.append("/lib/py/fibo.py")
sys.path.append("/home/sharris/Programming/nk-modules/janus/lib/py/fibo.py")
sys.path.append("file:///home/sharris/Programming/nk-modules/janus/lib/py/fibo.py")
sys.path.append("ffcpl:/lib/py/fibo.py")

#import lib.py.fibo # "module lib not found"
import fibo  # "module fibo not found"

Anyone done this?  I'm sure I'm missing something simple.
TIA,
Steve
Path example
Joined: 7-February-2005
Posts: 397
Location: UK
Posted: 31-July-2008 12:16
Hi Steve,

Been a long time since I looked at the python stuff - and I confess I'm not a python user.  However I've uploaded some tests that should show you what you need...

http://1060.org/upload/jython-examples.tar.gz

First thing is that with the Unix heritage languages on NK3 we did not provide a PATH model to them - so Python loads libraries out of the address space as though it were the unix tree and you can't provide an offset (essentially what the PYTHON_PATH does for you).  (Incidentally this is put right in NK4 (coming soon!) - NK4 feels exactly like Unix to Ruby/Python languages and supports the library PATH model of Unix.  Its interesting to speculate how these languages could become more Resource Oriented by allowing imports to specified by URI - then they could get them from anywhere in the the address space - but for now they think that the Unix resource model is always planted at / (one day we'll get there... ;-)).

Anyway this tar.gz contains a directory that was originally located in a module directory /test/jython/  which was mapped into the host modules' address space with <this><match>ffcpl:/test/hython/.*</match><this>

Therefore all the python classes in that directory can be loaded with...

from test.jython import xxxxxx


Please have a play with the examples and let me know if you have any difficulties.

Cheers,

P.
Python Path - 'mounting' local filesystem
Joined: 7-February-2005
Posts: 397
Location: UK
Posted: 31-July-2008 12:26
Steve,

After taking a second to think about your post I see you're trying to use a python library on your local filesystem.  Well as I said above Python on NK loads from the NK address space - it doesn't see the filesystem at all and does everything through the NK context.  But you can easily 'mount' a directory into the address space.

In the mapping section of your module do this...

<rewrite>
  <match>ffcpl:/py/(.*)</match>
  <to>file:///home/sharris/Programming/nk-modules/janus/lib/py/$1</to>
</rewrite>


So now in your python script you can do...

from py.fibo import xxxxxx


Caveat: Remember I don't know Python well so you might need to experiment to get the paths exactly how you need them.

Cheers,

P.

PS You can do this trick for any resources - you can even map active URI's into the address space and treat them as static resources  - if you wanted the pythong library could be in a DB by mapping ffcpl:/whatever -> active:sqlQuery...  Or you could have the library mapped over http! mad - but possible!
 new topic  post reply  To find out about new replies to this post as they occur
please subscribe to one of these feeds:
AtomRSS moderate 
© 2003-2006, 1060 Research Limited. 1060 registered trademark, NetKernel trademark of 1060 Research Limited.