开发者

How to get Python interactive console in current namespace?

I would like to have my Python code start a Python interactive console (REPL) in the middle of running code using something like code.interact(). But the console that code.interact() starts doesn't see the 开发者_Python百科variables in the current namespace. How do I do something like:

mystring="hello"

code.interact()

... and then in the interactive console that starts, I should be able to type mystring and get "hello". Is this possible? Do I need to set the "local" argument of code.interact() to something? What would this be set to? How should it be called?


Try:

code.interact(local=locals())


For debug I usually use this

from pdb import set_trace; set_trace()

it may help


Another way is to start the debugger, and then run interact:

import pdb
pdb.set_trace()

Then from the debugger:

(Pdb) help interact
interact

        Start an interactive interpreter whose global namespace
        contains all the (global and local) names found in the current scope.
(Pdb) interact
*interactive*
>>>


For Python 3.10.0:

code.InteractiveConsole(locals=locals()).interact()

See the Python documentation for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜