Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Re: Run pyc file without specifying python path ?

 
Thread Tools Search this Thread
Old 08-02-2009, 10:35 AM   #1
Default 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 (appatched) 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



Dave Angel
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Cgi Webserver sagar123 Software 1 04-23-2009 06:32 PM
Monty Python and the Holy Grail SE DVD questions Mark DVD Video 0 04-11-2005 05:29 PM
Monty Python Meaning of Life DVD hotline problems Robert Kaiser DVD Video 3 05-11-2004 06:38 AM
Help regarding damaged Monty Python DVD? Bex DVD Video 5 04-16-2004 02:11 AM
Universal Python Meaning Of Life reply Peter Williams DVD Video 31 09-27-2003 09:13 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46