正确的路径使用Cygwin中:蟒蛇C的`区别:\\ somefile.py`&安培; `蟒蛇/ cygdrive / C / somefile.py` [英] Correct path usage in Cygwin : Difference between `python c:\somefile.py` & `python /cygdrive/c/somefile.py`

查看:275
本文介绍了正确的路径使用Cygwin中:蟒蛇C的`区别:\\ somefile.py`&安培; `蟒蛇/ cygdrive / C / somefile.py`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Django 1.5安培; Python 2.7版的Windows + Cygwin的。下面的命令给我一个错误在bash shell中

I'm using Django 1.5 & Python 2.7 on Windows + Cygwin. The following command gives me an error in bash shell

$ python /cygdrive/c/Python27/Lib/site-packages/django/bin/django-admin.py

错误:

C:\Python27\python.exe: can't open file '/cygdrive/c/Python27/Lib/site-packages/django/bin/django-admin.py': [Errno 2] No such file or directory

不过这个作品

$ python c:/Python27/Lib/site-packages/django/bin/django-admin.py

我见过很多对django-admin.py错误类似的问题,但没有明确这一点。

I've seen lots of similar questions on django-admin.py errors but none specifically about this.

那么,什么是蟒蛇C之间的区别:\\ somefile.py &安培; 蟒蛇/cygdrive/c/somefile.py 在在Cygwin是bash shell

So what's the difference between python c:\somefile.py & python /cygdrive/c/somefile.py in a bash shell on Cygwin

推荐答案

正如其他人所指出的,这里的问题的一部分,是你调用cygwin的版本的Windows的Python。这是要做一件奇怪的事,因为你打奇怪的行为是这样,但它可以与护理工作。

As others have noted, part of the problem here is that you're calling Windows Python from Cygwin. This is an odd thing to do, as you hit strange behaviour like this, but it can work with care.

当您从Cygwin的调用Python - 这是为Cygwin的Python和Python的的Windows的情况下 - 传递路径将被指定为-是Python来处理。这意味着Python的必须知道如何处理样的路径。例如,如果您通过以下内容:

When you call Python from Cygwin - and this is the case for both Cygwin Python and Windows Python - the path you pass will be given as-is to Python to handle. That means Python must know how to handle that kind of path. For example, if you pass the following:


  • C:\\路径\\为\\ script.py - 视窗Python知道如何处理这个问题,而Cygwin的Python是可能会感到困惑。也就是说,如果你键入此成Cygwin的壳,去掉所有的反斜线都容易成为PTED为转义字符间$ P $,以及他们是否与否取决于一些相对复杂的规则。避免使用,如果你在Cygwin中玩耍。

  • C:\path\to\script.py - Windows Python knows how to handle this, while Cygwin Python is likely to get confused. That said, if you type this into a Cygwin shell, those backslashes are liable to be interpreted as escape characters, and whether they are or not depends on some relatively complicated rules. Avoid, if you're playing around in Cygwin.

'C:\\路径\\为\\ script.py - 这些报价将在Cygwin的bash shell中被剥离,而停止反斜杠被视为逃逸字符,因此这将正常工作从Cygwin的调用Windows的Python。从Windows命令行运行或类似的会造成问题,虽然,因为Windows不处理单引号。

'C:\path\to\script.py' - Those quotes will be stripped by Cygwin's bash shell, while stopping the backslashes being taken as escape characters, so this would work fine for calling Windows Python from Cygwin. Running from a Windows command line or similar will cause problems, though, as Windows doesn't handle single quotes.

/cygdrive/c/path/to/script.py - 这是从任何Cygwin的程序来看,如Cygwin的Python的路径。它完全适用于Cygwin的Python的,但不是在所有针对Windows的Python。

/cygdrive/c/path/to/script.py - This is the path from the point of view of any Cygwin program, such as Cygwin Python. It works perfectly for Cygwin Python, but not at all for Windows Python.

C:/path/to/script.py - 这不是很出名,但在Windows路径反斜杠可以用斜杠来代替,任何乖巧的Windows程序应该处理得很好:他们明确由Windows允许的。特别是中,Windows Python有与他们没有任何问题,和Cygwin的bash shell中不会试图要巧用斜杠。因此,您可以使用这种路径的任何地方,你会调用Windows的Python。

C:/path/to/script.py - It's not very well known, but backslashes in Windows paths can be replaced with forward slashes, and any well-behaved Windows program should handle that just fine: they're explicitly allowed by Windows. In particular, Windows Python has no problem with them whatsoever, and Cygwin's bash shell won't try to be clever with the forward slashes. You can therefore use this kind of path anywhere you would call Windows Python.

$(cygpath -w /cygdrive/c/path/to/script.py) - cygpath 是一个Cygwin的实用工具不同的路径方式之间的转换;所谓以 -w 选项,将其转换Cygwin的路径到Windows路径。在 $(...)表示,Cygwin的bash shell中应更换与运行命令的结果文本,因此,如果这被赋予作为参数到Windows Python从所谓的Cygwin的bash shell中时,Windows会的Python收到它可以使用Windows路径。

$(cygpath -w /cygdrive/c/path/to/script.py) - cygpath is a Cygwin utility to convert between different path styles; called with a -w option, it converts a Cygwin path to a Windows path. The $(...) indicated that Cygwin's bash shell should replace the text with the result of running the command, so if this were given as the argument to Windows Python called from a Cygwin bash shell, Windows Python would receive a Windows path it can use.

路径/到/ script.py - 这是一个相对路径,所以你需要从一个合适的目录调用它。它是将两个Cygwin的和Windows的Python工作的第一个版本,并且无论从C​​ygwin的外壳和Windows命令行调用。

path/to/script.py - This is a relative path, so you need to call it from a suitable directory. It's the first version that will work for both Cygwin and Windows Python, and called from both a Cygwin shell and a Windows command line.

正如你所看到的,路径是复杂的,特别是当你使用Cygwin和Windows工具结合在一起。

As you can see, paths are complicated, particularly when you're using Cygwin and Windows tools together.

作为一般规则,最安全的是要坚持使用一个或另一个可能的话(所以只能从Cygwin的外壳使用Cygwin的Python,只有从Windows命令行使用Windows的Python)。如果这是不可能的,未来最好是使用 cygpath 每当你调用从Cygwin的或其他方式Windows程序 - 运行 cygpath - W / cygdrive / C /视窗会给 C:\\ WINDOWS ;运行 cygpath'C:\\ WINDOWS将打印 / cygdrive / C /视窗

As a general rule, the safest thing is to stick to using one or the other if possible (so only use Cygwin Python from a Cygwin shell, and only use Windows Python from a Windows command line). If that's not possible, the next best is to use cygpath whenever you're calling a Windows program from Cygwin or the other way around - running cygpath -w /cygdrive/c/Windows will give c:\windows; running cygpath 'c:\Windows' will print /cygdrive/c/Windows.

这篇关于正确的路径使用Cygwin中:蟒蛇C的`区别:\\ somefile.py`&安培; `蟒蛇/ cygdrive / C / somefile.py`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆