Path question

Geert Vancompernolle geert.discussions at gmail.com
Sun Feb 8 07:07:40 EST 2009


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)?

I guess there's a difference, otherwise it would not be given as an example in the Python documentation.  But then, why is the application (at first sight) behaving the same and also working fine?


-- 
Best rgds,

Geert
________________________________________________

*Use EcoCho <http://www.ecocho.com>: environmentally friendly search the 
internet!*



More information about the Python-list mailing list