cschol cschol - 3 months ago 17
Python Question

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


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.


If Y is a module (and X a package) reload(Y) will be fine -- otherwise, you'll see why good Python style guides (such as my employer's) say to never import anything except a module (this is one out of many great reasons -- yet people still keep importing functions and classes directly, no matter how much I explain that it's not a good idea;-).