Module Dev: Reloading a module?

I’m trying to write several modules for Blender which make use of parts of each other, using an external IDE. (They are separated, so students can easily tinker with them individually)

During development, I noticed that Blender only recognized any changes I made to a module after closing and reopening the application. So I tried to write a short script to unload the module on request, thinking that Blender would reload it when it would be needed the next time.

This appears to be the case. However, when my module is loaded again, it gives an Attribute Error when I try to access other Blender modules (Draw, for example) within it.

Here’s the code I use to unload modules:


from sys import modules
del modules['myModule']

Would anyone share their best practises when it comes to module development? I can write single scripts and load and reload them in Blender just fine, but when it comes to several distinct files that potentially need to import each other, there has to be a trick I have missed. Right?

reload(myModule)

Thanks a lot for the quick reply!

I still appear to have a bit of a problem, though. The following test case illustrates it:
I have myModule.py open in my external IDE. myModule defines a class “myClass”. In the class’ constructor, there is a simple print statement (print “test”).

I have a text window open in Blender, containing a script which imports myModule and creates an instance of myClass, thereby causing the console window to output “test”.

I change this print statement in “myModule” to output “this module has been updated”. I change the code in my text window to


from sys import modules
reload ( modules['myModule'] ) 
from myModule import *
v = myClass()

However, the output of myClass’ constructor is still “test”. This is the same, whether I put the reload statement before or after “from myModule import *”

is your module that you’re trying to reload loaded into blender as a text file?

No. It’s located in the same path as the other python scripts. [edit: by that I mean, the scripts that come with Blender]

Hmm. Well I tried it with the following code and it worked for me:

import sys
sys.path.append(‘C:/Documents and Settings/jgilbert/Desktop’)

if sys.modules[‘tester_module’]:
reload(sys.modules[‘tester_module’])
‘print module reloaded’

import tester_module

a = tester_module.myClass()

I change this print statement in “myModule” to output “this module has been updated”

Did you remember to save it from your IDE?

I’ve tried it again, using ascotan’s code sample, and it worked well. I don’t know what went wrong yesterday, but apparently, the reload statement needs to go before the import statement after all.

Thanks very much for your help! This will pretty much double my productivity. :slight_smile: