Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Problems with relative imports and pep 366

Reply
Thread Tools

Re: Problems with relative imports and pep 366

 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      06-01-2010
Gabriele Lanaro wrote:
> I've yet asked this question on SO, I'll copy the contents:
>
> I have a "canonical file structure" like that (I'm giving sensible names
> to ease the reading):
>
> mainpack/
>
> __main__.py
> __init__.py
>
> - helpers/
> __init__.py
> path.py
>
> - network/
> __init__.py
> clientlib.py
> server.py
>
> - gui/
> __init__.py
> mainwindow.py
> controllers.py
>
> In this structure, for example modules contained in each package may
> want to access the helpers utilities through relative imports in
> something like:
>
> # network/clientlib.py
> from ..helpers.path import create_dir
>
> The program is runned "as a script" using the __main__.py file in this
> way:
>
> python mainpack/
>
> Trying to follow the PEP 366 I've put in __main__.py these lines:
>
> ___package___ = "mainpack"
> from .network.clientlib import helloclient
>
> But when running:
>
> $ python mainpack
> Traceback (most recent call last):
> File "/usr/lib/python2.6/runpy.py", line 122, in _run_module_as_main
> "__main__", fname, loader, pkg_name)
> File "/usr/lib/python2.6/runpy.py", line 34, in _run_code
> exec code in run_globals
> File "path/mainpack/__main__.py", line 2, in <module>
> from .network.clientlib import helloclient
> SystemError: Parent module 'mainpack' not loaded, cannot perform relative import
>
> What's wrong? What is the correct way to handle and effectively use
> relative imports?
>
> I've tried also to add the current directory to the PYTHONPATH, nothing
> changes.
>
> link:
> http://stackoverflow.com/questions/2...s-pep-366-work
>
>
>

I'm not using relative imports at all, so I can't help you on that
point, however, did you consider using absolute imports ? The world
becomes so simple with these .

BTW,

___package___ = "mainpack"


looks weird to me, are you sure it would not be better with

__package__ = "mainpack" # only 2 leading/traling underscore


JM

 
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
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Problems with relative imports and pep 366 Gabriele Lanaro Python 0 05-31-2010 06:47 PM
Opinions, please, on PEP 8 and local, 3rd party imports Philip Semanchuk Python 1 10-03-2009 07:02 AM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
Files, directories and imports - relative to the current directoryonly ptn Python 3 03-26-2008 03:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57