English 中文(简体)
mingw + python eagerly translating path
原标题:

I am using:

I have an environment variable looking like an absolute path (/path/to/dir) but I m using it to construct a git URL. At some point, it s getting translated to C:/Program Files/Git/path/to/dir. It seems like Python is at fault:

In a 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 is not translating the path, but Python is?

In a Windows Command Prompt, Python gets it right:

C:>set VAR=/path/to/dir
C:>echo %VAR%
/path/to/dir

C:>python
>>> import os
>>> os.environ[ VAR ]
 /path/to/dir 

Can anyone explain what s going on here? And how can I prevent the translation in a bash shell?

EDIT: I should add that my python script runs on OS X and Windows, so if anyone does have a solution it would be good if worked on both platforms.

最佳回答

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:

$ export test="/bin"
$ python
>>> import os
>>> os.environ["test"]
 /bin 
问题回答

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 FilesGit , 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.

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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签