1

So I'm trying to expand my knowledge and skill of Python by moving from small scripts and web development to desktop applications, and am having some trouble understanding entry points with setup.pyand the effect they have on imports. I also don't have a deep understanding of how imports work in Python, which doesn't help.

My confusion comes from looking at example code of a large desktop application, specifically Deluge, a torrenting app written in Python.

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

# 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:

# 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, this would not normally be possible. To use absolute imports the program would have to be started from a file 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

99lives
  • 798
  • 1
  • 8
  • 17

1 Answers1

0

All of these are actually doing the exact same thing.

In the entrypoint we have a line like this one

'deluge-console = deluge.ui.console:start'

it is accessing the top-level package name, deluge. Within that, it goes into the ui module, and a file within there called console.py. In that file is a definition for 'start'. After installation, when called from the command line, you will be accessing the installed python modules, and eventually the function you want to run.

In the files themselves we see lines like this.

from deluge.core.eventmanager import EventManager

It goes into the installed package deluge. Goes into the module 'core' and it will find a file called eventmanager.py. In that file, I'll guess there is a class called EventManager.

In each case, you are accessing installed packages, and providing information about modules and sub-modules, and then finally the actual objects functions classes whatever that you want.

James Natale
  • 476
  • 4
  • 9