Python PyDev,防止 input() 中的回车

发布于 2024-09-14 21:55:30 字数 2525 浏览 8 评论 0原文

EDIT-4

我已经执行了 sitecustomize.py,但它抛出了一个错误。 这是它的代码

错误是:

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

So this might be a Python3 thing only?我知道这不是解释器,因为我可以在命令提示符下运行它。什么给?

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 并具有所有不错的代码片段和自动完成功能。

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

我的搜索总是带我回到这个邮件列表: http://www.mail-archive.com/[email protected]/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 ),但是没有提供解决方案。预先感谢您的帮助。

EDIT-4

I've gotten my sitecustomize.py to execute, but it tosses up an error. Here's the code for it.

The error is:

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

I'm not terribly advanced with Python yet, so I figured I'd comment out only the lines I did not think Iwould need. No encoding issues are showing up, so I just commented out lines 23-104, but that didn't help either.

EDIT-3

I also happened to have 2.5.1 installed, so I compiled another script with that.

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 ran fine, and resulted in

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

So this is possibly a Python3 thing only? I know it's not the interpreter, because I'm able to run it in command prompt just fine. What gives?

EDIT-2

Just tested with Helios, still having the same problem. Here's my test program:

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)))

And here's the output when I type 'g' and press Enter:

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

In the grand scheme of things, it's a small issue. I could use input().rstrip() and it works. But the workaround shouldn't even be necessary. I'm typing twice as much as I should need to in a language that I'm using because it's concise and pretty.

EDIT-1

This is Eclipse 3.5. Unfortunately that's the latest version that's been approved for use at work. I'm going to try 3.6 at home to see if that's any different, but I wouldn't be able to use it anyway.


(original question)

I've been learning some basic Python, and decided to go with PyDev since it supported Python 3 as well as having all the nice code snippet and auto complete features.

However, I'm running into that darned carriage return issue on Windows.

My searches always lead me back to this mailing list:
http://www.mail-archive.com/[email protected]/msg269758.html

So I have grabbed the sitecustomize.py file, tried to include it in the Python path for my configured interpreter, as well as my project, but to no avail.

Has anybody else managed to work through this? Or maybe knows how to get the new sitecustomize.py to actually execute so it can override input() and raw_input()?

I know I could always make a short module with my own replacement input() function, but I'd really like to fix the problem at its root. Aptana acknowledges the issue ( http://pydev.org/faq.html#why_raw_input_input_does_not_work_correctly ) but offers no solution. Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-09-21 21:55:30

找到了一个技巧,让它在我的 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 编写脚本,它就可以工作。

Figured out a hack to make it work locally to my Python installation. In \Lib\site-packages\ make a script called "sitecustomize.py", and put this code in it:

original_input = builtins.input

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

input.__doc__ = original_input.__doc__

builtins.input = input

I don't know anything about the side effects of this, or what sort of error checking I should be doing, but it works if you're using PyDev on Windows to write scripts with Python3.

心病无药医 2024-09-21 21:55:30

了解有关 sitecustomize.py 的更多信息以及它与 site.py 的关系。

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

Found out some more things about sitecustomize.py and how it relates to site.py.

I don't know how to add my own sitecustomize.py to PYTHONPATH for execution only in a PyDev project, so I just stuck it in ${Python31dir}\Libs\site-packages. The module runs now, but generates errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文