Project layout / Import files from different subdirectories

M

Markus Mayer

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
 
D

Diez B. Roggisch

Markus said:
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
 
S

Steve Holden

Markus said:
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
 
M

Markus Mayer

Steve said:
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
 
A

Almar Klein

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
 
S

Steve Holden

Almar said:
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
 
A

Almar Klein

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
 
G

Gabriel Genellina

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/pylab_works/pw_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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top