在Cygwin中正确的路径使用:' python c:\somefile '之间的区别。py”&“python / cygdrive / c / somefile.py”

时间:2021-04-01 01:05:56

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

我在Windows + Cygwin上使用Django 1.5 & Python 2.7。下面的命令在bash shell中给出一个错误

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

Error:

错误:

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

However this works

但是这个工作

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

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

我在django-admin上看到过很多类似的问题。py错误,但是没有具体的错误。

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

那么python c:\somefile有什么区别呢?py和python / cygdrive / c / somefile。在Cygwin上

3 个解决方案

#1


20  

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调用Windows 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:

当您从Cygwin调用Python时——这是Cygwin Python和Windows Python的情况——您传递的路径将按原样给Python来处理。这意味着Python必须知道如何处理这种路径。例如,如果您通过以下步骤:

  • 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:\ \ \脚本路径。py - Windows Python知道如何处理这个问题,而Cygwin Python可能会感到困惑。也就是说,如果你把这个输入到Cygwin shell中,这些反斜杠很容易被解释为转义字符,它们是否存在取决于一些相对复杂的规则。如果你在玩Cygwin游戏,请避免。

  • '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.

    “C:\ \ \脚本路径。py' -这些引号将被Cygwin的bash shell删除,同时停止将反斜杠作为转义字符,因此这对于从Cygwin调用Windows Python很有效。但是,从Windows命令行或类似命令行运行会导致问题,因为Windows不处理单引号。

  • /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.

    / cygdrive / c /道路/ /脚本。py -这是从任何Cygwin程序(如Cygwin Python)的角度来看的路径。它适用于Cygwin Python,但完全不适用于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.

    C:/道路/ /脚本。py -它不是很有名,但是Windows路径中的反斜线可以用正斜线替换,任何表现良好的Windows程序都可以很好地处理这个问题:它们是Windows明确允许的。特别是,Windows Python对它们没有任何问题,Cygwin的bash shell不会尝试使用前斜杠。因此,您可以在调用Windows Python的任何地方使用这种路径。

  • $(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.

    $(cygpath -w /cygdrive/c/path/to/script.py) - cygpath是一个Cygwin实用程序,用于在不同的路径样式之间进行转换;使用-w选项调用,它将Cygwin路径转换为Windows路径。$(…)表示Cygwin的bash shell应该用运行命令的结果替换文本,因此,如果将此作为从Cygwin bash shell调用的Windows Python的参数,则Windows Python将接收它可以使用的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.

    路径/ /脚本。py -这是相对路径,所以需要从合适的目录调用它。这是第一个为Cygwin和Windows Python工作的版本,并从Cygwin shell和Windows命令行调用。

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

正如您所看到的,路径是复杂的,特别是当您同时使用Cygwin和Windows工具时。

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 shell中的Cygwin Python,只使用Windows命令行中的Windows Python)。如果这是不可能的,那么下一个最好的方法就是在调用Cygwin的Windows程序时使用cygpath—运行cygpath -w /cygdrive/c/Windows会得到c:\ Windows;运行cygpath 'c:\Windows'将打印/cygdrive/c/Windows。

#2


3  

You seem to have Python installed in C: instead of in /usr/bin/python or similar. I'm guessing you installed the Windows port of Python from python.org, and not the Cygwin python package. In this case, the python executable is not using Cygwin (it's running native) and is expecting Windows-formatted path names.

您似乎将Python安装在C中:而不是在/usr/bin/python中或类似的地方。我猜您安装的是Python的Windows端口,而不是Cygwin Python包。在本例中,python可执行文件没有使用Cygwin(它运行的是本机),而是期望使用windows格式的路径名。

Further, your Django seems to be installed in your Windows Python install. Cygwin is probably completely out of the loop.

此外,您的Django似乎安装在您的Windows Python安装中。Cygwin可能完全脱离了循环。

#3


1  

Well, nothing should be different:

好吧,没什么不一样的:

hgs15624@ESCLT0116 ~
$ python /cygdrive/c/test.py
Hello

hgs15624@ESCLT0116 ~
$ python c:/test.py 
Hello

I guess you've looked for odd permissions.

我猜你找过奇怪的权限。

Edit: the below was just referring to a typo in the question.

编辑:以下只是指问题中的错误。

The error says:

这个错误表示:

/cygdrive/c/Python27/Lib/sitepackages/django/bin/django-admin.py

Did you miss the hyphen in site-packages?

你是否错过了网站上的连字符?

#1


20  

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调用Windows 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:

当您从Cygwin调用Python时——这是Cygwin Python和Windows Python的情况——您传递的路径将按原样给Python来处理。这意味着Python必须知道如何处理这种路径。例如,如果您通过以下步骤:

  • 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:\ \ \脚本路径。py - Windows Python知道如何处理这个问题,而Cygwin Python可能会感到困惑。也就是说,如果你把这个输入到Cygwin shell中,这些反斜杠很容易被解释为转义字符,它们是否存在取决于一些相对复杂的规则。如果你在玩Cygwin游戏,请避免。

  • '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.

    “C:\ \ \脚本路径。py' -这些引号将被Cygwin的bash shell删除,同时停止将反斜杠作为转义字符,因此这对于从Cygwin调用Windows Python很有效。但是,从Windows命令行或类似命令行运行会导致问题,因为Windows不处理单引号。

  • /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.

    / cygdrive / c /道路/ /脚本。py -这是从任何Cygwin程序(如Cygwin Python)的角度来看的路径。它适用于Cygwin Python,但完全不适用于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.

    C:/道路/ /脚本。py -它不是很有名,但是Windows路径中的反斜线可以用正斜线替换,任何表现良好的Windows程序都可以很好地处理这个问题:它们是Windows明确允许的。特别是,Windows Python对它们没有任何问题,Cygwin的bash shell不会尝试使用前斜杠。因此,您可以在调用Windows Python的任何地方使用这种路径。

  • $(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.

    $(cygpath -w /cygdrive/c/path/to/script.py) - cygpath是一个Cygwin实用程序,用于在不同的路径样式之间进行转换;使用-w选项调用,它将Cygwin路径转换为Windows路径。$(…)表示Cygwin的bash shell应该用运行命令的结果替换文本,因此,如果将此作为从Cygwin bash shell调用的Windows Python的参数,则Windows Python将接收它可以使用的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.

    路径/ /脚本。py -这是相对路径,所以需要从合适的目录调用它。这是第一个为Cygwin和Windows Python工作的版本,并从Cygwin shell和Windows命令行调用。

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

正如您所看到的,路径是复杂的,特别是当您同时使用Cygwin和Windows工具时。

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 shell中的Cygwin Python,只使用Windows命令行中的Windows Python)。如果这是不可能的,那么下一个最好的方法就是在调用Cygwin的Windows程序时使用cygpath—运行cygpath -w /cygdrive/c/Windows会得到c:\ Windows;运行cygpath 'c:\Windows'将打印/cygdrive/c/Windows。

#2


3  

You seem to have Python installed in C: instead of in /usr/bin/python or similar. I'm guessing you installed the Windows port of Python from python.org, and not the Cygwin python package. In this case, the python executable is not using Cygwin (it's running native) and is expecting Windows-formatted path names.

您似乎将Python安装在C中:而不是在/usr/bin/python中或类似的地方。我猜您安装的是Python的Windows端口,而不是Cygwin Python包。在本例中,python可执行文件没有使用Cygwin(它运行的是本机),而是期望使用windows格式的路径名。

Further, your Django seems to be installed in your Windows Python install. Cygwin is probably completely out of the loop.

此外,您的Django似乎安装在您的Windows Python安装中。Cygwin可能完全脱离了循环。

#3


1  

Well, nothing should be different:

好吧,没什么不一样的:

hgs15624@ESCLT0116 ~
$ python /cygdrive/c/test.py
Hello

hgs15624@ESCLT0116 ~
$ python c:/test.py 
Hello

I guess you've looked for odd permissions.

我猜你找过奇怪的权限。

Edit: the below was just referring to a typo in the question.

编辑:以下只是指问题中的错误。

The error says:

这个错误表示:

/cygdrive/c/Python27/Lib/sitepackages/django/bin/django-admin.py

Did you miss the hyphen in site-packages?

你是否错过了网站上的连字符?