蟒蛇 + SVN + Windows/Mac = 语法无效?
我很确定以下错误与我通过 SVN 与使用 Windows 系统的同事共享代码有关。
我自己在 Mac 上使用 Python,并使用 TextMate 进行编辑。
#!/usr/bin/python
import os
from google.appengine.api import users
from google.appengine.ext import webapp
...
运行该代码时,我收到语法错误:
events.py:2 invalid syntax
使用 SVN 时是否存在行尾问题?
感谢每一个提示。
编辑
该问题似乎不是由 SVN 引起的。
有趣的是,直接在 Shell 中执行,不会出现语法错误。但使用 Textmate 进行验证(使用 PyCheckMate)并尝试使用 GoogleAppEngineLauncher 启动都会返回错误。
I'm pretty sure the following error is related to the fact that I'm sharing code via SVN with a colleague that is using a Windows system.
Myself, I use Python on Mac, editing with TextMate.
#!/usr/bin/python
import os
from google.appengine.api import users
from google.appengine.ext import webapp
...
When running that code, I get a syntax error:
events.py:2 invalid syntax
Is there an end-of-line issue when using SVN?
Thankful for every hint.
edit
The problem seems not to be caused by SVN.
Interestingly, executing directly at the Shell, there's no Syntax Error. But both validating with Textmate (using PyCheckMate) and trying to launch with GoogleAppEngineLauncher return the error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然 Python 不应该关心行结尾,但你的 Mac 不喜欢在第一行有 CRLF 行结尾,所以这可能是你的问题。
通过将
svn:eol-style
属性设置为native
,可以让 SVN 翻译行结尾。然后,当您在 Windows 中签出文件时,它将把您的 LF 结尾转换为 CRLF;当您在 Mac 上签出文件时,它会将您同事的 CRLF 结尾转换为 LF。While Python shouldn't care about line endings, your Mac won't like having a CRLF line ending on the first line, so this may be your problem.
SVN can be told to translate line endings by setting the
svn:eol-style
property tonative
. It will then translate your LF endings to CRLF when the file is checked out in Windows, and translate your colleague's CRLF endings to LF when you check out on your Mac.虽然 Windows 和计算世界的其他部分之间的行结束符(通常)确实不同,但 Python 被设计为对这个问题不可知。通常,Python 不会遇到不同的行结尾问题。
我尝试在 Mac 上运行具有多种行结尾的 Python 脚本,没有出现任何问题。请注意,我使用以下命令运行我的脚本:
而不是
可能值得尝试两种形式来查看您的问题是否真的是 Python 或者是否与您的 shell/内核有关。我知道某些环境确实在 shebang 行上的 CRLF 结尾方面存在问题。
While it's true that line endings are (usually) different between Windows and the rest of the computing world, Python is designed to be agnostic about the issue. Normally Python has no trouble with varied line endings.
I tried running a Python script on my Mac with a wide variety of line endings and had no problems. Note that I was running my script using the command:
instead of
It might be worth trying both forms to see whether your problem is really Python or whether it is related to your shell/kernel. I know that some environments do have trouble with CRLF endings on the shebang line.
Subversion 会忽略所有行尾 (EOL) 样式。它永远不会碰你的文件,除非你这么告诉它。
如何告诉 Subversion 使用不同的 EOL 样式?
通过将属性 svn:eol-style 设置为有问题的文件:
您可以使用以下命令检查您的文件是否设置了这些属性:
Subversion ignores all end-of-line(EOL)-styles. It will never touch your files, unless you told it so.
How can you tell Subversion to use a different EOL-Style?
By setting property svn:eol-style to files in question:
You can check if your files have these properties set by using:
字节顺序标记?不,需要原生 EOL。
如果 Python 不只是默默地忽略记录终止符样式的常见变化,我会感到惊讶。
shell 或内核可能不会,但是您会看到类似
python: badterpreter
的内容。可能存在 BOM,或字节顺序标记 在文件顶部。使用 od -c events.py 检查它。
在 UTF-8 中不需要也不建议使用 BOM,但出于某种原因,Windows 记事本有一个恼人的习惯,即插入 BOM 作为文件中的第一个字符。
因此,我们在注释中指出
python events.py
工作正常,清楚地表明\r
混淆了内核的脚本执行。 (#!interp [arg]
实际上是由内核处理的,如果失败,shell会尝试它,导致看到错误。)解决方案在属性svn:eol-style
中的 svn 手册。Byte order mark? No, native EOL needed.
I would be surprised if Python didn't just silently ignore the usual variations on record terminator styles.
A shell or kernel might not, but then you would see something like
python: bad interpreter
.It's possible that there is a BOM, or byte order mark at the top of the file. Check it with
od -c events.py
.The BOM is not needed and not recommended in UTF-8, but for some reason Windows Notepad has the annoying habit of inserting one as the very first character in a file.
So, we figured it out in the comments by noting that
python events.py
worked fine, making it clear that the\r
was confusing the kernel's script execution. (#! interp [arg]
is actually processed by the kernel, if that fails, the shell will try it, leading to the error seen.) The solution is in the svn manual in the propertysvn:eol-style
.