问题描述
目前我正在开发一个包含子模块并使用numpy / scipy的python项目。 Ipython用作交互式控制台。不幸的是我对我现在使用的工作流程不是很满意,我很感激一些建议。
Currently I am working on a python project that contains sub modules and uses numpy/scipy. Ipython is used as interactive console. Unfortunately I am not very happy with workflow that I am using right now, I would appreciate some advice.
在IPython中,框架由一个简单的<$ c $加载c> import 命令。但是,通常需要更改框架的一个子模块中的代码。此时已经加载了一个模型,我使用IPython与它进行交互。
In IPython, the framework is loaded by a simple import
command. However, it is often necessary to change code in one of the submodules of the framework. At this point a model is already loaded and I use IPython to interact with it.
现在,框架包含许多相互依赖的模块,即在最初加载框架时,主模块正在导入和配置子模块。只有在使用 reload(main_mod.sub_mod)
重新加载模块时,才会执行对代码的更改。这很麻烦,因为我需要使用完整路径单独重新加载所有已更改的模块。如果 reload(main_module)
也会重新加载所有子模块,但没有重新加载numpy / scipy,这将非常方便..
Now, the framework contains many modules that depend on each other, i.e. when the framework is initially loaded the main module is importing and configuring the submodules. The changes to the code are only executed if the module is reloaded using reload(main_mod.sub_mod)
. This is cumbersome as I need to reload all changed modules individually using the full path. It would be very convenient if reload(main_module)
would also reload all sub modules, but without reloading numpy/scipy..
推荐答案
IPython附带一些魔术:
IPython comes with some automatic reloading magic:
%load_ext autoreload
%autoreload 2
每次执行新行之前,它都会重新加载所有已更改的模块。它的工作方式与 dreload
略有不同。一些警告适用,键入%autoreload?
以查看可能出现的问题。
It will reload all changed modules every time before executing a new line. The way this works is slightly different than dreload
. Some caveats apply, type %autoreload?
to see what can go wrong.
如果要始终启用此设置,请修改IPython配置文件〜/ .ipython / profile_default / ipython_config.py
[1]并附加:
If you want to always enable this settings, modify your IPython configuration file ~/.ipython/profile_default/ipython_config.py
[1] and appending:
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
通过以下评论获得@Kos的信用。
[1]
如果你没有文件〜/ .ipython / profile_default / ipython_config.py
,你需要首先调用 ipython profile create
。或者文件可能位于 $ IPYTHONDIR
。
[1]If you don't have the file ~/.ipython/profile_default/ipython_config.py
, you need to call ipython profile create
first. Or the file may be located at $IPYTHONDIR
.
这篇关于在IPython中重新加载子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!