嘗試使用自寫模塊時,最初幾次可能會以錯誤結束。如何讓ipython意識到對自寫模塊所做的更改?
但是當修復這些錯誤時,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
OK,這很容易。我在第198行寫了一個錯字,並且寫了Cons_NaNs
而不是Cons_NaNs_str
,因此我得到了試圖用字符串連接數據框的明顯錯誤。
但它在mymodule.py
文件固定後,我得到以下(縮短)錯誤:
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
的錯字,但它仍然給出了一個錯誤,在第一眼看來似乎是不可能的。運行clear
並重新導入所有內容後,它會顯示相同的行爲。
所以,爲了確保我沒有在某個地方犯下一個愚蠢的錯誤,我在ipython中一步一步地去了整個模塊。而每一個引導我到那個點的變量的行爲都如預期的那樣。
Cons_NaNs
是一個數據幀,Cons_NaNs_count
是一個整數,Cons_NaNs_str
是一個字符串。
所以我退出ipython,重新啓動它並重新導入所有內容,現在它可以正常工作。但是不得不退出ipython。大多數情況下,這意味着必須重新導入數十件事並執行幾十個命令,才能真正測試我目前正在進行的工作。
您是否試過['reload'](https://docs.python.org/2/library/functions.html#reload)? – jonrsharpe