[pydotorg-www] Wiki page documenting running standard modules as scripts

Chris Angelico rosuav at gmail.com
Wed Jan 13 11:44:05 EST 2016


On Thu, Jan 14, 2016 at 3:21 AM, Alan Evangelista
<alanoe at linux.vnet.ibm.com> wrote:
> On 01/13/2016 02:15 PM, John Whitlock wrote:
>
> I've found that experienced Python developers are unaware that some standard
> modules can be run as scripts (...) I find these useful every few months,
> but they are also hard to discover.
>
>
> What is the usefulness of running a standard Python module as a script?
>

It depends on the module. Some are designed to be equally easily
imported or run with "python -m modulename", and that's a Good Thing.
Example:

rosuav at sikorsky:~$ python3 -m timeit -s "def foo(): return sum(i+i for
i in range(1000))" "foo()"
10000 loops, best of 3: 72.6 usec per loop
rosuav at sikorsky:~$ python3 -m timeit -s "def foo(): return sum(i*2 for
i in range(1000))" "foo()"
10000 loops, best of 3: 73.3 usec per loop
rosuav at sikorsky:~$ python3
Python 3.6.0a0 (default:1118dfcbcc35, Jan  6 2016, 15:05:01)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.repeat(setup="def foo(): return sum(i*2 for i in range(1000))",stmt="foo()",number=10000)
[0.809292299032677, 0.7410961069981568, 0.7404299289919436]
>>>

It's a way for a tool to have multiple interfaces - module or command-line tool.

ChrisA


More information about the pydotorg-www mailing list