Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct path usage in Cygwin : Difference between `python c:\somefile.py` & `python /cygdrive/c/somefile.py`

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

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.

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

like image 664
user Avatar asked Mar 11 '13 08:03

user


3 Answers

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.

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:\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:\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 - 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 - 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 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.

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

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

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.

like image 97
me_and Avatar answered Nov 17 '22 03:11

me_and


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.

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

like image 32
Mike DeSimone Avatar answered Nov 17 '22 04:11

Mike DeSimone


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?

like image 1
wrgrs Avatar answered Nov 17 '22 03:11

wrgrs