本文介绍了如何在python shell中重新加载一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我导入定义属于某个包的同名类的模块,则由于父包的__init__.py而将其导入为Class而不是Module。有关详细信息,请参阅。在Python shell或ipython shell中,如果我从MyPak导入MyMod



MyModule总是作为Class导入,因此我无法重新加载它(reload()仅适用于模块)。从MyPak导入MyMod



再次运行



似乎没有更新类定义。任何人都可以建议在python shell中更新类的方法吗?



ps。无需重新启动python解释器。



pps 。万一你手头有代码并想测试它:我实际上是在谈论BioPython,我正在研究Bio.PDB.PDBParser。我有一个ipython shell(v0.10)并编辑PDBParser.py。无法在ipython中重新加载它。



所以这就是我所做的:

  #start ipython v0.10 
import Bio
来自Bio.PDB导入PDBParser
p = PDBParser()
s = p.get_structure()
#然后我做出改变,例如只需打印一些文本,在PDBParser.py
del Bio
del PDBParser
del s
import Bio#或reload(Bio)而不删除所有对象
来自Bio .PDB导入PDBParser
p = PDBParser()
s = p.get_structure()#看不到更改后的预期输出:(

我看不到打印文本。这些更改没有以某种方式应用。

解决方案

我终于找到了答案:

 从MyPak导入MyPak 
导入MyMod

编辑 MyPak / MyMod.py 文件后,重新加载班级 MyMod 在文件 MyMod.py 中,需要

  import sys 
del sys.modules ['MyPak.MyMod']
reload(MyPak)
来自MyPak import MyMod

警告


  1. 执行 del MyPak del MyMod del MyPak.My Mod 无法解决问题,因为它只是删除了名称绑定。 Python仅搜索 sys.modules 以查看模块是否已导入。查看帖子。


  2. 重新加载MyPak时,python尝试从MyMod导入MyMod 执行 MyPak / __初始化__。PY 。但是,它在 sys.modules 中找到 MyPak.MyMod ,因此它将 NOT
    重新加载 MyMod 虽然 MyPak / MyMod.py 已更新。你会发现没有生成新的 MyPak / MyMod.pyc



If I import a module defining a class of the same name belonging to a package, it is imported as a Class, not a Module because of the __init__.py of the parent package. See different import results at different directories for details. In Python shell or ipython shell, if I do

from MyPak import MyMod

MyModule is always imported as Class thus I can not reload it (reload() works only for modules). Run

from MyPak import MyMod

again does not seem to update the Class definition. Could anyone suggest a way to update the class in python shell?

ps. without restarting the python interpreter.

pps. Just in case you have the code in hand and want to test it: I am actually talking about BioPython, and I am working on Bio.PDB.PDBParser. I have an ipython shell (v0.10) and edit PDBParser.py. Just got no way to reload it in ipython.

so here is what I did:

# start ipython v0.10
import Bio
from Bio.PDB import PDBParser
p = PDBParser()
s = p.get_structure()
# then I make changes,e.g. simply print some text, in PDBParser.py
del Bio
del PDBParser
del s
import Bio  # or reload(Bio) without deleting all the objects
from Bio.PDB import PDBParser
p = PDBParser()
s = p.get_structure() # expected output after change not seen :(

I could not see the printed text. The changes were not applied somehow.

解决方案

I finally found the answer:

import MyPak
from MyPak import MyMod

after editing MyPak/MyMod.py file, to reload the class MyMod in the file MyMod.py, one needs to

import sys
del sys.modules['MyPak.MyMod']
reload(MyPak)
from MyPak import MyMod

Caveats:

  1. Executing del MyPak or del MyMod or del MyPak.MyMod does not solve the problem since it simply removes the name binding. Python only searches sys.modules to see whether the modules had already been imported. Check out the discussion in the post module name in sys.modules and globals().

  2. When reloading MyPak, python tries to execute the line from MyMod import MyMod in MyPak/__init__.py. However, it finds MyPak.MyMod in sys.modules, thus it will NOTreload MyMod although MyPak/MyMod.py has been updated. And you will find that no new MyPak/MyMod.pyc is generated.

这篇关于如何在python shell中重新加载一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:58