明格+ python急切地翻译路径
我正在使用:
- Windows XP
- Python 2.6.2(从 python.org 标准安装)
- git 版本 1.6.5.1-preview20091022(从 http://code.google.com/p/msysgit/)
我有一个看起来像绝对路径(/path/to/dir
)的环境变量,但是我用它来构建 git URL。在某些时候,它会被转换为C:/Program Files/Git/path/to/dir
。看来Python有问题:
在git bash shell中:
$ export VAR=/path/to/dir
$ echo $VAR
/path/to/dir
$ python
>>> import os
>>> os.environ['VAR']
'C:/Program Files/Git/path/to/dir'
git bash没有翻译路径,但Python是?
在 Windows 命令提示符中,Python 的表现是正确的:
C:\>set VAR=/path/to/dir
C:\>echo %VAR%
/path/to/dir
C:\>python
>>> import os
>>> os.environ['VAR']
'/path/to/dir'
任何人都可以解释这里发生了什么吗?如何阻止 bash shell 中的翻译?
编辑:我应该补充一点,我的 python 脚本可以在 OS X 和 Windows 上运行,所以如果有人确实有解决方案,那么如果在这两个平台上都能工作,那就太好了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题听起来肯定是由 MSYS 引起的。当 MSYS 进程执行非 MSYS 进程(例如,您的 msysgit bash shell 调用本机 Windows Python)时,将检查参数中是否有任何看起来像绝对 POSIX 路径的内容(例如以单个“/”开头的内容),这些是转换为底层“真实”Windows 路径,以便非 MSYS 程序可以找到它们。出于同样的原因,您的环境变量的内容也可能会发生相同的过程。
这就是为什么删除前导的“/”有效(该值看起来不再像 POSIX 路径),为什么添加一个额外的有效(同上),以及为什么这在 Cygwin 下工作得很好(它不是 MSYS)。我还猜测您将 msysgit 安装在“C:\Program Files\Git”,这就是为什么 MSYS 认为它的“假”POSIX 文件层次结构扎根于此并将其添加到“/path/to/”的前面为你提供目录。
不幸的是,如果这就是解释,那么就没有明确的解决方案。我在尝试通过 ssh 传递远程路径时遇到了类似的问题,但我自己还没有找到任何解决此问题的好方法,根据 http://comments.gmane.org/gmane.comp.gnu.mingw.msys/4511(从 2008 年开始)根本没有明显的修复方法超出了您迄今为止找到的解决方法。如果这对您来说是一个更大的问题,您可能需要在 MinGW-MSYS 邮件列表或错误跟踪器上提出它。根据 Gmane 的讨论,尽管这是一个已知问题,但从未正式报告过。
The problem definitely sounds like it's caused by MSYS. When an MSYS process execs a non-MSYS process (e.g. your msysgit bash shell calling native Windows Python), the arguments are checked for anything that looks like an absolute POSIX path (e.g. things that start with a single '/') and these are translated to the underlying 'real' Windows path so that the non-MSYS program can find them. It's likely that this same process happens to the contents of your environment variables too, for the same reason.
This would be why removing the leading '/' works (the value doesn't look like a POSIX path any more), why adding an extra one works (ditto), and why this works fine under Cygwin (it's not MSYS). I'm also guessing that you installed msysgit at 'C:\Program Files\Git', and that this is why MSYS thinks its 'fake' POSIX file hierarchy is rooted there and adds it to the front of '/path/to/dir' for you.
Unfortunately, if that is the explanation then there isn't a clear solution. I hit a similar issue trying to pass remote paths through ssh and haven't found any good ways round this myself, and according to the discussion at http://comments.gmane.org/gmane.comp.gnu.mingw.msys/4511 (from 2008) there simply isn't an obvious fix beyond the workarounds you've found so far. If this turns into a bigger problem for you, you might want to raise it on the MinGW-MSYS mailing lists or bug tracker. According to the Gmane discussion, it had never been reported formally despite being a known issue.
我的猜测是,这不是 python 的错,而是 git bash shell 的错。
当您查看变量时,也许 git bash shell 正在向您撒谎。
或者,尝试不放置第一个 / 并稍后再次添加(如果未发生翻译)。
如果我尝试使用 cygwin,它会起作用:
My guess would be that this is not python at fault, but the git bash shell.
Maybe the git bash shell is lying to you when you look at the variable.
Or, try to not put the first / and add it again later (if translation does not occurs).
If I try with cygwin, it works:
从 msysgit 获得的控制台可能是根据 git 用户的需求进行修改的,从我的观点来看,它仅对简单任务和访问 git 命令行有用,而不是开发和运行 python 脚本(您正在 shell 中使用适用于 Windows 的 Python 安装)为特定应用程序安装,这听起来不太好)。
您应该安装 Cygwin 和他的 python 包(如果需要,甚至可以安装 git 包)以获得正确的 POSIX 环境并为其准备了二进制文件和库。
The console you get from msysgit is probably modified for git user's needs, from my POV, it is only useful for simple tasks and to access git command line not to develop and run python scripts (you are using a Python installation for Windows in a shell installed for a specific application, that doesn't sound good).
You should install Cygwin and his python package (and even git package if you want) to get a correct POSIX environment with binaries and libraries prepared for it.