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

Discussion in 'Python' started by Mark Lawrence, Sep 8, 2012.

  1. On 07/09/2012 23:04, Gelonida N wrote:
    > 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.
    >


    I hope this helps
    http://stackoverflow.com/questions/...elative-or-absolute-imports-in-python-modules

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Sep 8, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. stacey
    Replies:
    5
    Views:
    3,259
    Nigel Wade
    Dec 3, 2008
  2. Plotinus
    Replies:
    2
    Views:
    298
    Tad McClellan
    Dec 17, 2004
  3. Gelonida N
    Replies:
    0
    Views:
    196
    Gelonida N
    Sep 7, 2012
  4. Gelonida N
    Replies:
    0
    Views:
    180
    Gelonida N
    Sep 9, 2012
  5. Jean-Michel Pichavant
    Replies:
    0
    Views:
    159
    Jean-Michel Pichavant
    Sep 10, 2012
Loading...

Share This Page