有没有办法在 python doctest 中重新启动或重置 python 解释器?

发布于 2024-11-06 08:33:05 字数 156 浏览 6 评论 0原文

我正在编写一个简短的教程,并且希望能够使用 python 的 doctest 来运行其中的示例。

python -m doctest foo.txt

教程中的某个点我想开始使用一个新的、干净的 python 解释器。有这样做的机制吗?

I am writing a short tutorial, and would like to be able to run the examples therein using python's doctest using

python -m doctest foo.txt

There is a point in the tutorial at which I want to start using a new, clean python interpreter. Is there a mechanism for doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

得不到的就毁灭 2024-11-13 08:33:05

您可以使用代码模块来创建新的解释器。您甚至可以复制全局/局部变量。

Blender 文档中有一个很好的示例 此处

他们提出以下建议:

在脚本中间,您可能需要检查变量、运行函数并检查流程。

import code
code.interact(local=locals())

如果您想访问全局变量和局部变量,请运行以下命令:

import code
namespace = globals().copy()
namespace.update(locals())
code.interact(local=namespace)

下一个示例是上面脚本的等效单行版本,它更容易粘贴到代码中:

__import__('code').interact(local=dict(globals(), **locals()))

code.interact 可以添加到脚本中的任何行,并将暂停该脚本在终端中启动交互式解释器,完成后您可以退出解释器,脚本将继续执行。

You can use the code module to create a new interpreter. You can even copy global/local variables.

There is a nice example within the Blender documentation here

They advise the following:

In the middle of a script you may want to inspect variables, run functions and inspect the flow.

import code
code.interact(local=locals())

If you want to access both global and local variables run this:

import code
namespace = globals().copy()
namespace.update(locals())
code.interact(local=namespace)

The next example is an equivalent single line version of the script above which is easier to paste into your code:

__import__('code').interact(local=dict(globals(), **locals()))

code.interact can be added at any line in the script and will pause the script to launch an interactive interpreter in the terminal, when you’re done you can quit the interpreter and the script will continue execution.

小草泠泠 2024-11-13 08:33:05

如果您只想在 python 解释器中启动一个新的 python 解释器,您只需发出命令: os.system('python')

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import os
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'os']
>>> os.system('python')
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> quit()
0
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'os']
>>>

但是,如果您想重新启动或重置 python 解释器,而不是像上面那样启动新的 python 解释器你可以看看这个解决方案。我还没有探索过,但应该可以帮助你开始。

安静地重新启动 Python 解释器

If all you want is to start a new python interpreter inside the python interpreter you can just issue the command: os.system('python')

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import os
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'os']
>>> os.system('python')
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> quit()
0
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'os']
>>>

if however, you want to restart or reset python interpreter and not start a new python interpreter like above you could look at this solution. I haven't explored it but should help you get started of.

Restarting a Python Interpreter Quietly

揪着可爱 2024-11-13 08:33:05

要在 IDLE 中执行文件,只需按键盘上的 F5 键即可。您还可以从菜单栏中选择“运行”→“运行模块”。任一选项都会重新启动 Python 解释器,然后运行您使用新解释器编写的代码。

To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you've written with a fresh interpreter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文