导入到python解释器时更新模块 [英] update module on import to python interpreter

查看:183
本文介绍了导入到python解释器时更新模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之

每次我对模块代码进行一些更改时,如何强制python解释器加载模块的最新代码版本?

How to force python interpreter to load the most up-to-date code version of my module everytime I make some changes in the module code?

或者至少通过键入

>>> from myModule import *

进入控制台,而无需在我进行一些更改时重新启动整个python控制台并一次又一次地设置所有内容?这对于调试来说是非常不愉快的行为.

into console, without necessity to restart whole python console and setup everything again and again anytime I make some changes? This is extremely unpleasant behavior for debugging.

---------更长的故事-----------

试图删除.pyc文件,然后再次导入-但它没有任何作用.它甚至不会再创建.pyc文件-因此,如果模块已经加载,我希望它完全忽略我的"import"命令.

I tried to delete the .pyc file, and import it again - but it has no effect. It does't even create .pyc file again - so I expect it completely ignore my "import" command if the module is already loaded.

这也无济于事

>>> mymodule.myfunc()    # the old version
>>> del myModule         # unload mymodle from python conole / interpeter
...                  # now I removed .pyc
...                  # now I make some modifications in mymodule.myfunc() code   
>>> mymodule.myfunc()    # module is unknonwn, ... OK
>>> import myModule      # try to load modified version
>>> mymodule.myfunc()    # stil the old version :(((((, How it can remember?

我也尝试过Spyder,此功能在哪里称为用户模块删除器(UMD)" http://pythonhosted.org/spyder/console .html#reloading-modules-the-user-module-deleter-umd 我以为应该完全做到这一点,但似乎没有(是的,我检查了它是否已打开).

I have tried also Spyder where is this feature called "User Module Deleter (UMD)" http://pythonhosted.org/spyder/console.html#reloading-modules-the-user-module-deleter-umd which I thought should do exactly this, but it seem it doesn't (Yes, I checked that it is turned on).

也许我遗漏了一些东西-有人可以向我解释应该如何使用它吗?

Maybe I'm missing something - can somebody explain me how is it supposed to be used?

导入的模块不在工作目录"中而是在PYTHONPATH中,这是否在某种程度上受到影响?

Is this somehow affected by the fact that the imported module is not in "Working directory" but in PYTHONPATH ?

推荐答案

(此处为Spyder dev)我认为目前您无法直接在控制台中重新加载模块(但我们正在考虑将来进行更改) ).

(Spyder dev here) I think at the moment you are not able to reload a module directly in the console (but we are considering to change this in the future).

关于 UMD 的想法是,它将重新加载模块,但是只有如果您从导入了模块的编辑器中运行文件.如果您想直接在控制台中重新加载它们,则不起作用.

The idea about UMD is that it will reload your modules but only if you run a file from the editor that imports them. It doesn't work if you want to reload them directly in the console.

假设您开发了一个模块,那么您可能正在另一个脚本中使用它(很可能),该脚本将在我们的编辑器中编写并发送到我们的控制台. UMD 是一点魔法,可以在发生这种情况时为您重新加载.

Let's say you developed a module, then you are probably using it in a different script that (most likely) you'll be writing in our editor and send it to run to our console. UMD is a little bit of magic that reloads it for you when that happens.

这篇关于导入到python解释器时更新模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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