如何让ipython知道对自写模块所做的更改?

2024-10-01 15:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

在尝试自写模块时,它可能在最初几次出现错误。在

但在修正这些错误时,ipython似乎没有注意到这一点。在

是否有ipython命令来重新加载新模块?'“清除”不能起作用。到目前为止,唯一有效的方法就是“退出”并开始新的会话。但这也意味着要重做我迄今为止所做的一切。在

或者我需要在模块中添加一些东西,让它在运行后杀死所有的内部变量?在

示例:

from mymodule import readCSVts
import pandas as pd
data = readCSVts('file.csv')

TypeError                                 Traceback (most recent call last)
<ipython-input-158-8f82f1a78260> in <module>()
----> 1 data = readCSVts('file.csv')

/home/me/path/to/mymodule.py in readCSVts(filename)
    194                 Cons_NaNs=hydroTS[(hydroTS.level.isnull())&(hydroTS.level.shift().isnull())&(hydroTS.level.shift(periods=2).isnull())]
    195                 #This is a pandas dataframe containing all rows with NaN
    196                 Cons_NaNs_count = len(Cons_NaNs)
    197                 Cons_NaNs_str = str(Cons_NaNs_count)
    198                 Cons_NaN_Name_joiner = [current_csv,';',Cons_NaNs]
--> 199                 Cons_NaN_Name_str = ''.join(Cons_NaN_Name_joiner)

TypeError: sequence item 2: expected string, DataFrame found

好吧,很简单。我在第198行打错了字,写了Cons_NaNs,而不是{},因此我得到了一个明显的错误,即试图用字符串连接数据帧。在

但在mymodule.py文件中修复后,我得到了以下(缩短)错误:

^{pr2}$

查看回溯,ipython很清楚源文件中的更改,它显示我修复了丢失的_str的打字错误,但它仍然给出了一个错误,在第一眼看上去似乎是不可能的。在运行clear并重新导入所有内容之后,它显示了相同的行为。在

所以为了确保我没有犯一个愚蠢的错误,我在ipython中一步一步地复习了我的整个模块。每一个引导我到达这一点的变量都会按照预期的方式运行。在

Cons_NaNs是一个数据帧,Cons_NaNs_count是一个整数,Cons_NaNs_str是一个字符串。在

所以我退出了ipython,重新启动并重新导入了所有的东西,现在它开始工作了。在

但必须离开伊普顿真是糟透了。大多数时候,这意味着必须重新导入几十个东西和执行几十个命令,才能真正测试我当前正在进行的工作。在


Tags: 模块csvnamecount错误ipythonnanlevel
3条回答

这不仅适用于ipython,而且一般情况下,Python在第一次导入sys.modules时会缓存该模块。因此,在第一次导入之后,无论何时尝试导入它,都将从sys.modules获取缓存的模块对象。在

要使Python重新加载模块对象而不必重新启动Python,以便反映对模块所做的更改,应该使用^{} built-in function (Python 2.x)^{} (Python 3.x)。在

Python 2.x-

<module> = reload(<module>)

示例-

^{pr2}$

Python 3.x-

import importlib
<module> = importlib.reload(<module>)

与上面的Python 2.x示例类似,只需使用importlib.reload()而不是{}

当您关闭IPython笔记本服务器并重新启动它时,您将拥有一个新的内核实例,它不会与您的笔记本本身保持一致。 您应该在打开笔记本后立即运行所有单元格来启动工作流。在顶部菜单中,选择“单元格->全部运行”

Ipython有一种特定的方式,可以使用set up autoreload

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: from foo import some_function

In [4]: some_function()
Out[4]: 42

In [5]: # open foo.py in an editor and change some_function to return 43

In [6]: some_function()
Out[6]: 43

模块在没有显式重新加载的情况下被重新加载,并且使用from foo import导入的对象。。。也更新了。

用法 提供了以下魔术命令:

%autoreload

Reload all modules (except those excluded by %aimport) automatically now.

%autoreload 0

Disable automatic reloading.

%autoreload 1

Reload all modules imported with %aimport every time before executing the >Python code typed.

%autoreload 2

Reload all modules (except those excluded by %aimport) every time before executing the Python code typed.

%aimport

List modules which are to be automatically imported or not to be imported.

%aimport foo

Import module ‘foo’ and mark it to be autoreloaded for %autoreload 1

%aimport -foo

Mark module ‘foo’ to not be autoreloaded.

还有一个dreload,它适用于python2和python2。在

^{pr2}$

相关问题 更多 >

    热门问题