如何在IPython中自动重新加载模块?

如何在IPython中自动重新加载模块?

问题描述:

在开始之前,我想说我不是程序员;我是一个极客和一个工程师。因此,我喜欢编码并在学术上使用它。 *教会了我80%以上关于python的知识。

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. * taught me more than 80% of what I know about python.

我的问题是我需要先在我的脚本中手动重新加载模块,首先将importlib导入到我的终端中而不是使用 importlib.reload(* modulename *)来重新加载它们。当我通过我的IPython终端运行它时,我希望我的IPython终端自动重新加载我的python脚本中的模块。这个功能在以前的版本中使用magic命令%autoreload 提供,这对我来说似乎不起作用。

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.

我查看了@ IPython文档(链接1),尝试使用%load_ext autoreload 命令(链接2)和 import ipy_autoreload 后跟%autoreload 2 命令(链接3)。我在*中发现了超过4个其他答案,告诉我在链接2或3中执行操作;它对我不起作用。如果有人知道如何恢复自动加载,它会让我的手指更快乐。

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 * 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.

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

链接2: https ://*.com/a/18216967/5762140

链接3: https://*.com/a/4765191/5762140

我使用的是64 Windows 7的位安装。我安装了Anaconda3(3.18.9 64位)附带的IPython 4.0.1。当我尝试使用%load_ext autoreload 时,我可以根据请求提供有关我从IPython终端获取错误回溯的屏幕。

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.

上面的所有链接都使用ipython中的命令。您应该尝试编辑配置文件。打开终端并完成以下步骤。

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.

第1步:确保安装了最新的ipython版本

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

$ ipython --version

第2步:找出配置文件的位置

Step 2: find out where your config file is

$ ipython profile create

步骤3:根据配置文件的位置使用编辑器打开配置文件。我用原子。例如:

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

步骤4:在配置文件中查找以下行:

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

c.InteractiveShellApp.extensions = []

将其更改为:

c.InteractiveShellApp.extensions = ['autoreload']

然后取消注释该行

查找:

c.InteractiveShellApp.exec_lines = []

将其更改为:

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

然后取消注释该行

完成。