如何在python shell中重新加载类?
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

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.

#0

On Python 3 only, import the reload function:

>>> from importlib import reload

On both Python 2.x, and 3.x, you can then simply call reload on the module:

>>> import MyPak
>>> reload(MyPak)
>>> from MyPak import MyMod

However, instances of the old class will not be updated (there's simply no code that describes the update mechanism).

#1

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 NOT reload MyMod although MyPak/MyMod.py has been updated. And you will find that no new MyPak/MyMod.pyc is generated.

#2

There are thee ways to solve this:

1. Use import MyPak.MyMod instead of from MyPak import MyMod

Then you can write:

from importlib import reload  # If on Python 3
import MyPak.MyMod
reload(MyPak.MyMod)

and it works.

2. Use IPython.lib.deepreload

from MyPak import MyMod
from IPython.lib.deepreload import reload
reload(MyPak)  # This should also reload all submodules

3. Use autoreload magic

%load_ext autoreload
%autoreload 2
import MyPak.MyMod  # All changes to MyPak.MyMod will be loaded automatically

#3

You can use a magic function:

%load_ext autoreload
%autoreload 2
from MyPak import MyMod

#4

I have one myfile.py file which contains one class MyClass

To import just do:

from myfile import MyClass
mc = MyClass()

To reload:

import sys
del sys.modules['myfile']
from myfile import MyClass
modifiedmc = MyClass()

This is very useful while building modules. one can put these inside a function and just call the function

def myreload():
   import sys
   del sys.modules['myfile']
   from myfile import MyClass
   modifiedmc = MyClass()
   global mc
   mc = modifiedmc

#5

It works for me using python 3.5.2:

import importlib
importlib.reload(class)
from class import module

推荐文章

dropdownlist用于不引人注目的验证需要获取正确的属性

dropdownlist用于不引人注目的验证需要获取正确的属性

推荐文章

iOS应用程序能否将设备切换到静音模式?

iOS应用程序能否将设备切换到静音模式?

推荐文章

小型ref计数缓冲区类中的内存损坏

小型ref计数缓冲区类中的内存损坏

推荐文章

ExpandoObject中的列表

ExpandoObject中的列表

推荐文章

Facebook API-没有电子邮件响应

Facebook API-没有电子邮件响应

推荐文章

HTML/CSS中具有相同行高的多个列

HTML/CSS中具有相同行高的多个列

推荐文章

如何在MVC3.net中实现DotNetOpenAuth作为Oauth提供程序?

如何在MVC3.net中实现DotNetOpenAuth作为Oauth提供程序?

推荐文章

单个片段的垂直操作栏

单个片段的垂直操作栏

推荐文章

如何从sql Server中的.sql文件创建表

如何从sql Server中的.sql文件创建表

推荐文章

PHP垃圾收集澄清

PHP垃圾收集澄清

推荐文章

Haskell准则使用错误

Haskell准则使用错误

推荐文章

如何在一个文件中存储elasticsearch设置+映射(如架构.xml对于Solr)

如何在一个文件中存储elasticsearch设置+映射(如架构.xml对于Solr)

推荐文章

Android-本地HTTPS服务器上的视频流:SSL证书被拒绝

Android-本地HTTPS服务器上的视频流:SSL证书被拒绝

推荐文章

JSF命名为Bean,应用程序范围很广(aka@ManagedBean(Eager=true))

JSF命名为Bean,应用程序范围很广(aka@ManagedBean(Eager=true))

推荐文章

iOS 5 Newsstandkit在哪里存储下载的问题?

iOS 5 Newsstandkit在哪里存储下载的问题?

推荐文章

在HTML电子邮件中,有什么可以替代边距的?

在HTML电子邮件中,有什么可以替代边距的?