Path question

  • Thread starter Geert Vancompernolle
  • Start date
G

Geert Vancompernolle

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?

--
Best rgds,

Geert
________________________________________________

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

Diez B. Roggisch

Geert said:
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
 
G

Geert Vancompernolle

Diez said:
Rename "src" to "myapp", and start main.py from above that. That
should work.



Diez
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!*
 
G

Gabriel Genellina

En Sun, 08 Feb 2009 10:07:40 -0200, Geert Vancompernolle
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!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,252
Messages
2,571,267
Members
47,908
Latest member
MagdalenaR

Latest Threads

Top