1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

reload? bug?

Posted by Michael on Sep 04, 2008

hello - I have been using NodeBox for 2 days now for visualizing some linear algebra. Overall, I love it! However, I am having a big hassle with reloading a module that I am editing. Let's say my module is "a.py". In my main.py that is loaded into NodeBox I have an "import a" statement. If I need to change a.py, the only way I can get the new version of a is by totally exiting NodeBox. I tried "reload(a)" and got an error.

I am also having trouble importing modules that are on my python path, and which I find if I am running python in a terminal window. NodeBox gives me an error.

Can you help?


 
Posted by impiaaa on Sep 04, 2008

NodeBox is weird that way. First, I'd suggest trying the ximport command instead of a normal python import. Also, the python reload function only works after the module has been imported in a script.
For your second question, NodeBox has it's own import path inside the program bundle, so any python modules that you might want to import, you have to move into NB's App Support folder, located in either ~/Library/Application Support/NodeBox, or /Library/Application Support/NodeBox. I know it's a hassle, i have to put up with it too.



Posted by Cedric on Sep 04, 2008

I agree with the hassle. I am starting to use NodeBox quite often for my work, and my scripts get bigger and bigger. Hence I write libraries that are imported in the main script written in NodeBox. This quit and reopen dance is a bit annoying! Same or the PYTHONPATH. Would be cool to have NodeBox understand the current value, but when I try to insert it with sys.path.insert(0, PATH), it doesn't work.



Posted by Tom De Smedt on Sep 04, 2008

Hi all,

First, the only difference between import() and NodeBox's own ximport() is that ximport() makes the canvas / drawing context (_ctx) available in the imported module. This is essential if you're writing modules that have drawing functions (e.g. functions containing stuff like _ctx.beginpath(10, 10)).

reload() should work fine as long as 1) you first import the module, 2) it is located in the same folder as your main script or centrally in Library/Application Support/NodeBox.

import module
reload(module)
Like you say, sys.path.insert() or PYTHONPATH are not doing anything. Funny, I remember this used to work. We'll have a look at it.

Note that for linear algebra stuff, we've bundled numpy with NodeBox (simply do "import numpy").



Posted by Michael on Sep 08, 2008

Thanks for the quick responses. ( just found them by accident).
I did try the reload() after the import

if I do:
import foo
reload(foo) - reload does work. However class bar defined in foo.py is now undefined

what I had was:
from foo import *
in this context reload does NOT work, but bar is defined

OK - for fellow sufferers: here is what _does_ work:

import foo
reload(foo)
from foo import*

WRT the import statement - I cannot stress enough how wonderful it would be if NodeBox respected the PYTHONPATH. Yes, I can put my modules in the NodeBox directory as suggested, but that would really be a hassle as I work concurrently on the modules and my applications.

WRT to numpy, does this mean that I am not necessarily using the latest, which I just installed?



Posted by fdb on Sep 10, 2008

As a temporary fix, add the standard python paths to your sys path:

import sys
sys.path.append('/Library/Python/2.5/site-packages')
That should do the trick on Leopard. For Tiger, change 2.5 to 2.3.

Kind regards,

Frederik



Posted by michael on Sep 13, 2008

Thanks - that 'sorta' works. I am going to write a little routine to only add path once, since 'append' keeps adding every time I run my scripts.

I also notice that NodeBox seems to replace/override sys.path. Couldn't it simply append to it? Apologies for a dumb question from a relative neophyte.

best - Michael



Posted by michael on Sep 13, 2008

Here is a hack that works. I created a file 'NBrequires.py' that I include first in my nNodeBox scripts. You will need to change path(s) and requires. In this case I only needed simplejson.

best - Michael

#NBrequires.py
## following are a workaround to import other packages, since NodeBox does not include PYTHONPATH

import sys
def loadOnce(path):
if path in sys.path:
pass
else:
sys.path.append(path)

loadOnce('/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages')

# if you have eggs in path (from easy_install) you can add following:

from pkg_resources import require
require("simplejson")
from simplejson import*



Posted by michael on Sep 13, 2008

My last post on this I promise :)

I have found that you must import NBrequires in every module that you import that requires whatever. This surprised me.

In my case, my main.py needs simplejson and it imports foo.py that also need simplejson. foo.py needs my hack even though I ran the hack in main.py before the 'import foo' statement.