Python spy 程序: changing a function in another file takes no effect

标签 python spyder

将spyder 3.2.4与python结合使用。

当我调用 i 函数(在另一个文件中)时,让我从主脚本中调用它 my_func() ,它使用该函数的“旧版本”。澄清:

如果我更改 my_func 并保存该文件,如果从我的主脚本调用,则使用该函数的新版本,但前提是我运行整个主脚本。

如果我只是使用 my_func 运行这些行(使用运行单元或运行选定的行),则使用 my_func 的旧版本(在进行疯狂更改之前)。

我虽然 my_func 必须“重新导入”(from myOtherFile import my_func),所以我也尝试运行这些行,但仍使用旧版本的函数(未更改)。

我是否误解了什么,或者这是一个非常非常严重的错误?如果我关闭spyder并重新启动它,它就可以工作(它使用该功能的新版本),但这是一个 Not Acceptable 解决方案。

最佳答案

此行为与 (I)Python 解释器处理导入的方式有关。

您可以在这里找到详细的解释(以及可能的解决方案):https://support.enthought.com/hc/en-us/articles/204469240-Jupyter-IPython-After-editing-a-module-changes-are-not-effective-without-kernel-restart

简短回答:
- 重新启动内核
- 或使用 importlib.reload ( https://docs.python.org/3/library/importlib.html#importlib.reload )
- 或使用 IPython 的自动重新加载扩展 ( http://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html )

关于Python spy 程序: changing a function in another file takes no effect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673851/

相关文章:

python - Python打印包含汉字的列表

python - 如何在多个 Python 模块中更改变量?

python - pandas:使用 groupby yield SettingWithCopyWarning 的操作

python - Spyder 在启动时崩溃并且重置命令不起作用,因为它找不到 spyderlib.spyder

python - 制作填充图 Matplotlib - 错误 : invalid type promotion

python - 如何在不将事件窗口更改为 IPython 控制台的情况下在 Spyder 中运行代码选择?

python - 如何在 Django 模板中多次迭代同一个字典?

python - 如何从您的 Web 框架将 HTTP 路由动态加载到 NGINX 中?

python - 将 Spyder 设置为默认 Python

python - 在spyder ipython中启用subprocess.run的打印