Consider your last question first, because it is most important for structuring python projects. Once you figure out how to import imports into your project correctly, the rest becomes much easier to handle.
The main thing is to understand that the directory of the current script run is automatically added to the beginning of sys.path . Therefore, if you place the main.py script (what you are currently calling SailQt.pyw ) outside of your package in the top-level container directory, this ensures that importing packages will always work no matter where the script is executed from.
Thus, the minimum starting structure may look like this:
project/ main.py package/ __init__.py app.py mainwindow.py
Now, since main.py should be outside the top-level python package directory, it should contain only minimal code (enough to run the program). Given the above structure, this will mean nothing more:
if __name__ == '__main__': import sys from package import app sys.exit(app.run())
The app module will contain most of the actual code needed to initialize the program and configure gui, which will be imported as follows:
from package.mainwindow import MainWindow
and the same form of a complete import description can be used from anywhere in the package. So, for example, with this somewhat more complex structure:
project/ main.py package/ __init__.py app.py mainwindow.py utils.py dialogs/ search.py
The search module can import a function from the utils module as follows:
from package.utils import myfunc
For the specific issue of accessing the __version__ line: for PyQt, you can put the following at the top of the app module:
QtGui.QApplication.setApplicationName('progname') QtGui.QApplication.setApplicationVersion('0.1')
and then later enter the name / version as follows:
name = QtGui.qApp.applicationName() version = QtGui.qApp.applicationVersion()
Other problems with your current structure are mainly related to maintaining the separation between code files and resource files.
First: the package tree should only contain code files (i.e. python modules). Resource files belong to the project directory (i.e., outside the package). Secondly: files containing code generated from resources (e.g. pyuic or pyrcc) should probably go into a separate subpackage (this also makes the exception control easier for your version control tool). This will lead to the creation of a common project structure as follows:
project/ db/ database.db designer/ mainwindow.ui icons/ logo.png LICENSE Makefile resources.qrc main.py package/ __init__.py app.py mainwindow.py ui/ __init__.py mainwindow_ui.py resources_rc.py
Here the Makefile (or equivalent) is responsible for creating ui / rc files, compiling python modules, installing / uninstalling a program, etc. The resources needed by the program at run time (for example, a database file) will need to be installed in a standard location that your program knows how to find (for example, something like /usr/share/progname/database.db on Linux). During the installation of the Makefile also need to generate a bash script executable (or equivalent) that knows where your program is and how to run it. That is, something like:
which obviously should be installed as /usr/bin/progname (or something else).
This may seem like a lot at first, but of course, the main advantage of finding a project structure that works well is that you can reuse it for all future projects (and start developing your own templates and tools for setting up and managing these projects )