Python でモジュールを完全にリロードする必要があります。「完全に」とは、次の機能が必要ないということです(組み込みのリロード機能のpython 2.6ドキュメントから):
「...モジュールの新しいバージョンが、古いバージョンで定義された名前を定義していない場合、古い定義が残ります...」.
つまり、新しいモジュール バージョンで失われた古い名前を保持したくありません。
これを達成するためのベストプラクティスは何ですか?
ありがとう!
Python でモジュールを完全にリロードする必要があります。「完全に」とは、次の機能が必要ないということです(組み込みのリロード機能のpython 2.6ドキュメントから):
「...モジュールの新しいバージョンが、古いバージョンで定義された名前を定義していない場合、古い定義が残ります...」.
つまり、新しいモジュール バージョンで失われた古い名前を保持したくありません。
これを達成するためのベストプラクティスは何ですか?
ありがとう!
以下を超えてテストしていませんが、モジュールを削除してsys.modules
再インポートすることから始めることができます。
import itertools
itertools.TEST = 7
reload(itertools)
print itertools.TEST
# 7
import sys
del sys.modules['itertools']
import itertools
print itertools.TEST
#Traceback (most recent call last):
# File "/home/jon/stackoverflow/12669546-reload-with-reset.py", line 10, in <module>
# print itertools.TEST
# AttributeError: 'module' object has no attribute 'TEST'
サードパーティ モジュールでテストする
>>> import pandas
>>> import sys
>>> del sys.modules['pandas']
>>> import pandas
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import pandas
File "/usr/local/lib/python2.7/dist-packages/pandas-0.7.3-py2.7-linux-x86_64.egg/pandas/__init__.py", line 10, in <module>
import pandas._tseries as lib
AttributeError: 'module' object has no attribute '_tseries'
>>> to_del = [m for m in sys.modules if m.startswith('pandas.')]
>>> for td in to_del:
del sys.modules[td]
>>> import pandas
>>> # now it works