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


Groups > comp.lang.python > #28712

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'wrote:': 0.17; 'import': 0.21; 'example': 0.23; 'this:': 0.23; 'second': 0.24; 'command': 0.24; 'script': 0.24; 'allows': 0.25; 'header:In-Reply-To:1': 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; 'from:addr:yahoo.co.uk': 0.32; '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; 'subject:: ': 0.38; 'mark': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'stand': 0.61; 'helps': 0.63; 'tricks': 0.65; 'gelonida': 0.84; 'ideas.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: how to run python2.6 module with absolute imports stand alone
Date Sat, 08 Sep 2012 01:13:02 +0100
References <k2dr1c$5i5$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-147-18-75.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <k2dr1c$5i5$1@ger.gmane.org>
X-Antivirus avast! (VPS 120907-0, 07/09/2012), Outbound message
X-Antivirus-Status Clean
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.374.1347063112.27098.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347063112 news.xs4all.nl 6859 [2001:888:2000:d::a6]:57425
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28712

Show key headers only | View raw


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/3616952/how-to-properly-use-relative-or-absolute-imports-in-python-modules

-- 
Cheers.

Mark Lawrence.

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


Thread

Re: how to run python2.6 module with absolute imports stand alone Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-08 01:13 +0100

csiph-web