简体   繁体   English

Python-仅运行一行代码,而不运行脚本中的其余多行

[英]Python - Running just a single line of code, and not the rest of the Multiple lines in the Script

In - Python, inside the the IDLE, in the File Editor Window, 在-Python中,在IDLE中,在文件编辑器窗口中,

How do you run just a selected single line of code in the script, without having the rest of the lines of the program being runned ? 如何在脚本中仅运行选定的单行代码,而不运行程序的其余行?

You'll have to run your line of code form command line: 您必须运行代码行命令行:

With the -c (command) argument (assuming your file is named foo.py): 使用-c(命令)参数(假设您的文件名为foo.py):

$ python -c 'import foo; print foo.hello()'

You can select and copy a single statement in an Idle editor (or anywhere else, for that matter), switch to the Idle Shell, and paste on the line with the >>> prompt as the bottom. 您可以在Idle编辑器中(或其他任何地方)选择并复制一条语句,切换到Idle Shell,然后将>>>提示作为底部粘贴在该行上。 (You can hit to get a clean prompt.) Then hit return just as it you had entered into the shell directly. (您可以按一下以得到一个干净的提示。)然后按回车键,就像直接进入外壳一样。 This works for multiline statements. 这适用于多行语句。 Being able to do this with a menu selection, hotkey, or right-click selection is on my todo list, as you are not the first to ask about this. 可以通过菜单选择,热键或右键选择来执行此操作,这在我的待办事项列表中,因为您不是第一个询问此问题的人。

Basically, what we call "debugger" should be fulfilled your requirement. 基本上,我们所谓的“调试器”应满足您的要求。 Many IDEs are provided "debugger tools" including IDLE . 提供了许多IDE“调试器工具”,包括IDLE To use a debugger tool you can just simply 要使用调试器工具,您只需

  1. add break lines (any lines of code you want a program to stop while program is running), 添加换行符(您希望程序在运行时停止的任何代码行),
  2. explore your variables (when program is stop you can print out values of your variables at that time) 探索您的变量(当程序停止时,您可以在那时打印出变量的值)
  3. Click/type step to go to the next breakpoint. 单击/键入step转到下一个断点。

This is just a roughly procedure that should be fit to your requirement. 这只是一个粗略的过程,应该适合您的要求。

see more: debug 查看更多: 调试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Shell脚本的一行中编写多行python代码 - Writing multiple lines of python code in a single line in shell script 如何在Python中将多行代码作为一行编写? - How to write multiple lines of code in Python as a single line? 什么#!表示在示例python代码中多次显示而不仅仅是脚本的第一行? - What does #! mean when showing up multiple times in example python code and not just the first line of a script? 从 python 中的单个脚本运行多个脚本 - Running multiple scripts from a single Script in python Python脚本未与本地Web服务器一起运行,仅在浏览器中显示代码 - Python script not running with local web server, just displays code in browser 使用sed在python中用多行替换单个行模式 - Replace a single line pattern by multiple lines in python using sed Python:将多行连接到单个行/字符串中并将其附加到列表中 - Python: joining multiple lines into a single line/string and appending it to a list Python for循环迭代可将多行合并为一行 - Python for loop iteration to merge multiple lines in a single line 将多行转换为单行并存储在python中的变量中 - Convert multiple lines to single line and store in a variable in python 如何在 Python 中将单行 csv 文件拆分为多行 - How to split single line csv file into multiple lines in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM