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


Groups > comp.lang.python > #28711 > unrolled thread

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

Started byGelonida N <gelonida@gmail.com>
First post2012-09-08 00:04 +0200
Last post2012-09-08 00:04 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

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

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

FromGelonida N <gelonida@gmail.com>
Date2012-09-08 00:04 +0200
Subjecthow to run python2.6 module with absolute imports stand alone
Message-ID<mailman.372.1347055487.27098.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web