Path question

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Feb 8 15:25:53 EST 2009


En Sun, 08 Feb 2009 10:07:40 -0200, Geert Vancompernolle  
<geert.discussions at gmail.com> escribió:

> Diez B. Roggisch wrote:
>> Geert Vancompernolle schrieb:
>>> Hi,
>>>
>>> I have the following path construction:
>>>
>>> ./src/__init__.py
>>>     /main.py
>>>     /modules/__init__.py
>>>             /application.py
>>>     /ui/__init__.py
>>>        /mainwindow/__init__.py
>>>                   /mainwindow.py
>>>
>>> Now I want to call the method 'MainWindow' in the module 'mainwindow',  
>>> from the module 'application'.
>>>
>>> I'm having the following import statement in 'applications.py':
>>>
>>> from .. ui.mainwindow.mainwindow import MainWindow
>>>
>>> That doesn't work.  I've also tried many other combinations like from  
>>> ..ui.mainwindow...  but none of them work.
>>>
>>> I always get the following error:
>>>
>>> "Attempted relative import beyond toplevel package"
>>>
>>> How can I import the method 'MainWindow' from 'mainwindow' into  
>>> 'application', using the explicit relative import rules?
>>>
>>> Or is there a better way to do this?
>>>
>>
>> Rename "src" to "myapp", and start main.py from above that. That should  
>> work.
>>
>>
>>
>> Diez
>> -- http://mail.python.org/mailman/listinfo/python-list
>>
> That trick did it, thanks!
>
> But I have one more unclarity about relative import rules.  Here  
> <http://docs.python.org/tutorial/modules.html?highlight=modules>, one  
> can find an explanation about modules in the official Python 2.6.1  
> documentation.
>
> In that article, there's an example of importing relative packages (in  
> the section "Packages").
>
> I see the following examples:
>
> from . import echo
> from .. import formats
> from ..filters import equalizer
>
>
> Applying that mechanism on my application, I'm using the following  
> construction (see my initial email):
>
> "from .. ui.mainwindow.mainwindow import MainWindow"
>
> But using "from ..ui.mainwindow.mainwindow import MainWindow" seems to  
> be giving exactly the same result (mind the lacking space between ".."  
> and "ui").
>
> So, what's the difference (if there is one) between "from ..  
> ui.mainwin..." and "from ..ui.mainwin..." (again, mind the difference in  
> space)?

There is none. It's the same as:

 from .    . ui  .mainwindow.  mainwindow      import     mainWindow

Whitespace is irrelevant here. The quoted examples mean:

> from . import echo
import the "echo" module/package from the current package

> from .. import formats
import the "formats" module/package from one level above the current  
package (if it is a package, it's a sibling directory; if it's a module,  
it's in the parent directory).

> from ..filters import equalizer
import only the name "equalizer" from the "filters" module/package,  
located one level above the current package (same note as previous)

> I guess there's a difference, otherwise it would not be given as an  
> example in the Python documentation.

Note that examples 1 and 2 don't have any name in the "from" part; they're  
like a plain "import echo" or "import formats" (but telling also *where*  
to search for them, so it's not the same thing). The target is a  
module/package, as with a plain "import foo".

The third example only imports a name from another place (filters), also  
telling where to search for the module/package.

When you eventually grasp the concept (and I'm sure you will), could you  
think of a better wording/examples to improve the documentation? (I cannot  
see the deficiencies, just because I *already* know what it means!)

-- 
Gabriel Genellina




More information about the Python-list mailing list