Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28752
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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; 'importing': 0.04; '__name__': 0.07; 'converts': 0.07; 'main()': 0.07; 'valueerror:': 0.07; 'wrapper': 0.07; '__future__': 0.09; 'elegant,': 0.09; 'imported': 0.09; 'imports': 0.09; 'lawrence': 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; 'sys.path': 0.16; 'wrote:': 0.17; 'module': 0.19; 'import': 0.21; 'required.': 0.22; 'example': 0.23; 'this:': 0.23; 'seems': 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; 'am,': 0.27; '???': 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; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'another': 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; 'files': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'most': 0.61; '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 | Gelonida N <gelonida@gmail.com> |
| Subject | Re: how to run python2.6 module with absolute imports stand alone |
| Date | Sun, 09 Sep 2012 04:03:56 +0200 |
| References | <k2dr1c$5i5$1@ger.gmane.org> <k2e2fs$rng$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 | unicorn.dungeon.de |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 |
| In-Reply-To | <k2e2fs$rng$1@ger.gmane.org> |
| 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.400.1347156249.27098.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1347156249 news.xs4all.nl 6861 [2001:888:2000:d::a6]:56322 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:28752 |
Show key headers only | View raw
On 09/08/2012 02:13 AM, Mark Lawrence wrote: > 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 It seems the safest bet seems to be to not use relative imports. What I did in the end however is write a wrapper script, that takes another script as parameter, converts it's path name to a module name, imports it, adapts sys.path and calls main of the imported module. Perhaps a little overkill, but most convenient, as the script can even adapt sys.path prior to importing other files if required. > >
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: how to run python2.6 module with absolute imports stand alone Gelonida N <gelonida@gmail.com> - 2012-09-09 04:03 +0200
csiph-web