Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41463

Re: The usage of -m option of python

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <modulok@gmail.com>
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; 'string.': 0.04; '"__main__":': 0.07; '__name__': 0.07; 'executed': 0.07; 'used.': 0.07; 'scripts': 0.09; 'python': 0.09; 'imported': 0.09; "system's": 0.09; 'subject:python': 0.11; 'useful,': 0.13; 'library': 0.15; '.py': 0.16; '``__main__``': 0.16; '``__name__``': 0.16; 'executed.': 0.16; 'importable': 0.16; "module's": 0.16; 'scripts.': 0.16; 'subject:usage': 0.16; 'sys.path': 0.16; 'string': 0.17; 'basically': 0.17; 'directory.': 0.17; 'module,': 0.17; 'script.': 0.17; 'tests': 0.18; '>>>': 0.18; 'module': 0.19; 'all,': 0.21; 'import': 0.21; 'parse': 0.22; 'runs': 0.22; 'example': 0.23; 'random': 0.24; 'idea': 0.24; 'command': 0.24; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'thanks!': 0.26; 'forgive': 0.27; 'module.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'regular': 0.27; 'run': 0.28; 'searches': 0.29; "i'm": 0.29; 'maybe': 0.29; 'gets': 0.32; 'file': 0.32; 'anybody': 0.32; 'could': 0.32; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'built-in': 0.35; 'something': 0.35; 'but': 0.36; 'received:74.125': 0.36; 'modules': 0.36; 'useful': 0.36; 'anything': 0.36; 'too': 0.36; 'operating': 0.36; 'execute': 0.37; 'does': 0.37; 'option': 0.37; 'two': 0.37; 'being': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'unit': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'provide': 0.62; 'subject:The': 0.71; 'special': 0.73; 'subject: -': 0.84; 'remember,': 0.93; 'obligation': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=B+8i5XICwVUhphkGajIUP/caLrz5x06kuzpWRZjma+g=; b=WnCoPl8bVlf/NYpziJR66RDCLLUUfD83ZhSukQVtKHndYqPmGg+hvgtYDxHhCBAkwU SOQPzvIDjGo2nl3/labbFUUg1zORJTqM/K8Ohk8+RCRD6AopN5silzr45VtjZeWzx/m/ b0ytAuBbRSPAnZXSJg4rhnQLJUmVitCwfejh66jT8NgUB/rzYRrYOb8u2DsxomGEYX0O qqDd2I1T/yG+xfaBq/3U4tUwHgg0719+bjeBo0e0OqOMn6YfdN8sENsAhSdBzwU+i2uq P3QA430SoU9sPFBQ2d418LmWWGhhVHXe4dCvL9uZJYZJoR3nf5J8Y4urbPI0UELlRsaM gb+w==
MIME-Version 1.0
X-Received by 10.14.211.65 with SMTP id v41mr53306456eeo.33.1363646061635; Mon, 18 Mar 2013 15:34:21 -0700 (PDT)
In-Reply-To <CAPTjJmoK=Y_0E=7Jn0iwj4aPsCFQ8tQJa=bzzLUXR_vfkvLYZg@mail.gmail.com>
References <CABrM6wkuKu9XSbk4JKpEvX4Dfh=n2UKB4UmaRSTyV45Ezd2mYQ@mail.gmail.com> <CAPTjJmoK=Y_0E=7Jn0iwj4aPsCFQ8tQJa=bzzLUXR_vfkvLYZg@mail.gmail.com>
Date Mon, 18 Mar 2013 16:34:21 -0600
Subject Re: The usage of -m option of python
From Modulok <modulok@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3479.1363646069.2939.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363646069 news.xs4all.nl 6983 [2001:888:2000:d::a6]:44227
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41463

Show key headers only | View raw


>> Hi,
>>
>> I don't quite understand how -m option is used. And it is difficult to
>> search for -m in google. Could anybody provide me with an example on
>> how to use this option? Thanks!
>>
>>        -m module-name
>>               Searches sys.path for the named module and runs the
>> corresponding .py file as a script.

> I don't quite understand how -m option is used. And it is difficult to
> search for -m in google. Could anybody provide me with an example on
> how to use this option? Thanks!


Forgive me if I'm too verbose:

When this module is imported via a regular python import statement, the
module's special property ``__name__`` is automatically set to the
name of the module, as a string. For example::

    >>> import random
    >>> random.__name__
    >>> 'random'

If a file is executed as a script from the operating system's command line, the
value of the ``__main__`` property is instead automatically set to the string
"__main__". This is useful because many python modules have a line such as::


    if __name__ == "__main__":
        # Do something different...
        # Maybe run some unit tests or something.
        # Or perhaps parse some command line arguments...

This means you can cd into the python modules directory on your system and
execute one of the built-in modules directly::

    cd /usr/local/lib/python3.3

    python3.3 random.py
        # Tests the random number functions and spits out the result to the
        # console.

We have one file with two behaviors, depending on how it gets executed. You can
use the same concept in your own scripts to have both a command line script
that does something useful, as well as an importable python module to use in
other scripts. Remember, if ``__name__ == "__main__"`` the file is being run as
a script. Otherwise it's being imported as a module.

So what about that ``-m`` option? Having to cd into the python module directory
every time you need to execute a module as a script is cumbersome. The ``-m``
flag is basically a shortcut. These are basically the same::

    cd /usr/local/lib/python3.3
    python3.3 random.py

Same as::

    python3.3 -m random     #<-- I can do this from any directory.


The modules in the standard library have no obligation to do anything useful,
or even anything at all, if execute directly but you can use the same idea to
execute modules as scripts from packages you create.

-Modulok-

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: The usage of -m option of python Modulok <modulok@gmail.com> - 2013-03-18 16:34 -0600

csiph-web