如何让ipython了解对自写模块所做的更改? [英] How do I make ipython aware of changes made to a selfwritten module?

查看:94
本文介绍了如何让ipython了解对自写模块所做的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试自写模块时,最初几次可能会以错误结束。

When trying a selfwritten module, it is likely to end with errors at the first few times.

但是在修复这些错误时,ipython似乎没有注意到。

But when fixing these errors, ipython does not seem to notice that.

是否有ipython命令重新加载新模块? 清楚并不能解决问题。到目前为止,唯一有效的方法是退出并开始新的会话。但这也意味着重做到目前为止所做的一切。

Is there an ipython command to reload the new module? 'clear' does not do the trick. So far, the only thing that works is to 'exit' and start a new session. But this also means redoing everything I have done so far.

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

Or do I need to add something to the module that it kills all its internal variables after it has been run?

示例:

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 而不是 Cons_NaNs_str ,因此我得到了明显的尝试错误使用字符串加入数据框。

OK, that's easy. I made a typo in line 198, and wrote Cons_NaNs instead of Cons_NaNs_str, and thus I get the obvious error of trying to join a dataframe with a string.

但是在将其修复到 mymodule.py 文件后,我得到了跟随(缩短)错误:

But after fixing it in the mymodule.py file, I get the following (shortened) error:

    197                 Cons_NaNs_str = str(Cons_NaNs_count)
    198                 Cons_NaN_Name_joiner = [current_csv,';',Cons_NaNs_str]
--> 199                 Cons_NaN_Name_str = ''.join(Cons_NaN_Name_joiner)

TypeError: sequence item 2: expected string, DataFrame found

查看回溯,ipython非常清楚源文件中的更改,它显示我修复了错误的 _str ,但它仍然会出错,初看起来似乎是不可能的。运行清除并重新导入所有内容后,它会显示相同的行为。

Looking at the traceback, ipython is well aware of the changes is made in the source file, it shows that I fixed the typo with the missing _str, but it still gives an error, that at the first look seems to be impossible. After running clear and reimporting everything, it shows the same behaviour.

所以只是为了确保我没有在整个过程中的某个地方犯了一个愚蠢的错误,我在ipython中逐步完成了我的整个模块。而导致我这一点的每个变量都表现得如预期。

So just to make sure that I did not make a stupid mistake somewhere along the way, I went trough my whole module step by step in ipython. And every variable that leads me to that point behaves as expected.

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

Cons_NaNs is a dataframe, Cons_NaNs_count is an integer and Cons_NaNs_str is a string.

所以我退出了ipython,重新启动它并重新导入所有内容,现在它可以工作。

So I exited ipython, restarted it and reimported everything and now it works.

但不得不退出ipython糟透了。大多数时候,这意味着必须重新导入数十种内容并执行几十个命令,以达到我可以实际测试我当前正在进行的工作的程度。

But having to exit ipython sucks. Most of the times this means having to reimport dozens of things and doing a few dozen commands, to get to the point where I can actually test what I am currently working on.

推荐答案

这不仅适用于 ipython ,而且 Python 通常会缓存a模块首次导入 sys.modules 时。因此,在第一次导入后,每当您尝试导入它时,您将从 sys.modules 获取缓存的模块对象。

This is not just for ipython , but Python in general caches a module when it is first imported into sys.modules . So after the first import, whenever you try to import it, you would get the cached module object from sys.modules .

要使Python重新加载模块对象而不必重新启动Python,以便反映对模块所做的更改,您应该使用 reload()内置函数(Python 2.x) importlib.reload()(Python 3.x)

To make Python reload the module object without having to restart Python, so that changes done to the module are reflected, you should use reload() built-in function (Python 2.x) or importlib.reload() (Python 3.x).

Python 2.x -

Python 2.x -

<module> = reload(<module>)

示例 -

import module
module = reload(module) #This requires the module as it is, not a string.

Python 3.x -

Python 3.x -

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

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

Similar to Python 2.x example above, just use importlib.reload() instead of reload()

这篇关于如何让ipython了解对自写模块所做的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆