user2023370
user2023370

Reputation: 11056

Cygwin paths for Windows python

I have access to a machine with a minimal cygwin installation, and the Windows version of python. I need to run some python scripts there, however python requires Windows paths. I can use cygpath -w, on the arguments that I provide, however further unix/cygwin paths are included in numerous other scripts which are subsequently invoked.

Is there a way to tell Windows python to accept unix/cygwin paths?

Upvotes: 3

Views: 4253

Answers (2)

Alex Kosh
Alex Kosh

Reputation: 2554

Create a file named wpython and save it somewhere for example in /bin:

#!/bin/bash

path=$1
shift # Remove filepath from other args to pass them further
python.exe $(cygpath -w $path) $@ # Call python.exe with Windows path and passed args

So then use it like this:

#!/usr/bin/env wpython
print(123)

Don't forget to make both file executable with: chmod +x filename

P.S.: Soultion based on this blog post.

Upvotes: 1

ak2
ak2

Reputation: 6778

No, Windows python has no suport for Cygwin paths, but Cygwin does have its own Python. If you can't add that to the existing Cygwin install, you might want to consider doing a user-specific Cygwin install into a directory that you're allowed to write to.

There is a way to obtain limited Cygwin path support for Windows programs, although I suspect this isn't an option for you either: install Cygwin into C:\, so that a Cygwin /path is equivalent to C:\path. This relies on the fact that the Windows API (albeit not all Windows programs) accepts both backslashes and slashes as path separators, and that it considers absolute paths without drive letter as referring to the system drive (i.e. C:).

Obviously this won't work for Cygwin paths that point to other drives (via the Cygwin mount table). It also won't work for any programs that use / (rather than -) to introduce options, which includes most built-in Windows command-line tools. But it does usually work for cross-platform tools such as Python.

Yet another option is to use MSYS instead of Cygwin, which is a fork of an old Cygwin version, whereby its most distinctive feature is that it automatically translates POSIX paths to Windows paths when invoking Windows programs. Note however, that this approach has its pitfalls too, because it isn't always clear whether an argument is a path or not. Hence, sometimes it will fail to translate a path or wrongly change an argument that isn't a path.

Upvotes: 2

Related Questions