Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to detect or prevent multiple instances of the same modules in Python?
    primarykey
    data
    text
    <p>In short: in Python it is too easy to create multiple instances of the same module, each instance having its own set of global variables.</p> <p>I need to add a check to the module to detect such multiple instantiation and raise an exception.</p> <p>My problem is the same as this one: <a href="https://stackoverflow.com/questions/4897232/a-module-is-imported-multiple-times">Module imported multiple times</a></p> <p>This is the smallest directory structure to reproduce the problem:</p> <pre><code>/test/a/__init__.py /test/a/aa.py: print "aa: __name__: ", __name__ /test/b/b.py: from a import aa import aa </code></pre> <p>Then</p> <pre><code>export PYTHONPATH=/test:/test/a python /test/b/b.py </code></pre> <p>prints:</p> <pre><code>aa: __name__: a.aa aa: __name__: aa </code></pre> <p>So, module aa.py was imported twice with different names.</p> <p>Needless to say that module aa.py will get 2 sets of global variables, which screws up all the logic inside this module.</p> <p>Of course, in the trivial example above it is easy to detect error by eyes, but in complex project with multiple subdirectories, these errors keep popping up regularly.</p> <p>So, I need some really global variable or a process-wide store or something like that. Any idea?</p> <p>Edit: Bibhas asked for an example of multiple instances of the same global variable. Here it is:</p> <pre><code>/test/a/__init__.py /test/a/aa.py: print "aa: __name__: ", __name__ import thread import time test_var = __name__ def test(): for i in range(0,5): print "aa thread: test_var: ", test_var time.sleep(1) thread.start_new_thread( test, () ) /test/b/b.py: print "b: __name__: ", __name__ from a import aa import aa import time time.sleep(10) </code></pre> <p>Now running</p> <pre><code>export PYTHONPATH=/test:/test/a python /test/b/b.py </code></pre> <p>prints:</p> <pre><code>aa: __name__: a.aa aa: __name__: aa aa thread: test_var: aa aa thread: test_var: a.aa aa thread: test_var: aa aa thread: test_var: a.aa ... </code></pre> <p>So, it is clear that there are 2 instances of variable test_var. If I will try to implement a singleton in this module, there will be 2 instances of this singleton, etc.</p> <p>Edit2: So, solution suggested by Guy L is something like:</p> <pre><code>/test/a/aa.py: import os if "aa.py" in os.environ: raise Exception("Duplicate instantiation of aa.py") os.environ["aa.py"] = __name__ </code></pre> <p>It seems to work OK (as long as I do not call import on multiple threads). Anybody have a better one?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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