Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'sys': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'module)': 0.09; 'skip:/ 10': 0.09; 'try:': 0.09; '~ethan~': 0.09; 'runs': 0.10; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'thread': 0.14; 'filename:fname piece:signature': 0.16; 'nameerror:': 0.16; 'script,': 0.16; 'spurious': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'load': 0.23; 'header:User- Agent:1': 0.23; 'skip:` 20': 0.24; '---': 0.24; "i've": 0.25; 'script': 0.25; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'skip:- 40': 0.29; 'errors': 0.30; 'loads': 0.31; 'subject:some': 0.31; 'skip:- 30': 0.32; 'another': 0.32; '(e.g.': 0.33; 'skip:# 10': 0.33; 'actual': 0.34; 'except': 0.35; 'really': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:173': 0.61; 'soon': 0.63; 'printing,': 0.74; 'act.': 0.84; 'ethan': 0.84; 'furman': 0.84 Date: Mon, 01 Dec 2014 16:45:03 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> <5464F958.4070404@stoneleaf.us> <547CF797.4030407@stoneleaf.us> In-Reply-To: <547CF797.4030407@stoneleaf.us> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="NGw0clSpn4S2psUpb6DVpLaSNG9jEESE3" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417481122 news.xs4all.nl 2912 [2001:888:2000:d::a6]:38844 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81788 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --NGw0clSpn4S2psUpb6DVpLaSNG9jEESE3 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 12/01/2014 03:19 PM, Ethan Furman wrote: >=20 > Well, I've tried this out, and it's only okay. As soon as interesting = things start happening, spurious errors about=20 > thread IDs start printing, which really messes up the user experience [= =2E..] So here's another thought: Have a small python script that loads and run= s the actual script -- here's a POC: --- 8< py_main ------------------------------ #!/usr/bin/env python import sys sys.argv.pop(0) try: execfile except NameError: def execfile(file_name, globals): with open(file_name) as fh: script =3D fh.read() exec(fh, globals) module =3D {} execfile(sys.argv[0], module) module['main']() --------------------------------------------- Put the above somewhere in your path (e.g. /usr/local/bin), make it execu= table, and then instead of shebanging your scripts with `/usr/local/bin/python` you can use `/usr/local/bin/py_main`= , which will load and execute the script, calling script.main as its last act. -- ~Ethan~ --NGw0clSpn4S2psUpb6DVpLaSNG9jEESE3 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAEBAgAGBQJUfQucAAoJENZ7D1rrH75NX/wQAM5gxnlYypXTNhODLa4MCS/2 XYvhMeXBetuflDq5AipdyCKSTedq+W5g0hweKMzT0dWPh5GWHqOWteQFK5XP7Fow zP+i1UP2+BxP/GK7vkdT5uUbHr7RVLHElu0B9k/gv7HvSXBQpBdGacMB5FwvyTq7 nhxgOmDtKjvc4J/GVt560acAn7VJQJX8+783/RzFYpVhFi56f0umk7VRcx2aIc+k 810sFtDS2xzoVq3dtrdJsABc6vJkKFulK5aXcNseADHhDh5HETjSDAxkJmWy2K+Y UrK2m9BJat1Cw69xIUN6gQNPXSmOd1PrD0eDuSpxlEGrkzQfUkiLErqBPuZDouZM cNGZrmgcWLohPdEIurh50z4/hgqWLgI37s3o88lDnAhxNVE7Jyzn49v5wPgkrzF1 WqOpQffM5nELO6/cevoyPohtGGd9No2ycxlIR9U9nZRu2XJQemy3P5+Of7YJzyta pN1D8erKsdkaVZ53R3A2hbQWmPT2XZ5MHGtkaN5XUDbYvyQ3x/PY2qYuNWotXbdt 93Y83kmYoXrsFKqDBDevRlivUO9lVuSalWtrQRkXfxsJOGmsq5qqz0lXEY9Uixxy m9qTQjxNmUVcicyvg5JOUknu1MDo0pbsSu+SzqcCyL9QlWe7o9JizJ/dqhMlj1K0 UpmJi7VS3TBYbMKHBZv3 =VaLv -----END PGP SIGNATURE----- --NGw0clSpn4S2psUpb6DVpLaSNG9jEESE3--