Barak, Ron wrote:
-----Original Message-----
From: Dave Angel [mailto:da...@ieee.org]
Sent: Sunday, August 02, 2009 12:36
To: Barak, Ron
Cc: 'python-list@python.org'
Subject: Re: Run pyc file without specifying python path ?

Barak, Ron wrote:
Hi Dave,

It seems like I don't understand your solution.
I use the (attached) soapAPI.py as the wrapper to parsing.pyc.
However, if I do (for instance):

$ python -u parsing.pyc -U aaa

The last line of the output is (as expected):

return_code: 12 ; params: {'username': 'aaa'}

But, if I try the following:

$ soapAPI.py -U aaa

I don't get this line. Only the output to stderr gets
printed to the screen.
Bye,
Ron.


Hi Ron,

To make it easier for anybody following this thread, let me
post the minimum equivalent source files, inline.

parsing.py:
------------------------------
#!/usr/bin/env python

import sys

def main():
    print >> sys.stderr, "This is stderr output"
    return  5, sys.argv

if __name__ == "__main__":
    return_code, params = main()
    print "return_code:",return_code,"; params:",params
    sys.exit(return_code)
-------------------------------
soapapi.py:
-------------------------------
#!/usr/bin/env python

import sys
import parsing

parsing.main()
------------------------------


When I run soapapi.;py, it indeed prints only the stderr output.

The solution is to move (most or all) of the top-level code
of parsing.py into a main() function.  Since you already have
a main(), I'll rename that, and make a new one that calls it.

new   parsing.py:
-------------------------------
#!/usr/bin/env python

import sys

def innermain():
    print >> sys.stderr, "This is stderr output"
    return  5, sys.argv

def main():
    return_code, params = innermain()
    print "return_code:",return_code,"; params:",params
    sys.exit(return_code)

if __name__ == "__main__":
    main()
-------------------------------

The output is now two lines, one from innermain(), and one
from main().
And it's the same whether the user runs  parsing.py  or   soapAPI.py

To clarify what happened, realize that when the user invokes
parsing.py, the module is considered a script, and gets a
pseudo-name of
"__main__"    When that same module is imported by another one, it is
considered a library module, and gets its own name "parsing"

So any logic that explicitly checks for "__main__" has to
change, because we want identical behavior in the two cases.

DaveA



Hi Dave,

Trying your above scripts, I get other results than you do. E.g.:

$ python parsing.py --help
return_code: 5 ; params: ['parsing.py', '--help']
This is stderr output

$ python soapapi.py --help
This is stderr output

$

So, in my environment (cygwin on win xp, python Python 2.5.2) I'm getting 
different results than you get, namely -  soapapi.py on my environment does not 
show the stdout output.
On which environment did you get identical results from your parsing.py and 
soapapi.py ?

Bye,
Ron.

$ cat parsing.py
#!/usr/bin/env python

import sys

def main():
    print >> sys.stderr, "This is stderr output"
    return  5, sys.argv

if __name__ == "__main__":
    return_code, params = main()
    print "return_code:",return_code,"; params:",params
    sys.exit(return_code)

$ cat soapapi.py
#!/usr/bin/env python

import sys
import parsing

parsing.main()



Please read my message again, especially starting with the words "the solution is." I showed two versions of parsing.py, and you're still using the broken version. I posted both versions, with descriptions of what I changed and why, so that you could see why the change was important.

I'm using sys.version == 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit (Intel)] Running on XP Sp3. And 2.5 will give identical results, though I don't have it installed any more.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to