473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Project layout / Import files from different subdirectories

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
8 3100
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
2
by: Jignesh | last post by:
Can someone please tell me how to "import" a directory structure into a project in Microsoft Visual C++ .NET 2003. I have an extremeley complex directory structure consisting of thousands of...
9
by: Anubhav Jain | last post by:
Hi, I am having few .net source files(.cs or .vb) and I want to dynamically generate the corresponding .net project file(.csproj or .vbproj) for them without using visual studio.So that I could...
3
by: Frank Millman | last post by:
Hi all I am writing a business/accounting application. Once a user has logged in they are presented with a menu. Each menu option has a description and an associated file name and program name....
30
by: David T. Ashley | last post by:
I have a large project on a Linux box that may eventually have several hundred 'C' source files, which need to be compiled and linked into several executables. Not all of the executables will...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
0
by: ping235 | last post by:
Recently, i am busying myself on a project that support both the Full ..NET Framework 2.0 and .NET Compact Framework 2.0, with the same set of c# code, but have different project properties and...
5
by: ivarnelispam | last post by:
Hello all, I'm starting work on what is going to become a fairly substantial Python project, and I'm trying to find the best way to organize everything. The project will consist of: - A few...
4
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.