You need to distinguish between different globals() .
For example, suppose we have a module: mymodule.py
foo = 100 def test(): bar = 200 return bar
We run it under the control of pdb .
>>> import pdb >>> import mymodule >>> foobar = 300 >>> pdb.run('mymodule.test()') > <string>(1)<module>() (Pdb) print foobar 300 (Pdb) print foo *** NameError: name 'foo' is not defined (Pdb) global foobar2; foobar2 = 301 (Pdb) print foobar2 301
At the beginning, namely, before running test() , the environment in pdb is your current globals() . Thus, foobar is defined, and foo not defined.
Then we execute test() and stop at the end of bar = 200
-> bar = 200 (Pdb) print bar 200 (Pdb) print foo 100 (Pdb) print foobar *** NameError: name 'foobar' is not defined (Pdb) global foo2; foo2 = 101 (Pdb) print foo2 101 (Pdb) c >>>
The environment in pdb has been changed. It uses mymodule globals() in test() . Thus, foobar is not defined. while is not defined. while foo`.
We exported two variables foobar2 and foo2 . But they live in different areas.
>>> foobar2 301 >>> mymodule.foobar2 Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> mymodule.foobar2 AttributeError: 'module' object has no attribute 'foobar2' >>> mymodule.foo2 101 >>> foo2 Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> foo2 NameError: name 'foo2' is not defined
You have already found a solution. But it works a little differently.
source share