Table of Contents
回复内容:
Home Backend Development Python Tutorial 怎么在.py程序中进入python的交互模式?

怎么在.py程序中进入python的交互模式?

Jun 06, 2016 pm 04:23 PM
python

程序执行到这里后,等待输入python命令
最好可以保存上下文信息,能够随时退出

考虑单文件的情况,最好在进入断点时相当于把文件的前一部分手动输入repl

回复内容:

<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">code</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">x</span><span class="p">():</span>
<span class="gp">... </span>    <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span>    <span class="n">code</span><span class="o">.</span><span class="n">interact</span><span class="p">(</span><span class="n">banner</span><span class="o">=</span><span class="s">""</span><span class="p">,</span><span class="n">local</span><span class="o">=</span><span class="nb">locals</span><span class="p">())</span>
<span class="gp">... </span>
<span class="gp">>>> </span><span class="n">x</span><span class="p">()</span>

<span class="gp">>>> </span><span class="n">a</span>
<span class="go">1</span>
<span class="go">>>></span>
Copy after login
谢邀。
如@刘鑫 老师说的,有很多工具能够提供这种功能。

IPython 进入方法:
<span class="kn">from</span> <span class="nn">IPython</span> <span class="k">import</span> <span class="n">start_ipython</span>
<span class="n">start_ipython</span><span class="p">()</span>
Copy after login
有现成的工具呀,ipython有提供这个功能,去他们官网可以看到文档。 CPython 自带了命令行交互接口,如大家所说只需要:
<span class="kn">import</span> <span class="nn">code</span>
<span class="n">code</span><span class="o">.</span><span class="n">interact</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">banner</span><span class="p">,</span> <span class="n">local</span><span class="o">=</span><span class="n">context</span><span class="p">)</span>
Copy after login
<span class="kn">import</span> <span class="nn">IPython</span>
<span class="n">IPython</span><span class="o">.</span><span class="n">embed</span><span class="p">()</span>
Copy after login
不是有pdb这么专业的调试工具么… 只需要插入两行:

import pdb
pdb.set_trace()
# 之后程序进入pdb调试

可以输入c,让程序继续运行 请问在 Ipython中使用IPython.embed()中断程序后,如何继续运行程序啊?新手小白一个,谢谢 ptpython- a better Python REPL
from ptpython.repl import embed
embed(globals(), locals(), vi_mode=False, history_filename=None)
Copy after login
你说的应该是raw_input吧

比如
name=raw_input("enter your name:")
print name

或者你的想法是
while(true):
command=raw_input()
if(command != "stop"):
exec(command)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles