Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update for Python3</strong>: (quoted from the <a href="https://stackoverflow.com/a/437591/3358272">already-answered answer</a>, since the last edit/comment here suggested a deprecated method)</p> <blockquote> <p>In Python 3, <code>reload</code> was moved to the <a href="https://docs.python.org/3.2/library/imp.html" rel="noreferrer"><code>imp</code></a> module. In 3.4, <code>imp</code> was deprecated in favor of <a href="https://docs.python.org/3.4/library/importlib.html" rel="noreferrer"><code>importlib</code></a>, and <a href="https://docs.python.org/3.4/library/importlib.html#importlib.reload" rel="noreferrer"><code>reload</code></a> was added to the latter. When targeting 3 or later, either reference the appropriate module when calling <code>reload</code> or import it.</p> </blockquote> <p>Takeaway:</p> <ul> <li>Python3 >= 3.4: <code>importlib.reload(packagename)</code></li> <li>Python3 &lt; 3.4: <code>imp.reload(packagename)</code></li> <li>Python2: continue below</li> </ul> <hr> <p>Use the <code>reload</code> builtin function:</p> <p><a href="https://docs.python.org/2/library/functions.html#reload" rel="noreferrer">https://docs.python.org/2/library/functions.html#reload</a></p> <blockquote> <p>When <code>reload(module)</code> is executed:</p> <ul> <li>Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time.</li> <li>As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.</li> <li>The names in the module namespace are updated to point to any new or changed objects.</li> <li>Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.</li> </ul> </blockquote> <p>Example:</p> <pre><code># Make a simple function that prints "version 1" shell1$ echo 'def x(): print "version 1"' &gt; mymodule.py # Run the module shell2$ python &gt;&gt;&gt; import mymodule &gt;&gt;&gt; mymodule.x() version 1 # Change mymodule to print "version 2" (without exiting the python REPL) shell2$ echo 'def x(): print "version 2"' &gt; mymodule.py # Back in that same python session &gt;&gt;&gt; reload(mymodule) &lt;module 'mymodule' from 'mymodule.pyc'&gt; &gt;&gt;&gt; mymodule.x() version 2 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload