I'm sure this is either easy or impossible. :)

I have a Windows command console window open with a Python 2.5 interpreter running. I also have a text editor open on a module I'm debugging. What I'd like to do is make some changes in the editor, save the file, load it into the interpreter window, call a few functions from the module, and repeat until satisfied. But import doesn't reload a module it already has in sys.modules. Is there a "forget" command? Or some other way of loading the module afresh?

Recommended Answers

All 4 Replies

I wonder if you could try a trick like this. I don't have a moment to test it myself. Lets say you've written up a class for kittens and you have it in kittens.py. You could import the module the normal way, import kittens, or you could import it by saying myclass = __import__('kittens.py').

Then you make some changes and instead of having it forget, you just do that thing above again. myclass = __import__('kittens.py')
I'm not sure if you want the .py on those file names or not.

Then you would create a variable of the class by saying
MyKitteh = myclass.Kitten()

Try it out and see if it works for you.

There's a built-in reload function. It takes a module object as it's argument, so you can use reload(sys.modules["kitten"]) or

import kittens
reload(kittens)

It works only for python modules, not for shared libraries.

I wonder if you could try a trick like this. I don't have a moment to test it myself. Lets say you've written up a class for kittens and you have it in kittens.py. You could import the module the normal way, import kittens, or you could import it by saying myclass = __import__('kittens.py').

Then you make some changes and instead of having it forget, you just do that thing above again. myclass = __import__('kittens.py')
I'm not sure if you want the .py on those file names or not.

Then you would create a variable of the class by saying
MyKitteh = myclass.Kitten()

Try it out and see if it works for you.

Actually, I already tried that. There aren't any classes involved, just functions. I did t = __import__('filename'), followed by t.funcName(), and it found the old function, not the new one. I was disappointed.

There's a built-in reload function. It takes a module object as it's argument, so you can use reload(sys.modules["kitten"]) or

import kittens
reload(kittens)

It works only for python modules, not for shared libraries.

Perfect! Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.