Windows上Python cmd模块下pyreadline自动补全的奇怪行为

0 投票
1 回答
1381 浏览
提问于 2025-04-17 15:11

我正在尝试在以下环境中运行这段简单的代码:

  • Python 2.7.2
  • Windows 7
  • 通过pip安装了pyreadline

    class HelloWorld(cmd.Cmd):
        """Simple command processor example."""
    
        FRIENDS = [ 'Alice', 'Adam', 'Barbara', 'Bob', 'Chris' ]
    
        def do_greet(self, person):
            "Greet the person"
            if person and person in self.FRIENDS:
                greeting = 'hi, %s!' % person
            elif person:
                greeting = "hello, " + person
            else:
                greeting = 'hello'
            print greeting
    
        def complete_greet(self, text, line, begidx, endidx):
            if not text:
                completions = self.FRIENDS[:]
            else:
                completions = [ f
                                for f in self.FRIENDS
                                if f.startswith(text)
                                ]
            return completions
    
        def do_EOF(self, line):
            return True
    
    if __name__ == '__main__':
        HelloWorld().cmdloop()
    

看起来自动补全功能没有正常工作。当我运行代码并输入:

(Cmd) greet A<tab>

它没有给出任何自动补全的建议,但应该能显示“Alice Adam”。

不过,当我输入:

(Cmd) greet C<tab>

它正确地补全为“Chris”。所以这似乎和是否有多个匹配项有关。

有没有人能解释一下或者知道怎么解决这个问题?

谢谢!

编辑:删除了多余的短语。

1 个回答

0

好的,我搞明白了。文件 pyreadlineconfig.ini 放错地方了。

我按照这个链接里的方法修复了问题:Windows上iPython的模糊补全不工作

里面说要把这个文件复制到 %HOMEPATH% 这个位置。结果发现我电脑上的这个环境变量配置错了,文件被复制到了 e:\pyreadlineconfig.ini。

其实应该放在 c:\users\用户名 这个路径下。

撰写回答