我有蟒蛇(如果它的事項pycharm IDE)一般的調試問題Python:如何在調試時保存當前變量?
可以說我有它由2塊代碼:
Code block 1 (takes very long to run)
Code block 2 (where I want to debug)
有沒有一種方法來保存當前變量在運行代碼塊1的末尾,以便我可以「倒帶」回那些變量狀態並從那裏調試,而不必重新運行整個腳本來重新計算這些變量?
我有蟒蛇(如果它的事項pycharm IDE)一般的調試問題Python:如何在調試時保存當前變量?
可以說我有它由2塊代碼:
Code block 1 (takes very long to run)
Code block 2 (where I want to debug)
有沒有一種方法來保存當前變量在運行代碼塊1的末尾,以便我可以「倒帶」回那些變量狀態並從那裏調試,而不必重新運行整個腳本來重新計算這些變量?
您當然可以在運行代碼塊1結束時「保存」當前變量。只需將變量存儲在字典中,並在第一個代碼塊完成後將其寫入文件。
這裏是一個很小的例子,因爲你沒有提供的數據:
import csv
# code block 1
for i in range(1000000): # meant to simulate a "long" time
var1 = 2*i
var2 = 4*i
# basically this is a log that keeps track of the values of these variables
to_save = {'var1': var1, 'var2': var2}
# write that dictionary to a file
with open('my_log.csv', 'w+') as f:
w = csv.DictWriter(f, to_save.keys())
w.writeheader()
w.writerow(to_save)
# continue with code block 2
for i in range(1000):
var1 = "BLAH"
var2 = "BLAH BLAH"