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

G

Gelonida N

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top