简体   繁体   English

如何从IDLE中的.py文件单独运行Python脚本的选择性部分

[英]How to run a selective part of the Python script individually from .py file in IDLE

I have just started using Python, and, recently, I came up with an issue. 我刚刚开始使用Python,最近我想到了一个问题。 When I am using a Python script for saving the code and running the code, right from the script, it always runs the whole code in the script. 当我使用Python脚本保存代码并运行代码时,直接从脚本开始,它将始终在脚本中运行整个代码。 I would like to know if there is any other option, where I can run only a selective part of the code. 我想知道是否还有其他选择,我只能在其中运行代码的一部分。 Let's suppose that we have the following code with us, and, I want to run the code till printing the thisdict , which is located in the sixth line. 假设我们有以下代码,并且我想运行代码直到打印位于第六行的thisdict But, when I try to run this code from the script in IDLE, it runs the whole code. 但是,当我尝试从IDLE脚本中运行此代码时,它将运行整个代码。 So, please let me know if there is any other solution to run the selective code from the whole script. 因此,请让我知道是否还有其他解决方案可以运行整个脚本中的选择性代码。

thisdict={
"brand" : "Ford",
"model" : "Mustang",
"year" : 1964
}
print thisdict
#Tracing the model using the index/key named "model"
print "Model of the car:", thisdict.get("model")
#Changing the "year" to 2018 and re-printing the dictionary
thisdict["year"]=2018
print thisdict 
#Print both keys and values from the dictionar
for x,y in thisdict.items():
print x,y
#Add a key and value to the dictionary

thisdict["color"]="red"
print thisdict

#To delete a keyword from the dictionary
del thisdict["color"]
print thisdict
#OR
thisdict.pop("model")
print thisdict

 #To remove the last item from the dictionary
thisdict.popitem()
print thisdict

#Dist constructore for creating a dictionary
thisdict=dict(brand="Ford",model="Mustang", year=1964)
print thisdict

In pretty much any IDE, you can set breakpoints, and in debugging mode, the execution will pause at each breakpoint. 在几乎所有IDE中,您都可以设置断点,在调试模式下,执行将在每个断点处暂停。

You've mentioned IDLE and tagged Xcode, so I'm not sure which you're using, but this link has a tutorial for debugging with breakpoints in IDLE. 您已经提到了IDLE并标记了Xcode,所以我不确定您使用的是哪个,但是此链接包含一个使用IDLE中的断点进行调试的教程。 This one is for Xcode (not using Python). 是针对Xcode的(不使用Python)。

This is a weirdly common problem for beginner programmers in Python! 对于Python初学者来说,这是一个奇怪的普遍问题! There are a lot of fixes. 有很多修复程序。

The first an most obvious is to just comment out anything that you don't want to run. 首先,最明显的是只注释掉您不想运行的所有内容。 Just put a "#" before any line that you don't want to run, and it won't run. 只要在您不想运行的任何行之前加上“#”,它就不会运行。 Most editors let you automatically comment out whole blocks at once. 大多数编辑器允许您一次自动注释掉整个块。

The second, and much better practice, way of doing this is to start using functions. 第二种方法(更好的做法)是开始使用函数。 So say you have this code: 假设您有以下代码:

print("HERE 1")
print("HERE 2")

And sometimes you want to run both lines but sometimes you only want to run one of them. 有时您想同时运行两条线,但有时您只想运行其中一条。 Then try putting them in different functions. 然后尝试将它们置于不同的功能中。

def print_1():
    print("HERE 1")

def print_2():
    print("HERE 2")

Now, you just need to put in a command (with no indentation), calling the function that you want: 现在,您只需要输入命令(不带缩进),即可调用所需的函数:

print_1()

You can put as much code as you like under a function. 您可以在函数下放置任意多的代码。 Also just FYI, this probably isn't important for you yet, but so that you know, the best way to call a function when a script is run directly is like this: 也是仅供参考,这对您可能并不重要,但是要知道,直接运行脚本时调用函数的最佳方法是这样的:

if __name__=="__main__":
    print_1()

But you can just write print_1() for now without the if name is main stuff. 但是,您现在可以只编写print_1(),而无需if名称是主要内容。 It will be relevant if you start importing the module from elsewhere. 如果您开始从其他地方导入模块,则将是相关的。

Then a third hack you can also use, and I still sometimes do this in complex situations where I don't know where control is and just want to kill a program, is this: 然后,您也可以使用第三个技巧,有时我仍然在复杂的情况下执行此操作,即我不知道控制权在哪里,只想杀死程序,是这样的:

import sys
sys.exit(0)

That will stop the program running whereever it is. 它将停止程序在任何地方运行。

Another option is to have a boolean at the top of your code. 另一种选择是在代码的顶部使用布尔值。

whole_program = True # or False 

print("here 1")
a = 1+3
print("here 2")
if whole_program:
    print("here 3")
    print("something else")
else:
    pass

Another option, if you are just writing scripts, is to use Jupter notebooks, which are very good at isolating sections of code to run. 如果您只是编写脚本,则另一个选择是使用Jupter笔记本,它非常适合隔离要运行的代码部分。

Oh, and of course, Python is an interpreted language. 哦,当然,Python是一种解释语言。 So you can always run one command at a time directly in the console! 因此,您总是可以一次直接在控制台中一次运行一个命令!

Ultimately, you should start learning to use functions, and then classes, because these are the base structures that you need to use to control the flow of a program. 最终,您应该开始学习使用函数,然后学习类,因为这些是控制程序流所需要使用的基本结构。

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

相关问题 如何从Idle的包中运行python文件? - how to run a python file in a package from Idle? 如何从相同的python脚本运行.py和.exe文件 - how to run both a .py and a .exe file from the same python script 如何在 Google Colab 笔记本的“.py”文件中运行 Python 脚本? - How to run a Python script in a '.py' file from a Google Colab notebook? 我需要从BAT文件在IDLE中运行python脚本 - I need to run a python script in IDLE from a BAT file 我如何运行脚本文件作为python setup.py安装的一部分? - How would I run a script file as part of the python setup.py install? 如何从 IDLE 命令行运行 Python 脚本? - How to run a Python script from IDLE command line? 如何从 IDLE 交互式 shell 运行 python 脚本? - How to run a python script from IDLE interactive shell? 如何在 IDLE 中运行与 Python 安装位置不同的文件夹中的 .py 文件? - How to run a .py file in IDLE which is in a different folder other than where Python is installed? 如何在 Python 中使用 IDLE 打开当前正在运行的脚本的 .py 文件? - How can I open the currently running script's .py file using IDLE in Python? 如何从python shell或闲置命令运行.exe文件 - How to run .exe file with command from python shell or idle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM