How to develop a Python module / package without having to restart the interpreter after each change?

I am developing a Python package using a text editor and IPython. Every time I change any module code, I have to restart the interpreter to check this. This is a pain because the classes I'm developing rely on a context that needs to be restored with every reboot.

I know about the reload() function, but it looks like a frown (also since it was diverted from the built-in Python 3.0) and, moreover, it rarely works, since modules almost always have multiple references.

My question is: what is the best / generally accepted way to develop a Python module / package so that I don't have to feel the pain of constantly restoring my interpreter context?

One idea that I thought of was to use the if __name__ == '__main__': trick if __name__ == '__main__': to run the module directly, so the code is not imported. However, this leaves a bunch of contextual cracks (specific to my installation) at the bottom of my module files.

Ideas?

+6
python module packages
source share
4 answers

Ipython allows reload to see magic function% run iPython doc

or if the modules below it changed the recursive function dreloadd ()

If you have a complex context, is it possible to create it in another module? or assign it to a global variable that will remain without restarting the interpreter

+2
source share

Another approach might be to formalize your test development, but instead of using the interpreter to test your module, save your tests and run them directly.

You probably know about the various ways of doing this with python, I assume that the easiest way to start in this direction is to copy and paste what you are doing in the interpreter into a docstring as a doctrine and add the following: of your module:

 if __name__ == "__main__": import doctest doctest.testmod() 

Your unofficial test will be repeated every time the module is called directly. This has a number of other advantages. See the doctrine documentation for more information on writing doctrines.

+3
source share

How about using nose with nosy to automatically run tests in a separate terminal every time you save your changes to disk? Configure all the state you need in your unit tests.

+1
source share

you can create a python script that sets up your context and runs it with

 python -i context-setup.py -i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception. 
0
source share

All Articles