By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

Project layout / Import files from different subdirectories

P: n/a
Hi folks.

I'm new to python and have a slight problem importing - or maybe
understanding - modules. I'm writing a GUI application using Qt4 and
wanted to separate the business from the view logic. So I have my folder
structure as following:

project/ main.py
important.py

project/ gui/ __init__.py
mainwindow.py
anotherwindow.py

Now I can import mainwindow etc. from main and important, but how do I
do it the other way round?

Also, is there maybe a better project layout? I couldn't find anything
useful on it asking Dr. Google.

Best regards,
Markus

--
PGP/GPG key 0x2EB39BF9
--
--
PGP/GPG key 0x2EB39BF9
Nov 11 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Markus Mayer schrieb:
Hi folks.

I'm new to python and have a slight problem importing - or maybe
understanding - modules. I'm writing a GUI application using Qt4 and
wanted to separate the business from the view logic. So I have my folder
structure as following:

project/ main.py
important.py

project/ gui/ __init__.py
mainwindow.py
anotherwindow.py

Now I can import mainwindow etc. from main and important, but how do I
do it the other way round?
By placing a __init__.py into project, and then

import project.main
import project.gui.mainwindow
Diez
Nov 11 '08 #2

P: n/a
Diez B. Roggisch schrieb:
>
By placing a __init__.py into project, and then

import project.main
import project.gui.mainwindow
Diez
Ouch. Thanks.

Markus

--
PGP/GPG key 0x2EB39BF9
Nov 11 '08 #3

P: n/a
Markus Mayer wrote:
Diez B. Roggisch schrieb:
>By placing a __init__.py into project, and then

import project.main
import project.gui.mainwindow
Diez

Ouch. Thanks.
If you want shorter names in your main code, of course, you can use

import project.main as main
import project.gui.mainwindow as window

or somethihg similar.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 11 '08 #4

P: n/a
Steve Holden schrieb:
If you want shorter names in your main code, of course, you can use

import project.main as main
import project.gui.mainwindow as window

or somethihg similar.

regards
Steve
Yeah, I was going with the "from x import y" scheme by now, didn't know
"as" was available as well. Perfect, that makes it a bit easier.

Thanks!
Markus

--
PGP/GPG key 0x2EB39BF9
Nov 11 '08 #5

P: n/a
If your main file is in the root of the project, you can just
use absolute imports. So you can use gui.anotherwindow
or project.important from all files.

I'm not sure this is good practice though...

I was first under the impression that you can always import
modules that are in your current working dir. But this seems
to not always work...

Almar
2008/11/11 Jeremiah Dodds <je************@gmail.com>:
>

On Tue, Nov 11, 2008 at 7:08 AM, Markus Mayer <co**@organisati.onwrote:
>>
Steve Holden schrieb:
If you want shorter names in your main code, of course, you can use

import project.main as main
import project.gui.mainwindow as window

or somethihg similar.

regards
Steve


Are explicit relative imports applicable here? Could he do a

from .. import project.main as main

without adding another __init__.py?

Or do I have my head screwed on funny?

--
http://mail.python.org/mailman/listinfo/python-list

Nov 11 '08 #6

P: n/a
Almar Klein wrote:
If your main file is in the root of the project, you can just
use absolute imports. So you can use gui.anotherwindow
or project.important from all files.

I'm not sure this is good practice though...

I was first under the impression that you can always import
modules that are in your current working dir. But this seems
to not always work...
It works when the program you are executing is in the current working
directory, because Python always puts the directory containing the
program you are executing (not the current working directory) on the path.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 11 '08 #7

P: n/a
It works when the program you are executing is in the current working
directory, because Python always puts the directory containing the
program you are executing (not the current working directory) on the path.
Aha, that makes sense.
I also found with a quick test that importing a module from the current dir
works because '' is part of sys.path. I'm again amazed how beautifull it is
all put together :)

Cheers,
Almar
Nov 11 '08 #8

P: n/a
En Tue, 11 Nov 2008 09:29:44 -0200, Stef Mientki <st**********@gmail.com>
escribió:
On Tue, Nov 11, 2008 at 8:46 AM, Markus Mayer <co**@organisati.onwrote:
>I'm new to python and have a slight problem importing - or maybe
understanding - modules. I'm writing a GUI application using Qt4 and
wanted to separate the business from the view logic. So I have my folder
structure as following:

project/ main.py
important.py

project/ gui/ __init__.py
mainwindow.py
anotherwindow.py

Now I can import mainwindow etc. from main and important, but how do I
do it the other way round?

Also, is there maybe a better project layout? I couldn't find anything
useful on it asking Dr. Google.


Since a couple of days,
(good to know it's not a long established practice!)
I use this construct and it seems to work quite well,
http://mientki.ruhosting.nl/data_www...importing.html
uhm... I don't think extending sys.path to include every package is a good
idea. You're basically flattening the directory structure. Just put all
your stuff in a single directory if this is what you want -- if that's not
what you want, it is what you are actually doing, anyway.
Among other nasty things, you may end up having multiple copies of the
same module, when imported using different names.

--
Gabriel Genellina

Nov 15 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.