2002-08-26 14:11:49 -04:00
|
|
|
|
PEP: 299
|
|
|
|
|
Title: Special __main__() function in modules
|
|
|
|
|
Version: $Revision$
|
|
|
|
|
Last-Modified: $Date$
|
|
|
|
|
Author: Jeff Epler <jepler@unpythonic.net>
|
2006-03-29 14:18:47 -05:00
|
|
|
|
Status: Rejected
|
2002-08-26 14:11:49 -04:00
|
|
|
|
Type: Standards Track
|
2016-07-06 22:37:27 -04:00
|
|
|
|
Content-Type: text/x-rst
|
2002-08-26 14:11:49 -04:00
|
|
|
|
Created: 12-Aug-2002
|
2007-06-19 00:20:07 -04:00
|
|
|
|
Python-Version: 2.3
|
2006-03-29 14:29:43 -05:00
|
|
|
|
Post-History: 29-Mar-2006
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
|
2002-08-26 14:11:49 -04:00
|
|
|
|
Abstract
|
2016-07-06 22:37:27 -04:00
|
|
|
|
========
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
Many Python modules are also intended to be callable as standalone
|
|
|
|
|
scripts. This PEP proposes that a special function called ``__main__()``
|
|
|
|
|
should serve this purpose.
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
2016-07-06 22:37:27 -04:00
|
|
|
|
==========
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
There should be one simple and universal idiom for invoking a module
|
|
|
|
|
as a standalone script.
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
The semi-standard idiom::
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
perform "standalone" functionality
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
is unclear to programmers of languages like C and C++. It also does
|
|
|
|
|
not permit invocation of the standalone function when the module is
|
|
|
|
|
imported. The variant::
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main_function()
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
is sometimes seen, but there exists no standard name for the function,
|
|
|
|
|
and because arguments are taken from sys.argv it is not possible to
|
|
|
|
|
pass specific arguments without changing the argument list seen by all
|
|
|
|
|
other modules. (Imagine a threaded Python program, with two threads
|
|
|
|
|
wishing to invoke the standalone functionality of different modules
|
|
|
|
|
with different argument lists)
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Proposal
|
2016-07-06 22:37:27 -04:00
|
|
|
|
========
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
The standard name of the 'main function' should be ``__main__``. When a
|
|
|
|
|
module is invoked on the command line, such as::
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
python mymodule.py
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
then the module behaves as though the following lines existed at the
|
|
|
|
|
end of the module (except that the attribute __sys may not be used or
|
|
|
|
|
assumed to exist elsewhere in the script)::
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
if globals().has_key("__main__"):
|
|
|
|
|
import sys as __sys
|
|
|
|
|
__sys.exit(__main__(__sys.argv))
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
Other modules may execute::
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
import mymodule mymodule.__main__(['mymodule', ...])
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
It is up to ``mymodule`` to document thread-safety issues or other
|
|
|
|
|
issues which might restrict use of ``__main__``. (Other issues might
|
|
|
|
|
include use of mutually exclusive GUI modules, non-sharable resources
|
|
|
|
|
like hardware devices, reassignment of ``sys.stdin``/``stdout``, etc)
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
2016-07-06 22:37:27 -04:00
|
|
|
|
==============
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
In ``modules/main.c``, the block near line 385 (after the
|
|
|
|
|
``PyRun_AnyFileExFlags`` call) will be changed so that the above code
|
|
|
|
|
(or its C equivalent) is executed.
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Open Issues
|
2016-07-06 22:37:27 -04:00
|
|
|
|
===========
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
* Should the return value from ``__main__`` be treated as the exit value?
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
Yes. Many ``__main__`` will naturally return ``None``, which
|
|
|
|
|
``sys.exit`` translates into a "success" return code. In those that
|
|
|
|
|
return a numeric result, it behaves just like the argument to
|
|
|
|
|
``sys.exit()`` or the return value from C's main().
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
* Should the argument list to ``__main__`` include ``argv[0]``, or just the
|
|
|
|
|
"real" arguments ``argv[1:]``?
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
``argv[0]`` is included for symmetry with ``sys.argv`` and easy
|
|
|
|
|
transition to the new standard idiom.
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
2006-03-29 14:29:43 -05:00
|
|
|
|
Rejection
|
2016-07-06 22:37:27 -04:00
|
|
|
|
=========
|
2006-03-29 14:29:43 -05:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
In a short discussion on python-dev [1], two major backwards
|
|
|
|
|
compatibility problems were brought up and Guido pronounced that he
|
|
|
|
|
doesn't like the idea anyway as it's "not worth the change (in docs,
|
|
|
|
|
user habits, etc.) and there's nothing particularly broken."
|
2006-03-29 14:29:43 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
References
|
2016-07-06 22:37:27 -04:00
|
|
|
|
==========
|
2006-03-29 14:29:43 -05:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
.. [1] Georg Brandl, "What about PEP 299",
|
|
|
|
|
http://mail.python.org/pipermail/python-dev/2006-March/062951.html
|
2006-03-29 14:29:43 -05:00
|
|
|
|
|
|
|
|
|
|
2002-08-26 14:11:49 -04:00
|
|
|
|
Copyright
|
2016-07-06 22:37:27 -04:00
|
|
|
|
=========
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
This document has been placed in the public domain.
|
2002-08-26 14:11:49 -04:00
|
|
|
|
|
|
|
|
|
|
2016-07-06 22:37:27 -04:00
|
|
|
|
..
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
sentence-end-double-space: t
|
|
|
|
|
fill-column: 70
|
|
|
|
|
End:
|