One of the biggest pains of having to deal with python development in Windows is using the right runtime when running your script. Many scripts will run fine in the python interpreter bundled with Cygwin, but many require the official Python build for windows which understands paths etc correctly.
The problem then, is executing the right interpreter. It quickly becomes tiring to manually type
cmd /c C:\\Python25\\python.exe myscript.py arg1 arg2 arg3
.. for every time you want to invoke a script. Furthermore, it becomes very tedious when you start using bash tab completion for paths, only to discover windows python doesn’t understand cygwin paths. So you start having to use cygpath, or worse hard-coding all your paths:
cmd /c C:\\Python25\\python.exe myscript.py `cygpath -w ~/some_relevant_file.txt` #or cmd /c C:\\Python25\\python.exe myscript.py C:\\Users\\marshall\\some_relevant_file.txt
To solve this problem, I’ve written a small shell wrapper that auto-converts any unix style paths, and calls into python.exe directly. Feel free to use this for your own environment (download here):
#!/bin/sh
PYTHONHOME=C:\\Python25
newargs=
while [ $# -ne 0 ]; do
arg=$1
index=`expr index "$arg" '/' | tr -d '\n'`
if [ "$index" -ne "0" ]; then
# convert to windows path
newarg=`cygpath -w "$arg" | tr -d '\n'`
newargs="$newargs \"$newarg\""
else
newargs="$newargs \"$arg\""
fi
shift
done
command="cmd /c \"$PYTHONHOME\\python.exe\" $newargs"
eval $command
You’ll need to set PYTHONHOME to your windows python directory. Save the script as /usr/bin/python (move the current /usr/bin/python to /usr/bin/python_cyg or something similar), and you should be able to use #!/usr/bin/python in any of your python scripts, and execute them just like any other executable. For reference, now I can do:
./myscript.py ~/some_relevant_file.txt
viagra how it works Cheap Cheap Viagra herbal viagra forums
snorting viagra health Buy Viagra Order Viagra viagra patent levitra
viagra anxiety; Viagra For Women taking viagra woman
cheap gerneric viagra Viagra Rx generic viagra levitra and cialis pills
can i take viagra! Price Of Viagra viagra how it works
herbal viagra forums Acheter Cialis France snorting viagra health
viagra patent levitra Cialis Generic India viagra anxiety;
taking viagra woman Cheap Viagra Canada cheap gerneric viagra
generic viagra levitra and cialis pills Viagra 34434 Buy can i take viagra!
viagra how it works Levitra Commercial herbal viagra forums
snorting viagra health Cialis Stories viagra patent levitra
viagra anxiety; Buying Viagra taking viagra woman
cheap gerneric viagra Female Ingestion Of Viagra generic viagra levitra and cialis pills
can i take viagra! Buy Cialis In The Uk viagra how it works
herbal viagra forums Viagra For Sale Houston Tx snorting viagra health
viagra patent levitra Buy Viagra Uk viagra anxiety;
taking viagra woman Cialis Anwendung cheap gerneric viagra
generic viagra levitra and cialis pills Viagra Prescription Uk can i take viagra!
viagra how it works Sublingual Cialis herbal viagra forums
snorting viagra health Viagra No Prior Prescription viagra patent levitra
viagra anxiety; Herbal Cialis taking viagra woman
cheap gerneric viagra Is Viagra Professional Real generic viagra levitra and cialis pills
can i take viagra! Aspirin And Viagra viagra how it works
herbal viagra forums 20mg Cialis snorting viagra health
viagra patent levitra Sublingual Viagra viagra anxiety;
taking viagra woman Viagra Times cheap gerneric viagra
generic viagra levitra and cialis pills Viagra 34434 can i take viagra!
viagra how it works Snorting Viagra herbal viagra forums
snorting viagra health Viagra Online Overnight viagra patent levitra
viagra anxiety; Viagra On-line taking viagra woman
cheap gerneric viagra Viagra Litigation generic viagra levitra and cialis pills
can i take viagra! White Rabbit And Viagra viagra how it works
herbal viagra forums Online Generic Cialis snorting viagra health
viagra patent levitra Cialis Uk Chemist viagra anxiety;
taking viagra woman Cheap Viagra Sales cheap gerneric viagra
generic viagra levitra and cialis pills Cialis Drug Prescription can i take viagra!
viagra how it works Super Viagra Pro herbal viagra forums
snorting viagra health Cialis Viagra viagra patent levitra
viagra anxiety; Mexican Rx Cialis Low Priced taking viagra woman
cheap gerneric viagra New Viagra generic viagra levitra and cialis pills
can i take viagra! Cheapest Viagra Prices viagra how it works
herbal viagra forums Cialis In Uk snorting viagra health
viagra patent levitra Levitra viagra anxiety;
taking viagra woman Cialis Pen Promotional Item cheap gerneric viagra
generic viagra levitra and cialis pills Herbal Viagra Australia can i take viagra!
viagra how it works Viagra Max Complaints herbal viagra forums
snorting viagra health Levitra Medicine viagra patent levitra
viagra anxiety; Natural Cialis taking viagra woman
cheap gerneric viagra Is Viagra Safe For Women generic viagra levitra and cialis pills
can i take viagra! Cheapest Viagra Ever viagra how it works
herbal viagra forums Overnight Cialis snorting viagra health
viagra patent levitra Viagra Sales viagra anxiety;
taking viagra woman No Prescription Viagra cheap gerneric viagra
generic viagra levitra and cialis pills Online Viagra Sales can i take viagra!
viagra how it works Can Viagra Cause Restless Leg Syndrome herbal viagra forums
snorting viagra health Women Take Viagra viagra patent levitra
viagra anxiety; Cheapest Price For Cialis taking viagra woman
cheap gerneric viagra Can Women Take Mens Viagra generic viagra levitra and cialis pills
can i take viagra! Viagra Mailing List viagra how it works
herbal viagra forums Genuine Viagra snorting viagra health
viagra patent levitra Viagra Logo viagra anxiety;
taking viagra woman Buy Viagra In Uk cheap gerneric viagra
generic viagra levitra and cialis pills Viagra Price Strategy can i take viagra!
viagra how it works Cialis Without A Prescription herbal viagra forums
snorting viagra health Viagra Cialis Levitra viagra patent levitra
viagra anxiety; Can Cialis Cut In Half taking viagra woman
cheap gerneric viagra Viagra Manufacturer generic viagra levitra and cialis pills
can i take viagra! Buy Viagra Online Without Prescription viagra how it works
herbal viagra forums Acheter Du Viagra snorting viagra health
viagra patent levitra Cialis Fedex Overnight Shipping viagra anxiety;
taking viagra woman 18 Takes Viagra cheap gerneric viagra
generic viagra levitra and cialis pills Cialis Cheapest Lowest Price can i take viagra!
viagra how it works Viagra Cialis Comparison herbal viagra forums
snorting viagra health Viagra For Sale Without A Prescription viagra patent levitra
viagra anxiety; Buy Viagra Ups taking viagra woman
cheap gerneric viagra Viagra Perscription Online generic viagra levitra and cialis pills
can i take viagra! Buy Viagra In England viagra how it works
herbal viagra forums Impotence Viagra snorting viagra health
viagra patent levitra Buying Viagra Online In Britain viagra anxiety;
taking viagra woman Viagra Generic Brand cheap gerneric viagra
generic viagra levitra and cialis pills Cialis can i take viagra!
viagra how it works Cheap Viagra herbal viagra forums
snorting viagra health Soft Tabs Viagra viagra patent levitra
viagra anxiety; Cheap Levitra taking viagra woman
cheap gerneric viagra Chewable Viagra generic viagra levitra and cialis pills
can i take viagra! Levitra Male Enhancement viagra how it works
herbal viagra forums Mexican Rx Cialis Low Price snorting viagra health
viagra patent levitra Viagra Racing Heart viagra anxiety;
5 Responses to Using win32 python from cygwin
Jonathan Hartley
July 6th, 2009 at 4:57 pm
A quantum of genius. Thank you.
Marshall
July 7th, 2009 at 4:08 am
Welcome! I figured this might be useful to others
As a side note, you can basically take this script as-is and use it for any win32 command you want to call directly from cygwin (just change the command string at the bottom). Enjoy!
Dave
August 13th, 2009 at 6:44 pm
Works for me. I used the same method for my perl stuff too. Haven’t been able to break it. Thanks.
AnandAnand
October 18th, 2009 at 2:21 am
Maybe an easier way is to just copy the python executable to system32 folder — cygwin seems to recognize this automatically!
Mike Pennington
October 23rd, 2009 at 8:15 am
This is great info… I like the bash script, but it doesn’t handle standalone “python” calls into the interpreter. Right now, I have to remember to type “python -i” when I call into the interpreter… btw, if you haven’t already noticed, the win32 interpreter hangs if you hit as you would normally under cygwin. I’ve found that ‘quit()’ is a great cross-platform workaround to get out of the interpreter… works everywhere I’ve tried it (Windows, BSD, Linux, Cygwin).