Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically reload modules in IPython?

Tags:

Before I begin, I want to say that I am not a programmer; I am a geek and an engineer. Thus, I love coding and use it academically. Stackoverflow taught me more than 80% of what I know about python.

My problem is I need to manually reload the modules in my scripts by first importing importlib into my terminal and than using importlib.reload(*modulename*) to reload them. I want my IPython terminal to automatically reload the modules in my python scripts when I run them through my IPython terminal. This functionally was provided in previous version using the magic command %autoreload, which does not seem to work for me.

I have looked @ the IPython documentation (link 1), tried using the %load_ext autoreload command (link 2) and the import ipy_autoreload followed by %autoreload 2 command (link 3). I found more than 4 other answers in stackoverflow telling me to do the things in either link 2 or 3; it didn't work for me. If anyone knows how to bring back autoreloading, it would make my fingers a bit happier.

Link 1: https://ipython.org/ipython-doc/3/config/extensions/autoreload.html

Link 2: https://stackoverflow.com/a/18216967/5762140

Link 3: https://stackoverflow.com/a/4765191/5762140

I am using a 64 bit installation of Windows 7. I have IPython 4.0.1 which came with my installation of Anaconda3 (3.18.9 64bit). Screenies about my error traceback from the IPython terminal when i try to use %load_ext autoreload can be provided on request.

like image 426
Whynot Ogle Avatar asked Jan 27 '16 12:01

Whynot Ogle


People also ask

How do I reload a Python module?

The reload() is used to reload a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have made changes to the code.

What is auto reload in Python?

Autoreload is a simple python script to watch a directory for changed files and restarts a process when the change is detected.

What does Autoreload 2 do?

autoreload reloads modules automatically before entering the execution of code typed at the IPython prompt. The module was reloaded without reloading it explicitly, and the object imported with from foo import ... was also updated.


1 Answers

All the links you have above use commands within ipython. You should try editing your config file. Open up your terminal and complete the following steps.

Step 1: Make sure you have the latest ipython version installed

$ ipython --version 

Step 2: find out where your config file is

$ ipython profile create 

Step 3: Open the config file with an editor based on the location of your config file. I use atom. For example:

$ atom ~/.ipython/profile_default/ipython_config.py 

Step 4: Look for the following lines in the config file:

c.InteractiveShellApp.extensions = [] 

change it to:

c.InteractiveShellApp.extensions = ['autoreload'] 

and then uncomment that line

find:

c.InteractiveShellApp.exec_lines = [] 

change it to:

c.InteractiveShellApp.exec_lines = ['%autoreload 2'] 

and then uncomment that line

Done.

like image 184
Jomonsugi Avatar answered Oct 09 '22 16:10

Jomonsugi