Python IDE - One Day of IDLE Toying

本文是Python新手入门指南,介绍了IDLE集成开发环境的使用。先说明了安装Python后如何启动IDLE,通过在解释器窗口输入命令进行实验,还介绍了将命令保存到程序文件、运行程序文件,以及处理语法错误等内容,最后提及可保存和加载程序文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

One Day of IDLE Toying

  • Spanish translation by Jose Miralles-Santana
  • Thai translation by Pramote Khuwijitjaru
  • Malaysian translation by Mohd. Fahmi Basar
  • Korean translation by Jisoo Yi
  • Chinese translation by Tongli Zhang
  • Greek translation by Leon IV Isauvros
  • Italian translation by Roberto Distefano
  • Indonesian translation by I Wayan Wandira
  • French translation by Yannick Gobin
  • Dutch translation by Mark Lambrecht
  • German translation by Gregor Lingl
  • Portuguese translation by Fernando Mana鏰s Ferreira

This page is meant to help new users of Python who might feel a little disoriented. One question that might come to mind is: ok, we've installed Python... ummm... now what?

It might be nice to have a "visual" guide to reduce any initial anxieties. That's what this page is for. The plan is to go through a small session with IDLE: the Integrated Development Environment. IDLE is designed to provide a simple way of exploring the language. During this session, I'll make a few fumbling mistakes, just to show what to expect when things don't go exactly hunky-dory.

By the way, this is an online document If you have any suggestions, or if you want to make corrections or improvements, please feel free to email at dyoo@hkn.eecs.berkeley.edu. Small plug: also, don't forget that there's a great resource in the Python Tutor mailing list: we're a group of people who like to show each other interesting stuff in Python. We're all learning together, so feel free to subscribe and join us.

Changes

  • October 26, 2004: Jose Miralles-Santana sent me a Spanish translation. Gracias!
  • February 10, 2004: Pramote Khuwijitjaru sent me a translation in Thai. Kob-Kkun!
  • February 2, 2004: A reader named Jim noticed that the link to the Beginner's tutorials doesn't go to the right place anymore! Fixed the link to http://www.python.org/topics/learn/non-prog.html
  • January 7, 2004: Mohd. Fahmi Basar has written a wonderful translation into Malaysian. Ka-sih! And Maafkan saya as well; I'm so sorry for being so late about updating the page. One thing drove out another, as one might say.
  • June 4, 2003: I forgot to put this link up: One Day of MacPython IDE Toying is an adaptation of this tutorial for MacPython. Much nicer screenshots, too. *grin*
  • March 11, 2003: Jisoo Yi just sent me a translation into Korean! Gam sa hap nee da!
  • February 8, 2003: Tongli Zhang sent me a new translation of the IDLE tutorial in Chinese. Shie-shie!
  • January 9, 2003: Leon IV Isauvros has written a Greek translation! Sas efharisto poli!
  • December 13, 2002: Roberto Distefano wrote a translation in Italian. This is awesome! Grazie!
  • August 26, 2002: I Wayan Wandira has translated the tutorial into Indonesian. Selamat tinggal! Also, I've implemented Ferg_S's suggestion to add page-breaking BR tags to make a dead-wood version of the guide more pleasant to read.
  • July 8, 2002: Yannick Gobin graciously donated a French translation. Merci!
  • April 30, 2002: Mark Lambrecht donated his Dutch translation. Dankzegging!
  • January 30, 2002: Fernando Mana鏰s Ferreira sent me a Portuguese translation. This is wonderful! Obrigado!
  • December 4 (Evening): Gregor Lingl sent me a German translation of the guide. Danke!
  • December 4, 2001: there's enough demand for this page to be done, so I've tried to patch the FIXME's throughout this page.]
  • [June 19, 2001: my computer monitor has just burnt to a brown crisp, so image updates to this page will be a little delayed until I get it replaced. My apologies!]


Ok, let's assume that we've already installed Python. (If not, we can visit: http://python.org and download the most recent Python interpreter. As of this writing, that's Python 2.1.) The first thing we'd like to do is actually start running it! We can do this by opening up IDLE, which should be in our Start Menu under the newly-created Python program group.



We'll see that a new window magnificantly opens up:

This is the main window to IDLE, and what we see right now is called the "Interpreter" window. The Interpreter allows us to enter commands directly into Python, and as soon as we enter in a command, Python will execute it and spit out its result back to us. We'll be using this Interpreter window a lot when we're exploring Python: it's very nice because we get back our results immediately. If it helps, we can think of it as a very powerful calculator.



Let's try something now! As per tradition, let's get Python to say the immortal words, "Hello World".

Those '>>>' signs act as a prompt for us: Python is ready to read in a new command by giving us that visual cue. Also, we notice that as we enter in commands, Python will give us its output immediately.



Ok, this seems pretty simple enough. Let's try a few more commands. If we look below:

we'll see the result of running a few more commands. Don't worry too much about knowing the exact rules for making programs yet: the idea is that we can experiment with Python by typing in commands. If things don't work, then we can correct the mistake, and try it again.

If you got to this point, you now know enough to start playing around with Python! Crack open one of the tutorials from the Python For Beginners web page, and start exploring with the interpreter. No time limit here. *grin*



Now that we've paddled long enough, we might be asking: ok, this is neat, but if we close down Python and start it up again, how do we get the computer to remember what we typed?

The solution is a little subtle: we can't directly save what's on the interpreter window, because it will include both our commands and the system's responses. What we'd like is to make a prepared file, with just our own commands, and to be able to save that file as a document. When we're in the mood, we can later open that file and "run" Python over it, saving us the time of retyping the whole thing over again.

Let's try this. First, let's start with a clean slate by opening up a new window.

Here's the result of that menu command:

We notice that there's nothing in this new window. What this means is that this file is purely for our commands: Python won't interject with its own responses as we enter the program, that is, not until we tell it to. I'll call this the "Program" window, to distinguish it from the Interpreter window.



What we wanted to do before was save some of the stuff we had tried out on the interpreter window. Let's do that by typing (or copy/pasting) those commands into our Program window.

Ok, we're done with copying and pasting. One big thing to notice is that we're careful to get rid of the ">>>" prompts because there's not really part of our program. The interpreter uses them just to tell us that we're in the interpreter, but now that we're editing in a separate file, we can remove the artifacts that the interpreter introduces.



Let's save the file now. The Save command is located under the File menu:



Now that we've saved the program, how do we run the program? If we look at the menus on our program window,

we'll see that there's a menu option to "Run script", and that's what we'll do. What we want to see is Python running through the program, and displaying its results in the Interpreter window.

By the way, one thing to notice is that I made a typo: I didn't quite copy exactly what I had entered in the interpreter window before. Does this affect things?

Ooops. Here is an example of what Python calls a "syntax error". Python sees that we made a typo, and warns us to take a much closer look at our program. The designers of Python feel that having the system point out the error is better than trying to guess at what the programmer meant. It's the idea of explicitness versus implicitness. There are certain rules that Python follows that measure what looks good and what looks suspicious. As we speak the language, we'll get a feel for these rules. And if you're feeling suspicious, yes, this is a bit like grammar. *grin*

Python is often perceptive enough to direct us toward the problem, and in this case, it's telling us that we forgot to put something at the end of this line. In this case, we need to add an additional quotation mark. Let's add that in now.



Ok, let's say that we fixed that silly typo. Let's try to run the program again.

Another snag, but not that complicated, just silly. One other thing that might put us off guard is that IDLE wants us to save any Program windows before we run them; this is a user interface issue that makes sure that we save our work before we start running the program.

(Note: the comment above might be outdated; enough people got annoyed by IDLE's lazy behavior that they've clamored for change. We hope that the development version of IDLE doesn't give this error message anymore.)

They say that third time's the charm, so let's try running it again. Hopefully, it should run well now.



As we play with Python, we'll find ourselves "switching modes" between the Interpreter window and the Program window. The reason for this is because we can use the Interpreter as if it were a small laboratory and experimentally try small programs. After we're satisfied (or when we're tired), we can save what we've learned into a program file.

This, of course, assumes that we can actually load the file later on; it would be a silly thing to save a program, and not be able to load it up later. Let's show this, and then stop for today. I'll close everything down in IDLE, and start from a clean slate.

We'll find the Open command under the File menu:

and if all goes well, we should see a new Program window open up:

with our old program. We're in business! We can save our old work and open it up at a later time. It's not jaw-opening, but it is crucial for the person who wants to play with Python for more than a day. *grin*

This is pretty much all we need to know about IDLE to actually do interesting things. This guide has skipped a lot of stuff about IDLE: IDLE is much more than an mere editor, but it takes some time to explore all of its features, so let's stop for now. There's a IDLE Documentation page that explains advanced IDLE use, for those with an insatiable curiosity. Again, if you have any questions, please feel free to stop by the Python Tutor mailing list; there's usually someone there who'd be happy to chat about Python with you. It's been fun, and I hope this has been helpful!

Back to my personal Python page.

<think>好的,用户现在问的是关于`apt-cache rdepends python3.8`的输出结果,并询问是否建议卸载。之前用户已经询问过如何卸载Python 3.8,而我在之前的回答中提到了需要检查依赖关系,特别是使用这个命令来查看反向依赖。现在用户执行了这个命令,得到了具体的依赖列表,想知道是否可以安全卸载。 首先,我需要分析用户提供的输出。依赖列表里包括了一些关键软件包,比如`python3`、`gedit`、`obs-studio`、`rhythmbox-plugins`等。这些看起来都是用户可能常用的应用或系统组件。这意味着如果卸载Python 3.8,这些软件可能会被同时移除,导致系统功能缺失或损坏。 接下来,我需要考虑用户的背景。用户可能是一个普通Ubuntu用户,尝试卸载Python 3.8可能是为了清理空间或者升级到新版本,但可能不了解系统内部依赖关系的重要性。用户可能没有意识到卸载Python 3.8会影响其他关键应用,甚至系统稳定性。 用户的需求可能是安全地卸载Python 3.8,但需要明确告知风险,并提供替代方案。需要解释为什么这些反向依赖存在,卸载会带来什么问题,以及是否有其他方法达到用户的目的,比如使用虚拟环境或者保留Python 3.8但不作为默认版本。 还需要注意用户之前的问题中提到了手动安装的情况,可能需要提醒用户如果Python 3.8是手动安装的,处理方式不同。但根据当前的依赖列表,这些是通过apt安装的软件包,因此属于系统包管理的一部分。 需要明确建议用户不要直接卸载Python 3.8,因为可能导致系统问题,并给出替代建议,比如使用update-alternatives管理多版本,或者使用虚拟环境。同时,如果用户确实需要卸载,必须警告风险,并指导如何安全操作,比如逐个检查依赖项,手动移除不重要的软件包,但避免影响核心组件。 最后,总结时需要强调谨慎操作,备份数据,并评估是否真的需要卸载系统级的Python版本,可能存在的风险是否值得。</think>--- ### **关键分析:依赖列表解读** 你列出的反向依赖关系表明,**多个系统组件和常用软件依赖Python 3.8**。以下是需要特别注意的条目: | 依赖包名称 | 风险等级 | 说明 | |------------------------|----------|----------------------------------------------------------------------| | `python3` | ⚠️ 高危 | 系统默认Python 3的元包,卸载可能导致所有Python 3程序失效 | | `gedit` | ⚠️ 高危 | Ubuntu默认文本编辑器 | | `obs-studio` | ⚠️ 中危 | 直播/录屏工具 | | `rhythmbox-plugins` | ⚠️ 中危 | 音乐播放器插件 | | `kdevelop-python` | ⚠️ 中危 | KDE开发环境的Python支持 | | `cluster-glue` | ⚠️ 高危 | 高可用性集群基础设施组件 | --- ### **卸载建议** #### **1. 强烈不建议直接卸载** - 若强行卸载Python 3.8,**所有依赖它的软件包会被连带删除**(包括`gedit`等关键工具) - 可能导致: ```bash $ sudo apt purge python3.8 下列软件包将被【自动卸载】: gedit obs-studio rhythmbox-plugins python3 ... ``` #### **2. 替代解决方案** - **方案一:保留Python 3.8但禁用冲突** ```bash # 使用update-alternatives管理多版本 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 sudo update-alternatives --config python3 # 选择其他版本作为默认 ``` - **方案二:仅清理用户级安装** ```bash # 删除通过pip安装的第三方库(不影响系统包) python3.8 -m pip freeze | xargs python3.8 -m pip uninstall -y ``` --- ### **如果必须卸载的操作指南** 1. **逐步卸载非关键依赖**(示例): ```bash # 先卸载明确不需要的软件(如stimfit、pitivi等) sudo apt purge stimfit pitivi # 再尝试卸载Python 3.8 sudo apt purge python3.8 ``` 2. **强制保留关键组件**: ```bash # 使用apt-mark阻止自动卸载关键包 sudo apt-mark hold gedit python3 cluster-glue ``` --- ### **风险验证实验** 可先在虚拟机中测试卸载影响: ```bash # 1. 创建系统快照 sudo timeshift --create # 2. 模拟卸载(不执行实际操作) sudo apt -s purge python3.8 # 3. 观察输出中的"将被自动卸载"列表 ``` --- ### **最终建议** 🔴 **除非你完全理解所有依赖项的作用**,否则不要卸载系统级Python 3.8。 🟢 如果目标是解决版本冲突,建议通过`virtualenv`或`pyenv`管理独立Python环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值