import and shared global variables
Hi, I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the example) to store some config data that the plugins can access. However, the output shown belown seems to imply that glob is *copied* or recreated during the import in m2. Am I missing something? I thought m1 should be in sys.modules and not be recreated during the import in m2. After browsing c.l.p, it seems that this is probably somehow due to the circular import. However, I do not really see why this should be a problem here. Interestingly, the problem disappears when I put the code in m1 in a real main() function instead of if __name__ etc. Though this seems to solve my problem, I still want to understand what's happening. Thanks, michael m1.py: -- glob = [1] if __name__ == __main__: glob.append(2) print m1.main().1:, glob m2 = __import__(m2) m2.test() print m1.main().2:, glob -- m2.py: -- def test(): import m1 print m2.test():, m1.glob - Output: m1.main().1: [1, 2] m2.test(): [1] m1.main().2: [1, 2] -- http://mail.python.org/mailman/listinfo/python-list
import and shared global variables
Hi, I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the example) to store some config data that the plugins can access. However, the output shown belown seems to imply that glob is *copied* or recreated during the import in m2. Am I missing something? I thought m1 should be in sys.modules and not be recreated during the import in m2. After browsing c.l.p, it seems that this is probably somehow due to the circular import. However, I do not really see why this should be a problem here. Interestingly, the problem disappears when I put the code in m1 in a real main() function instead of if __name__ etc. Though this seems to solve my problem, I still want to understand what's happening. Thanks, michael m1.py: -- glob = [1] if __name__ == __main__: glob.append(2) print m1.main().1:, glob m2 = __import__(m2) m2.test() print m1.main().2:, glob -- m2.py: -- def test(): import m1 print m2.test():, m1.glob - Output: m1.main().1: [1, 2] m2.test(): [1] m1.main().2: [1, 2] -- http://mail.python.org/mailman/listinfo/python-list
Re: import and shared global variables
Michael Brenner wrote: Hi, I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the example) to store some config data that the plugins can access. However, the output shown belown seems to imply that glob is *copied* or recreated during the import in m2. Am I missing something? I thought m1 should be in sys.modules and not be recreated during the import in m2. After browsing c.l.p, it seems that this is probably somehow due to the circular import. However, I do not really see why this should be a problem here. Interestingly, the problem disappears when I put the code in m1 in a real main() function instead of if __name__ etc. Though this seems to solve my problem, I still want to understand what's What happens here is that there does end up being two copies of m1: the one named __main__ and the one imported as m1. If you think about this, there has to be two copies -- otherwise how could sometimes __name__ be __main__ and sometimes not. Anyway, there are several options. The simplest one here is not to modify anything locally from __main__ block. Instead import m1, and modify that copy. That is: glob = [1] if __name__ == __main__: import m1 m1.glob.append(2) print m1.main().1:, m1.glob m2 = __import__(m2) m2.test() print m1.main().2:, glob Regards, -tim happening. Thanks, michael m1.py: -- glob = [1] if __name__ == __main__: glob.append(2) print m1.main().1:, glob m2 = __import__(m2) m2.test() print m1.main().2:, glob -- m2.py: -- def test(): import m1 print m2.test():, m1.glob - Output: m1.main().1: [1, 2] m2.test(): [1] m1.main().2: [1, 2] -- http://mail.python.org/mailman/listinfo/python-list
Re: import and shared global variables
I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the example) to store some config data that the plugins can access. However, the output shown belown seems to imply that glob is *copied* or recreated during the import in m2. Am I missing something? I thought m1 should be in sys.modules and not be recreated during the import in m2. Yes, you are missing that your first glob is in __main__.glob, _not_ in m1.glob. To make that happen, use something like this: glob = [1] def main(): pass if __name__ == __main__: import m1 m1.main() Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: import and shared global variables
Ah, thanks everybody! I had thought that, although the name was set to __main__, the module that was stored in sys.modules was m1 nevertheless, not a copy. Well, having to write import m1 inside m1.py seems a bit peculiar - it's probably nicer to keep the __main__ module free from stuff that has to be imported by others. Would a module global.py (defining glob and imported by whoever needs it) be more pythonic? (I didn't want to do that because I really want to resist the temptation of introducing glob1, glob2, glob3...) michael To make that happen, use something like this: glob = [1] def main(): pass if __name__ == __main__: import m1 m1.main() Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: import and shared global variables
has to be imported by others. Would a module global.py (defining glob and imported by whoever needs it) be more pythonic? I'd say yes. (I didn't want to do that because I really want to resist the temptation of introducing glob1, glob2, glob3...) I miss seeing what that has to do with creating a special module. Diez -- http://mail.python.org/mailman/listinfo/python-list