蟒蛇.导入一个函数然后修改它.稍后在同一个python会话中返回错误

Jos*_*ose 2 python

我有两个python脚本,一个有我定义的所有函数(functions.py),另一个只运行那些函数(running_functions.py).我使用将函数导入running_functions脚本from functions import*

我的问题是,当我跑running_functions使用到蟒蛇控制台execfile('running_functions.py')在第一工作就像一个魅力,但如果我不关闭蟒蛇会议,并做了一些修改成一个功能functions.py(例如改变该参数的数量getLabels()需要(从4到5))保存然后我再次运行running_functions.py与相同的命令或当我打电话给getLabels()我得到错误:

用execfile()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "running_functions.py", line 82, in <module>
    predict_labels = getLabels(pred_labels, ids_tr ,labels_tr,filenames_tr, filenames_ts)
TypeError: getLabels() takes exactly 4 arguments (5 given)
Run Code Online (Sandbox Code Playgroud)

调用函数

>>> predict_labels = getLabels(pred_labels, ids_tr ,labels_tr,filenames_tr, filenames_ts)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: getLabels() takes exactly 4 arguments (5 given)
Run Code Online (Sandbox Code Playgroud)

为了让它再次工作,我必须关闭python会话,然后再次运行execfile()或重命名functions.py或使用修改过的函数执行小pythons脚本.

这非常烦人,因为所有代码大约需要10或15分钟,我不喜欢有很多小脚本.那么,我该如何避免这个错误呢?我不想每次会话都关闭,并且不想在每个功能pickle模块中使用.导入函数的方式有误吗?为什么python会返回此错误?对不起这个愚蠢的问题

Tht*_*htu 5

我建议略读python导入的工作方式.一般来说,使用像glob这样的glob导入被认为是不好的做法from module import *.它不透明,难以利用reload.

我建议您重写代码以执行以下操作:

import functions

functions.getLabels(...)
Run Code Online (Sandbox Code Playgroud)

然后在更改getLabels之后,您可以从shell运行以下命令:

reload(functions)
Run Code Online (Sandbox Code Playgroud)

这将重新导入您的更改,而无需重新启动python内核.