Chris's Wiki :: blog/python/DualImportProblems Commentshttps://utcc.utoronto.ca/~cks/space/blog/python/DualImportProblems?atomcommentsDWiki2013-05-12T20:47:30ZRecent comments in Chris's Wiki :: blog/python/DualImportProblems.From 88.96.1.126 on /blog/python/DualImportProblemstag:CSpace:blog/python/DualImportProblems:5e6df9791d8430fd8469ae8c5fefb8b08063efecFrom 88.96.1.126<div class="wikitext"><p>Anonymous commenter, you are confusing the local name used by the importer (t1 or t2 in your example) with the global name used in sys.modules (testimport in your example).</p>
<p>Try this:</p>
<p>$ mkdir -p foo/bar <br>
$ touch foo/__init__.py foo/bar/__init__.py <br>
$ mv testimport.py foo/bar/</p>
<p>>>> import sys <br>
>>> sys.path.append('foo') <br>
>>> import foo.bar.testimport as ti1 <br>
>>> import bar.testimport as ti2 <br>
</p>
<p>- Ben Hutchings</p>
</div>2013-05-12T20:47:30ZFrom 83.157.48.229 on /blog/python/DualImportProblemstag:CSpace:blog/python/DualImportProblems:0663a396e98fdf7660660fb24068263457f28204From 83.157.48.229<div class="wikitext"><p>I am very surprised by your post because it is not what I observe:</p>
<p>Just consider this module (put it in <code>testimport.py</code>)</p>
<pre>
def create_const():
import random
return random.random()
class Test(object):
my_static = create_const()
def __init__(self):
self.my_local = create_const()
print "static= %f" % self.my_static
print "local = %f" % self.my_local
</pre>
<p>then use it via 2 imports:</p>
<pre>
import testimport as ti1
t1 = ti1.Test()
</pre>
<p>and</p>
<pre>
import testimport as ti2
t2 = ti2.Test()
</pre>
<p>you have back</p>
<pre>
>>> static= 0.982529
>>> local = 0.620927
</pre>
<p>and </p>
<pre>
>>> static= 0.982529
>>> local = 0.435612
</pre>
<p>the first variable <strong>being the same</strong>, meaning that <code>t1</code> and <code>t2</code> are pointing o the same module that is once in memory only, no?</p>
</div>2013-05-12T06:55:44Z