给Python IDLE加上自动补全和历史功能

redswallow posted @ Sun, 23 May 2010 22:03:41 +0800 in python , 7840 readers

许多时候,我们使用Python,并不用写一个程序,一些不复杂的任务,我更喜欢在 IDLE(也就是交互式提示模式)下输入几行代码完成。然而,在这个模式下编辑代码,也有不够便利的地方,最主要的就是,不能用Tab自动补全,不能记忆 上一次输入的命令(没办法,谁让我们在Shell下习惯了呢)。
这时候,我们可以直接使用Python启动脚本,解决这个问题。

启动脚本的程序非常简单,这里不多说明,只给出代码:

import readline
import rlcompleter
import atexit
import os
# tab autocomplete
readline.parse_and_bind(‘tab: complete’)
# history file
histfile = os.path.join(os.environ['HOME'], ‘.pythonhistory’)
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

 完成之后,我们把它保存为.pythonstartup,存放在自己的目录下(譬如/home/yurii),再将PYTHONSTARTUP变量指向刚才放的地址,就可以了。最省事的办法是在bashrc中添加这样一行: 

export PYTHONSTARTUP=/home/yurii/.pythonstartup

 这样,不但增加了tab的自动补全功能,而且重新启动IDLE时,通过上下键,还能翻到上次输入的命令,非常方便。

 

Avatar_small
ningyu said:
Mon, 17 Jun 2013 11:06:10 +0800

lz试试alt+p,alt+n,http://forum.ubuntu.org.cn/viewtopic.php?t=249852

Avatar_small
Landon Jackson said:
Fri, 25 Jan 2019 19:18:31 +0800

Appreciate it intended for giving new messages the matter, When i count on understand far more. Tax Advice

Avatar_small
Gay Tube said:
Thu, 16 Apr 2020 22:33:44 +0800

Very nice blog and articles. I am realy very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post.

Avatar_small
123mvoies said:
Thu, 28 May 2020 16:54:38 +0800

movie drama site online https://123movies7.com

Avatar_small
free sexting website said:
Mon, 15 Jun 2020 01:31:18 +0800

I wish more writers of this sort of substance would take the time you did to explore and compose so well. I am exceptionally awed with your vision and knowledge.

Avatar_small
artes para mídias so said:
Sat, 27 Jun 2020 06:54:12 +0800

Hi there, I found your blog via Google while searching for such kinda informative post and your post looks very interesting for me

Avatar_small
kings lynn nightclub said:
Sat, 04 Jul 2020 04:29:48 +0800

Well everything has some pros and cons but its upto us that how we handle kings lynn nightclubs challenges because its always how do we respond to things.

Avatar_small
bigcommerce data ent said:
Wed, 08 Jul 2020 04:06:58 +0800

I really thank you for the valuable info on this great subject and look forward to more great posts

Avatar_small
Safir Painting said:
Sat, 08 Aug 2020 16:03:15 +0800

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing.

Avatar_small
oferty pracy said:
Fri, 21 Aug 2020 01:28:08 +0800

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well..

Avatar_small
job posting said:
Sat, 22 Aug 2020 21:07:49 +0800

I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site.

Avatar_small
Immobilien Franchise said:
Thu, 27 Aug 2020 02:17:44 +0800

Only a few blogger would discuss this topic the way you do.’.”`:

Avatar_small
Dj animation de mari said:
Sun, 13 Sep 2020 18:58:41 +0800

Im using a tiny problem I cant subscribe your feed, Im using google reader fyi.

Avatar_small
hansestadt bremen im said:
Wed, 30 Sep 2020 19:54:42 +0800

I am not afraid to find this but this is just insane.

Avatar_small
pak24tv said:
Mon, 05 Oct 2020 20:27:33 +0800

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. top 10 richest countries in the world top 10 poorest countries in the world top 10 most beautiful places in the world

Avatar_small
QHS Development said:
Tue, 06 Oct 2020 00:18:37 +0800

you have a great weblog here! do you need to have invite posts in my blog?

Avatar_small
RajaQQ said:
Thu, 22 Oct 2020 17:58:11 +0800

As a seller of legal steroids, you can buy Crazy Bulk products, explore stacks and finally get the body you’ve always wanted


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter