python重新加载文件_如何在python shell中重新加载一个类?

1586010002-jmsa.png

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:

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().

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 NOT

reload MyMod although MyPak/MyMod.py has been updated. And you will find that no new MyPak/MyMod.pyc is generated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值