How does Deluge use absolute imports in it's code, but not an entrypoint in the root of the project?

Suggestions and discussion of future versions
Post Reply
99lives

How does Deluge use absolute imports in it's code, but not an entrypoint in the root of the project?

Post by 99lives »

Firstly, sorry for asking a question that is more suited to StackOverflow here, I didn't get any responses from there :(

I'm trying to expand my knowledge and skill of Python by moving from small scripts and web development to desktop applications, and am looking at Deluge's code for examples of how to structure a project. I am especially interested in how Deluge has separated the torrenting functionality from the user interface. Designing the core functionality (which is within Deluges core module), and having the user interface (with multiple user interfaces within the ui module) written separately and simply calling functions from the core is something I would like to emulate in my own project.

Looking at Deluges code, it seems that all imports for its own modules are absolute, for example:

Code: Select all

# deluge/core/core.py
import deluge.common
from deluge.core.eventmanager import EventManager
But interestingly all it's entrypoints, as defined in `setup.py` are also deeper within the program:

Code: Select all

# setup.py
entry_points = {
    'console_scripts': [
        'deluge-console = deluge.ui.console:start',
        'deluge-web = deluge.ui.web:start',
        'deluged = deluge.core.daemon_entry:start_daemon'
    ],
    'gui_scripts': [
        'deluge = deluge.ui.ui_entry:start_ui',
        'deluge-gtk = deluge.ui.gtkui:start'
    ],
    'deluge.ui': [
        'console = deluge.ui.console:Console',
        'web = deluge.ui.web:Web',
        'gtk = deluge.ui.gtkui:Gtk',
    ],
}
From what I can tell, according to the answer for this SO question: https://stackoverflow.com/questions/646 ... -right-way, this would not normally be possible. To use absolute imports the program would have to be started from a file in the projects root. However, Deluge can be started from several locations, none of which are in the projects root.

As far as my understanding goes, deluge must be changing its PYTHONPATH somehow, such as with `sys.path`, however searching the code only finds one instance of it, but it only seems to be related to the documentation, not the actually software code.

So how would one have an entry point for their Python program deeper within the modules, but use absolute imports from the project root? Deluge seems to have done it, but I don't know how, and I would like to do so in my project.

And as a side question, if you know of any other large desktop apps written in Python that I can look at for examples, I'd love it if you told me about it :)

Cheers
Post Reply