I'm a bit lost when it comes to structuring my project(s). I try to structure things in ways that make sense, but always end up restructuring the whole thing at least twice per day. Granted, my projects aren't very big, but I would love to not have to restructure everything and just settle on something for once.
I'll describe my current program to try to make sense of things. It's a graphical program with a database backend for calculating the price of sails. Not everything is written yet, but the user will be able to select a sail category and model from two dropdown menus. Depending on the category-model combination, the program will present checkboxes and spinboxes. These checkboxes and spinboxes, when changed, draw information from a database and present the price of having that checkbox checked or having a certain number (e.g., area in square metres) in the spinbox.
In its current form, the project looks like this:
COPYING
README.md
SailQt.pyw (Should program be called from here ...)
sailqt/
__init__.py (This holds a __version__ string)
SailQt.pyw (... or here?)
gui/
__init__.py
MainWindow.py (This needs access to a __version__ string)
MainWindow_rc.py
OptionsWidget.py
ui_MainWindow.py
ui_OptionsWidget.py
resources/
__init__.py
database.db
generate_gui.py
MainWindow.ui
MainWindow.qrc
OptionsWidget.ui
icons/
logo.png
To further clarify. resources
holds all .ui
files made in Qt Designer. They are XML files that describe the GUI. They can be converted to Python scripts with a terminal tool, which I've embedded into generate_gui.py
. The same goes for .qrc
files. generate_gui.py
places the autogenerated files in the gui
folder with either prefix ui_
or suffix _rc
. database.db
is currently empty, but will eventually be used to hold prices and everything.
MainWindow.py
and OptionsWidget.py
are Python files that hold objects of the same name, minus the .py
suffix. MainWindow
holds OptionsWidget
in its display surface. Both objects use their corresponding ui
and rc
files.
SailQt.pyw
is the file that makes a MainWindow
instance, tells it to show itself, and then tells (Py)Qt to enter its loop and take over from there. It's basically much like a .exe
file of a lot of graphical applications in that it's a small file that gets the program running.
My initial guess was to place SailQt.pyw
inside the sailqt
folder. But then MainWindow.py
suddenly needed access to a __version__
string. The only way I could figure out how to achieve that was to move SailQt.pyw
to the root folder of my project, and to let MainWindow.py
import sailqt.__version__
. But considering that was the nth time I had to shuffle things around and redo lines in most files to account for that tiny shuffle, I decided to just ask here.
My questions are fairly clear:
- How are, in general, Python projects structured? This pydoc link was helpful, but that seems more like a module to me than something that is actually executed by a user.
- Did I get the above structuring right?
- Bonus points for answering this, as it's a bit off-topic. How come I can do
import os
and then do stuff likeos.system("sudo rm -rf /")
, but I can't do stuff likeimport sailqt
and then dosailqt.gui.generate_gui.generate()
?