<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
					xmlns:content="http://purl.org/rss/1.0/modules/content/"
					xmlns:wfw="http://wellformedweb.org/CommentAPI/"
					xmlns:dc="http://purl.org/dc/elements/1.1/"
				  >
<channel>
<title>NEVERFEAR (Post Comments)</title>
<link>http://neverfear.org</link>
<description><![CDATA[NEVERFEAR.org weblog feed for post comments]]></description>
<image><title>NEVERFEAR (Post Comments)</title>
<link>http://neverfear.org</link>
<url>http://neverfear.org/favicon.ico</url>
</image>
<language>en-uk</language>
<pubDate>Tue, 14 Apr 2026 14:42:57 +0000</pubDate>
<item>
<title>Comment on Dynamically importing Python modules by filename</title>
<link>http://neverfear.org/blog/view/37/Dynamically_importing_Python_modules_by_filename/comments#comment_166</link>
<pubDate>Wed, 28 Jul 2010 17:48:50 +0000</pubDate>
<description><![CDATA[<p>It would depend greatly on what you had in your module. If your module had no code ran by default that changes global state, then it should be safe just to re-execfile on the module object.</p>

<p>However if you're removing variable/class/function/etc names from your module then this wont be reflected.</p>

<p>Frankly, the best way is just to call <code>CreateNewModule</code> again. Otherwise you have to worry about just what exact changes you're reloading and to what.</p>

<p>Interestingly you can load multiple module files into the same virtual module object by executing <code>execfile(filename, module.__dict__, module.__dict__)</code> multiple times.</p>

<p>Let's say you had a file called <code>ModuleA.py</code>:</p>

<pre class="python" style="font-family:monospace;"><span class="co1"># Filename: ModuleA.py</span>
<span class="kw1">def</span> FuncA<span class="br0">&#40;</span><span class="br0">&#41;</span>:
    <span class="kw1">print</span> <span class="st0">&quot;I am function A&quot;</span></pre>

<p>And a file called <code>ModuleB.py</code>:</p>

<pre class="python" style="font-family:monospace;"><span class="co1"># Filename: ModuleB.py</span>
<span class="kw1">def</span> FuncB<span class="br0">&#40;</span><span class="br0">&#41;</span>:
    <span class="kw1">print</span> <span class="st0">&quot;I am function B&quot;</span></pre>

<p>You could modify <code>CreateNewModule</code> to take in multiple filenames like so:</p>

<pre class="python" style="font-family:monospace;"><span class="kw1">def</span> CreateNewModuleFromList<span class="br0">&#40;</span>name, filenames<span class="br0">&#41;</span>:
    module = <span class="kw3">types</span>.<span class="me1">ModuleType</span><span class="br0">&#40;</span>name<span class="br0">&#41;</span>
    <span class="kw1">for</span> filename <span class="kw1">in</span> filenames:
        <span class="kw2">execfile</span><span class="br0">&#40;</span>filename, module.<span class="kw4">__dict__</span>, module.<span class="kw4">__dict__</span><span class="br0">&#41;</span>
    <span class="kw1">return</span> module</pre>

<p>Example usage:</p>

<pre class="python" style="font-family:monospace;"><span class="sy0">&gt;&gt;&gt;</span> m = CreateNewModuleFromList<span class="br0">&#40;</span><span class="st0">&quot;MULTIMODULE&quot;</span>, <span class="br0">&#91;</span><span class="st0">&quot;ModuleA.py&quot;</span>, <span class="st0">&quot;ModuleB.py&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>
<span class="sy0">&gt;&gt;&gt;</span> m
<span class="sy0">&lt;</span>module <span class="st0">'MULTIMODULE'</span> <span class="br0">&#40;</span>built-<span class="kw1">in</span><span class="br0">&#41;</span><span class="sy0">&gt;</span>
<span class="sy0">&gt;&gt;&gt;</span> <span class="kw2">dir</span><span class="br0">&#40;</span>m<span class="br0">&#41;</span>
<span class="br0">&#91;</span><span class="st0">'FuncA'</span>, <span class="st0">'FuncB'</span>, <span class="st0">'__builtins__'</span>, <span class="st0">'__doc__'</span>, <span class="st0">'__name__'</span><span class="br0">&#93;</span>
<span class="sy0">&gt;&gt;&gt;</span> m.<span class="me1">FuncA</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
I am function A
<span class="sy0">&gt;&gt;&gt;</span> m.<span class="me1">FuncB</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
I am function B
<span class="sy0">&gt;&gt;&gt;</span></pre>

<p>Note: If ModuleB.py contained something called <code>FuncA</code> then that would overwrite the previous implementation of <code>FuncA</code> that was defined in ModuleA.py.</p>
]]></description>
<author>doug@neverfear.org</author>
<guid isPermaLink="true" >http://neverfear.org/blog/view/37/Dynamically_importing_Python_modules_by_filename/comments#comment_166</guid>
</item>
<item>
<title>Comment on Dynamically importing Python modules by filename</title>
<link>http://neverfear.org/blog/view/37/Dynamically_importing_Python_modules_by_filename/comments#comment_164</link>
<pubDate>Wed, 28 Jul 2010 17:04:55 +0000</pubDate>
<description><![CDATA[<p>This is nice. If I where to reload(module) after changing the contents of filename, what would happen? Presumably I would have to re-run CreateModule to re-exec the file contents. But If I re-initialised variable holding the old module with the new (modified) module, would all the code based on that module update? classes initialised from the module would need to be re-initialised, wouldn't they?</p>
]]></description>
<author>CHS2048@gmail.com</author>
<guid isPermaLink="true" >http://neverfear.org/blog/view/37/Dynamically_importing_Python_modules_by_filename/comments#comment_164</guid>
</item>
</channel>
</rss>