【问题标题】:Python PyDev, Prevent carriage returns from input()Python PyDev,防止输入()回车
【发布时间】:2011-03-31 17:02:24
【问题描述】:

EDIT-4

我已经执行了我的 sitecustomize.py,但它抛出了一个错误。 Here's the code 为它。

错误是:

Error in sitecustomize; set PYTHONVERBOSE for traceback:
RuntimeError: maximum recursion depth exceeded while calling a Python object

我对 Python 的使用还不是很先进,所以我想我只会注释掉我认为不需要的行。没有出现编码问题,所以我只是注释掉了第 23-104 行,但这也没有帮助。

EDIT-3

我也碰巧安装了 2.5.1,所以我用它编译了另一个脚本。

print 'This will test carriage returns on Windows with PyDev on Eclipse Helios'
print'Type something:',
test = raw_input()
print('You entered the following ascii values:')
for c in test:
    print(str(ord(c)))

这运行良好,并导致

This will test carriage returns on Windows with PyDev on Eclipse Helios
Type something: g
You entered the following ascii values:
103

所以这可能只是 Python3 的事情?我知道它不是解释器,因为我能够在命令提示符下运行它就好了。什么给了?

EDIT-2

刚刚用 Helios 测试过,仍然有同样的问题。这是我的测试程序:

print('This will test carriage returns on Windows with PyDev on Eclipse Helios.')
print('Type something:', end='')
test = input()
print('You entered the following ascii values:')
for c in test:
    print(str(ord(c)))

这是我输入“g”并按 Enter 时的输出:

This will test carriage returns on Windows with PyDev on Eclipse Helios.
Type something:g
You entered the following ascii values:
103
13

从总体上看,这是一个小问题。我可以使用 input().rstrip() 并且它有效。但解决方法甚至不应该是必要的。我用我正在使用的语言输入的字数是我应该输入的两倍,因为它简洁漂亮。

EDIT-1

这是 Eclipse 3.5。不幸的是,这是已获准在工作中使用的最新版本。我打算在家里尝试 3.6 看看有什么不同,但无论如何我都无法使用它。


(原始问题)

我一直在学习一些基本的 Python,并决定使用 PyDev,因为它支持 Python 3 以及所有漂亮的代码 sn-p 和自动完成功能。

但是,我在 Windows 上遇到了该死的回车问题。

我的搜索总是让我回到这个邮件列表: http://www.mail-archive.com/python-list@python.org/msg269758.html

所以我抓取了 sitecustomize.py 文件,尝试将它包含在我配置的解释器的 Python 路径中,以及我的项目中,但无济于事。

还有其他人设法解决这个问题吗?或者可能知道如何让新的 sitecustomize.py 实际执行,以便它可以覆盖 input() 和 raw_input()?

我知道我总是可以用我自己的替换 input() 函数制作一个简短的模块,但我真的很想从根本上解决问题。 Aptana 承认了这个问题 (http://pydev.org/faq.html#why_raw_input_input_does_not_work_correctly),但没有提供任何解决方案。提前感谢您的帮助。

【问题讨论】:

    标签: python windows eclipse pydev carriage-return


    【解决方案1】:

    了解更多关于 sitecustomize.py 的内容以及它与 site.py 的关系。

    我不知道如何将我自己的 sitecustomize.py 添加到 PYTHONPATH 以便仅在 PyDev 项目中执行,所以我只是将它放在 ${Python31dir}\Libs\site-packages 中。该模块现在运行,但会产生错误。

    【讨论】:

      【解决方案2】:

      想出了一个技巧,让它在我的 Python 安装本地工作。在 \Lib\site-packages\ 中创建一个名为“sitecustomize.py”的脚本,并将此代码放入其中:

      original_input = builtins.input
      
      def input(prompt=''):  
          return original_input(prompt).rstrip('\r')
      
      input.__doc__ = original_input.__doc__
      
      builtins.input = input
      

      我不知道这样做的副作用,或者我应该做什么样的错误检查,但如果你在 Windows 上使用 PyDev 来用 Python3 编写脚本,它就可以工作。

      【讨论】:

      • 安装该脚本后,IDLE 无法启动(Windows、Python 3)。
      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 2011-08-28
      相关资源
      最近更新 更多