Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to run python2.6 module with absolute imports stand alone

Reply
Thread Tools

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

 
 
Mark Lawrence
Guest
Posts: n/a
 
      09-08-2012
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/3...python-modules

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: how to run python2.6 module with absolute imports stand alone Jean-Michel Pichavant Python 0 09-10-2012 09:28 AM
Re: how to run python2.6 module with absolute imports stand alone Gelonida N Python 0 09-09-2012 02:03 AM
how to run python2.6 module with absolute imports stand alone Gelonida N Python 0 09-07-2012 10:04 PM
stand alone executable using pp doesn't stand alone Plotinus Perl Misc 2 12-17-2004 01:09 AM



Advertisments