Discussion:
[Tutor] Refresh library imported
Steven D'Aprano
2015-08-11 11:38:00 UTC
Permalink
HI there!!
I try to develop some scripts. I use ipython for check if my script work.
When I change the script and try to import again that script I'm not
able to see the modification so I need every time close ipython and
run again and import the script. How can do the refresh of library
without close ipython? thanks so much!
In Python 2:


import mymodule
# make some changes
reload(mymodule)


But be careful that objects attached to the module are *not* updated to
use the new module!

import mymodule
x = mymodule.MyClass()
reload(mymodule)
x.method()

x still uses the old version of the class and method, and will *not* use
the new one. You have to throw it away and start again:

reload(mymodule)
x = mymodule.MyClass()
x.method()

Now you will see the new behaviour.


In Python 3, everything is the same except you have to do:

from imp import reload

first.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-11 11:48:03 UTC
Permalink
HI there!!
I try to develop some scripts. I use ipython for check if my script work.
When I change the script and try to import again that script I'm not able to see the modification so I need every time close ipython and run again and import the script.
How can do the refresh of library without close ipython?
thanks so much!
In your Ipython prompt type
load_ext autoreload

This will get you the autoreloading extension.

Then type
%autoreload?

and read what it says about autoreload, and its limitations.

For what you want, I think you want to type:
autoreload 2
(at least that is what I have, and it works great.)

But this really is something you will want all the time, and so belongs
in your ipython config file.

So, the next question is:
Do you have an ipython config file?

Type at the shell prompt:
ipython profile create

If you didn't have them, it will make them for you, and tell you where it
put it. If it is silent you already have one, probably in your
home directory/.ipython/profile_default/ipython_config.py.

You need to find and edit the ipython_config.py file.
Don't worry about the .ipython/profile_default/ipython_nbconvert_config.py
file.

Look for these lines

# lines of code to run at IPython startup.
# c.InteractiveShellApp.exec_lines = []

change that to
# lines of code to run at IPython startup.
c.InteractiveShellApp.exec_lines = ['%autoreload 2']

Also look for

# A list of dotted module names of IPython extensions to load.
# c.InteractiveShellApp.extensions = []

change this to:
# A list of dotted module names of IPython extensions to load.
c.InteractiveShellApp.extensions = ['autoreload']

If you have an .ipython config file, and these lists aren't empty,
just add '%autoreload 2' and 'autoreload' to whatever already is there.

Hope this helps,
Laura Creighton




_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...