PyQT project organization

pyqt4, python

Solution

There is one key thing to remember when structuring a python project: the directory of the currently running script is automatically added to the start of `sys.path`.

So if you put your `main.py` script outside of your package in a top-level container directory, this will guarantee that package imports will always work, no matter where the script is executed from. To illustrate, here is a simple project structure:

project /
    main.py
    package /
        __init__.py
        app.py
        ui /
            __init__.py
            mainwindow.py

The `main.py` script should be very minimal, and contain only something like this:

if __name__ == '__main__':

    import sys
    from package import app
    sys.exit(app.run())

and within the `app` module, the gui modules would be imported like this:

from package.ui.mainwindow import Ui_MainWindow

This same import syntax can be used anywhere within the package tree. So if you added another sub-package like this:

project /
    main.py
    package /
    ...
        dialogs /
            __init__.py
            search.py

then the `search` module would import its gui module like this:

from package.ui.search import Ui_SearchDialog

If you organize all your python projects in this way, there should never be any need to manipulate `sys.path` in order to get your local imports working correctly.

Problem

I want to organize my PyQT project, I tried to put the UIs in a subfolder and import them like this: ``` import sys sys.path.append('UI/gui_sensors') from gui_sensors_extended import Ui_SensorsWindow_Extended ``` But it gives me error as it can't find *Ui_SensorsWindow*, the ui-class that was inherited in *Ui_SensorsWindow_Extended* So, What do you suggest to organize my project? And how can I handle it in the code?

Original source