473,543 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Win32: Running python programs from a Cygwin shell

I couldn't think of a good solution, and it's hard to Google for...

I write python command-line programs under Win2k, and I use the bash shell
from Cygwin. I cannot use Cygwin's python package because of a binary
module which has to be compiled with Visual C 6.

My scripts start with a '#!/usr/bin/env python' shebang, as God intended.

Now, I assume I can make cmd.exe run foo.py by asociating *.py with the
python interpreter.

However, if foo.py is in my path and I try to run it from bash, what happens
is:

- bash runs env /cygdrive/h/bin/foo.py
- env runs something like 'python /cygdrive/h/bin/foo.py'
- ... and python (which doesn't know about Cygwin-style paths)
croaks on this, of course

Is there a way around this, which doesn't make my script unusable if I moved
it to a Unix box, or ran it from cmd.exe?

I'd prefer not to write wrapper scripts in perl/bash/BAT...

/Jorgen

--
// Jorgen Grahn <jgrahn@ ''If All Men Were Brothers,
\X/ algonet.se> Would You Let One Marry Your Sister?''
Jul 18 '05 #1
2 14343

You could probably write a replacement for env that uses cygpath -m on the
script file. So, your env could call original env then do cygpath -m on the
second paramter. The one thing to think about is what the shell might do to
other file/directory paramters passed to your script. So, does it convert

foo.py .

into:

python /cygdrive/h/bin/foo.py python /cygdrive/h/bin/

Then your python script might not work. So, do you run cygpath on all paramters?

The other option would be to write a special alias/script just for foo.py to
handle on the oddities. This is what I did to get it to execute gvim for
win32. I wrote a shell function to go through all paramters and run cygpath -m
on everything that does not begin with a '-'.

If cygwin normalized everything to the cygpath -m, then I think that the
integration with win32 would be substantially better. So, in the above
scenario, you would have gotten:

python h:/bin/foo.py python h:/bin/

Both cygwin and win32 can handle this. There would be problems when passing to
CMD.exe because it would see the "/"'s and barf. I seem to remember that
therewas some setting to get command.com or cmd.exe to change the switch char.

Jorgen Grahn wrote:
I couldn't think of a good solution, and it's hard to Google for...

I write python command-line programs under Win2k, and I use the bash shell
from Cygwin. I cannot use Cygwin's python package because of a binary
module which has to be compiled with Visual C 6.

My scripts start with a '#!/usr/bin/env python' shebang, as God intended.

Now, I assume I can make cmd.exe run foo.py by asociating *.py with the
python interpreter.

However, if foo.py is in my path and I try to run it from bash, what happens
is:

- bash runs env /cygdrive/h/bin/foo.py
- env runs something like 'python /cygdrive/h/bin/foo.py'
- ... and python (which doesn't know about Cygwin-style paths)
croaks on this, of course

Is there a way around this, which doesn't make my script unusable if I moved
it to a Unix box, or ran it from cmd.exe?

I'd prefer not to write wrapper scripts in perl/bash/BAT...

/Jorgen


Jul 18 '05 #2
JoeSmith <Jo******@IDont WantSpam.bogus. bogusaddress.co m> writes:
You could probably write a replacement for env that uses cygpath -m on
the script file. So, your env could call original env then do cygpath
-m on the second paramter. The one thing to think about is what the
shell might do to other file/directory paramters passed to your
script. So, does it convert

foo.py .

into:

python /cygdrive/h/bin/foo.py python /cygdrive/h/bin/

Then your python script might not work. So, do you run cygpath on all paramters?
(...)
The other option would be to write a special alias/script just for
foo.py to handle on the oddities. This is what I did to get it to
execute gvim for win32. I wrote a shell function to go through all
paramters and run cygpath -m
on everything that does not begin with a '-'.


That sounds very similar to how I have my system setup. I've got bash
functions defined to translate just the first (non-option) argument to
Windows form for use from my bash prompt. I haven't really found it
necessary to process all subsequent arguments (on the rare occasion I
need it I just use `cygpath` myself), and as you point out it might
adversely affect how the commands perceive the input.

I tend to run everything explicitly with "py##" aliases (or python)
from the command line because I bounce between python versions a lot,
so I don't depend on the Windows .py mapping, but presumably a similar
approach could work by an alias for "env".

So for example, my Python relevant section from my .bashrc:

- - - - - - - - - - - - - - - - - - - - - - - - -

#
# Function to pre-process first argument (skipping past options) of a command
# with cygpath to translate paths to for Windows tools.
#
function wpath {
typeset -i cmdstart=1
local cmd=""
local args=""

while arg=${*:$cmdsta rt:1} && [ "${arg:0:1} " == "-" ]; do
cmdstart=cmdsta rt+1
done

if [ $# -ge $cmdstart ]; then
cmd=`cygpath -w ${*:$cmdstart:1 }`
args=${*:$((cmd start+1))}
fi

echo ${*:1:$((cmdsta rt-1))} $cmd $args
}

#
# Function used to execute a command with its first argument translated to
# windows compatible paths.
#
function wcmd {
$1 `wpath ${*:2}`
}

#
# Functions to run explicit Python versions as well as to establish a
# new default path. Automatically use wpath when executing for path names.
#

function py15path
{
export PATH=/c/python/1.5:/c/python/1.5/DLLs:$ORIGPATH
}
function py15
{
PATH=/c/python/1.5:/c/python/1.5/DLLs:$ORIGPATH wcmd python $*
}

function py20path
{
export PATH=/c/python/2.0:/c/python/2.0/DLLs:$ORIGPATH
}
function py20
{
PATH=/c/python/2.0:/c/python/2.0/DLLs:$ORIGPATH wcmd python $*
}

function py21path
{
export PATH=/c/python/2.1:/c/python/2.1/DLLs:$ORIGPATH
}
function py21
{
PATH=/c/python/2.1:/c/python/2.1/DLLs:$ORIGPATH wcmd python $*
}

function py22path
{
export PATH=/c/python/2.2:/c/python/2.2/DLLs:$ORIGPATH
}
function py22
{
PATH=/c/python/2.2:/c/python/2.2/DLLs:$ORIGPATH wcmd python $*
}

function py23path
{
export PATH=/c/python/2.3:/c/python/2.3/DLLs:$ORIGPATH
}
function py23
{
PATH=/c/python/2.3:/c/python/2.3/DLLs:$ORIGPATH wcmd python $*
}

# And establish 'python' to filter through wcmd
alias python='wcmd python'

- - - - - - - - - - - - - - - - - - - - - - - - -
-- David
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2739
by: Nicolas Lehuen | last post by:
Hi, Is it me, or does anyone else get significantly better pystone results under Cygwin versus the standard Win32 build ? CYGWIN 1.5.6 + python 2.3.3-1 : $ time python /usr/lib/python2.3/hotshot/stones.py Pystone(1.1) time for 50000 passes = 2.344 This machine benchmarks at 21331.1 pystones/second 850004 function calls in 4.371 CPU...
2
1885
by: | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A second request for help... Has anyone run the PyEphem ephemeris application under WinXP? http://rhodesmill.org/brandon/projects/pyephem.html I have compiled it with Visual Studio 6 and it crashes Python with a simple
2
2366
by: Christopher Subich | last post by:
From the documentation, it looks like DParser-python will do what I need, but I'm having trouble getting it installed properly. I'm using a win32 environment, with official 2.4 Python binaries. The official DParser for Python win32 binaries (staff.washington.edu/sabbey/dy_parser) fail, saying that I don't have Python 2.3 installed. :/ ...
16
2219
by: Kenneth McDonald | last post by:
For unfortunate reasons, I'm considering switching back to Win XP (from OS X) as my "main" system. Windows has so many annoyances that I can only compare it to driving in the Bay Area at rush hour (OS X is like driving in Portland at rush hour--not as bad, but getting there), but there are really only a couple of things that are really,...
3
1949
by: James Stroud | last post by:
Hello All, I am helping someone write a python script to run their DOS application through an SSH terminal. It seems that this program wants to access a DOS shell and send output there. If running remotely, this causes a problem because it locks up the program. The program seems (to me) to be looking for some non-existant DOS shell to send...
9
2478
by: Endless Story | last post by:
My last version of Python was 2.4, running smoothly on XP with path c: \Python24 - no need even to include this path in PATH; everything worked as it's supposed to at the command line. Just installed Python 2.5, after uninstalling 2.4 (and also 2.3 which had lingered). Now if I open a shell in Windows Python is not available! Here are the...
40
2686
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I'm really annoyed at Python - and not for the reasons already mentioned on this list. Everyone know that programming is supposed to be a dark art, nearly impossible to learn. Computer code is supposed to be something impossible to read to the common person and yet reveal their secrets to the initiated - just remember the code displayed in...
8
1944
by: Grant Edwards | last post by:
When I ssh into a windows machine (running Cygwin sshd), I can invoke python at the shell prompt (you have to use -i option and I don't really understand why). Once it's started there are couple rather odd behaviors: 1) readline support doesn't work. 2) you have call sys.exit() to exit (neither ctrl-D or ctrl-Z work)
2
2107
by: Ant | last post by:
Hi all, There's a sweet combination of tools that you can assemble using Vim, a Python shell (or any shell for that matter) and GNU screen, where you essentially send selected text from Vim to the Python shell. (See http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/ for more details - though I had to tweak the script a bit to...
0
7349
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7735
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7688
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5885
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5271
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
636
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.