Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: reload component Y imported with 'from X import Y'?

In Python, once I have imported a module X in an interpreter session using import X, and the module changes on the outside, I can reload the module with reload(X). The changes then become available in my interpreter session.

I am wondering if this also possible when I import a component Y from module X using from X import Y.

The statement reload Y does not work, since Y is not a module itself, but only a component (in this case a class) inside of a module.

Is it possible at all to reload individual components of a module without leaving the interpreter session (or importing the entire module)?

EDIT:

For clarification, the question is about importing a class or function Y from a module X and reloading on a change, not a module Y from a package X.

like image 910
cschol Avatar asked Nov 16 '09 03:11

cschol


People also ask

What is the use reload () in Python?

The reload() - reloads a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have mades changes to the code.

What is from X import Y in Python?

from x import y will only import y from module x . This way, you won't have to use dot-notation. (Bonus: from x import * will refer to the module in the current namespace while also replacing any names that are the same) import x as y will import module x which can be called by referring to it as y.

How do you reload a module in Jupyter notebook?

Simple solution: Use the autoreload to make sure the latest version of the module is used. The autoreloading module is not enabled by default. So you have to load it as an extension. And each time you execute some code, IPython will reimport all the modules to make sure that you are using the latest possible versions.

How do you reload a function from a module in Python?

You can't reload a method from a module but you can load the module again with a new name, say foo2 and say bar = foo2. bar to overwrite the current reference. Note that if bar has any dependencies on other things in foo or any other side effects, you will get into trouble.


1 Answers

Answer

From my tests, the marked answer, which suggests a simple reload(X), does not work.

From what I can tell the correct answer is:

from importlib import reload # python 2.7 does not require this import X reload( X ) from X import Y 

Test

My test was the following (Python 2.6.5 + bpython 0.9.5.2)

X.py:

def Y():     print "Test 1" 

bpython:

>>> from X import Y >>> print Y() Test 1 >>> # Edit X.py to say "Test 2" >>> print Y() Test 1 >>> reload( X )  # doesn't work because X not imported yet Traceback (most recent call last):   File "<input>", line 1, in <module> NameError: name 'X' is not defined >>> import X >>> print Y() Test 1 >>> print X.Y() Test 1 >>> reload( X ) # No effect on previous "from" statements >>> print Y() Test 1 >>> print X.Y() # first one that indicates refresh Test 2 >>> from X import Y >>> print Y() Test 2  >>> # Finally get what we were after 
like image 182
Catskul Avatar answered Sep 19 '22 23:09

Catskul