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


Groups > comp.lang.python > #28711

how to run python2.6 module with absolute imports stand alone

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 <python-python-list@m.gmane.org>
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; '__name__': 0.07; 'main()': 0.07; 'valueerror:': 0.07; '__future__': 0.09; 'elegant,': 0.09; 'imported': 0.09; 'imports': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'subject:module': 0.09; 'advance': 0.10; 'def': 0.10; "'__main__':": 0.16; 'funcname': 0.16; 'main():': 0.16; 'parameter,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:run': 0.16; 'import': 0.21; 'example': 0.23; 'this:': 0.23; 'second': 0.24; 'command': 0.24; 'script': 0.24; 'allows': 0.25; 'header:User- Agent:1': 0.26; '???': 0.27; 'module.': 0.27; 'header:X -Complaints-To:1': 0.28; 'run': 0.28; 'attempted': 0.29; 'complain': 0.29; 'idea,': 0.29; 'mode.': 0.29; 'probably': 0.29; 'relative': 0.30; 'basic': 0.30; 'function': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'thanks': 0.34; 'path': 0.35; 'skip:. 20': 0.35; 'received:org': 0.36; 'but': 0.36; 'alone': 0.36; 'modules': 0.36; 'subject:with': 0.36; 'test': 0.36; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'stand': 0.61; 'tricks': 0.65; 'ideas.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Gelonida N <gelonida@gmail.com>
Subject how to run python2.6 module with absolute imports stand alone
Date Sat, 08 Sep 2012 00:04:28 +0200
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host unicorn.dungeon.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0
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.372.1347055487.27098.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347055487 news.xs4all.nl 6940 [2001:888:2000:d::a6]:48563
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28711

Show key headers only | View raw


Hi,

many of my modules contain following section at the end


def main():
     do_something()
if __name__ == '__main__':
     main()

This allows me to run some basic example code
or some small test in a stand alone mode.


My new modules contain following line at the beginning:

from __future__ import absolute_import


I like this:
- It can reduce import name conflicts
- and second it allows 'relative' imports like
    from .othermodule import funcname
    from ..mod_one_level_higher import fdfsd


However If I try to run such a script from the command line it will now 
complain with

ValueError: Attempted relative import in non-package

Any tricks to work around this ???

The only idea, that I have is to have a script, that would take my 
modulename or path name as parameter, and try to import it and then call 
the main function of the imported module.


Not very elegant, but probably functional.

Thanks in advance for any other suggestions / ideas.

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


Thread

how to run python2.6 module with absolute imports stand alone Gelonida N <gelonida@gmail.com> - 2012-09-08 00:04 +0200

csiph-web