How to automatically reload modules in IPython?

家住魔仙堡 提交于 2019-12-31 21:55:09

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/35037815/how-to-automatically-reload-modules-in-ipython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!