How to easy_install egg plugin and load it without restarting application?
Asked Answered
W

1

6

I'm creating an app that downloads and installs its own egg plugins, but I have a problem loading the egg after easy_install extracts it into place. This is how it works now:

  • App downloads egg into temp folder
  • Installs egg with setuptools.command.easy_install.main() into ~/.app/plugins folder (which is pointed by a pth on dist-packages)
  • At this point, the ~/.apps/plugins/easy-install.pth is updated with the new egg path

The problem is that the pth is not reloaded until the python process is relaunched, which means the app has to be stopped and restarted (app is a long-running process, and plugin installation must not require a restart).

So the question is how to, either reload the pth programatically so that plugin entry-point discovery works for the new egg, or somehow have easy_install return the path it installed the egg into, so I can manually (with pkg_resources) load the new plugin?

I could create a function that tries to guess the easy_install'ed path or parse the pth on my own, but I prefer not to, if at all possible.

Python 2.6, setuptools 0.6c9


Thanks to Marius Gedminas, what I'm doing now basically is:

dist = pkg_resources.get_distribution(plugin_name)
entry = dist.get_entry_info(entry_point_name, plugin_name)
plugin = entry.load()
Weiss answered 12/7, 2010 at 18:15 Comment(3)
I don't know much about setuptools, but have you tried reload function? docs or #438089 ?Reilly
Yes i did, but reload is not enough. I tried reloading the site module (which I think is responsible for reading pth) but it did nothing. Even site.addsitedir(os.path.expanduser('~/.app/plugins')), which seems to add the new egg into sys.path list, is not enough to make it importable (import plugin_name) or discoverable for pkg_resources.Weiss
zc.buildout does something like this, although I wouldn't want to try reading its source code. Have you tried asking on the distutils-sig mailing list?Izy
I
5

After some browsing of the documentation I think what you need to do is

pkg_resources.get_distribution(name).activate()

where name is the name of the package you just installed.

Izy answered 14/7, 2010 at 18:20 Comment(1)
Wow, didn't notice that it would search everywhere to find a distribution, great :)Weiss

© 2022 - 2024 — McMap. All rights reserved.