Skip to content

Importerror with multi module plugin #17

Open

Description

@deusyss

Hi,

I'm trying to create a plugin. I made a module for UI, one for database and one for funcionalities. All that modules are import in main module (with geany.plugin).

If i want to import correctly these module i have to import them into init of class which inherit from geany.plugin.

I attach my beginning plugin with the error (change the extension to zip).

I'm working under Linux Mint , and my plugin is under .conf directory in my home.

gearcos

Activity

  1. frlan commented on Feb 20, 2015

    @frlan

    I'm having also issues here. I have a class file in combination with an init.py inside a subfolder, but importing scripts seems not to recognize it. Could it be possible, that module search path due to the way of handling Python file maybe is screwed up?

  2. elextr commented on Feb 20, 2015

    @elextr
    Collaborator

    Probably the geany conf dir is not in Python's sys.path. The sys.path will probably start with where the geanypy Python script was loaded from. You can try printing sys.path from a pyplugin to see.

    If thats the case you can just add an appropriate dir to sys.path.

  3. frlan commented on Feb 20, 2015

    @frlan

    Geany config dir is really not inside. Only geanypy related path is /usr/local/lib/geany-plugins/geanypy. From my understanding, folder of file should be also part of this. Can this being done by geanypy via default?

  4. elextr commented on Feb 20, 2015

    @elextr
    Collaborator

    The problem is Geanypy loads plugins itself, it doesn't use Python import so no paths are added to sys.path. The potential problem of adding the plugin directory to sys.path is that it is a global, ie any value added for one plugin will affect the load path of another. The best way is for a plugin to append what it needs to sys.path possibly using the directory part of its __FILE__ , import its modules and then restore sys.path so it won't affect other plugins.

    As an aside, plugins should be aware that they are sharing a Python instance, so loading stuff in the global namespace (like modules) may also clash with another plugin.

  5. codebrainz commented on Feb 21, 2015

    @codebrainz
    Owner

    FWIW, I think this is the only sys.path modifications, needed in order to be able to import the geany module. As @elextr mentioned, the loader "manually" imports plugins by file path, like this.

  6. frlan commented on Feb 21, 2015

    @frlan

    Not sure whether it's the best solution as it looks little hackish to me:
    I'm try to succeed with something like that

    import sys
    import os
    sys.path.append(os.path.dirname(file))
    import my local modules
    sys.path.remove(os.path.dirname(file))

    for the meanwhile. Will keep you updated whether it a working solution.

  7. elextr commented on Feb 21, 2015

    @elextr
    Collaborator

    I agree that the Python import system is quite annoying, and quite the most poorly documented part of the language, the package layout and so on being in PEPs only referred to from the manual, so searching the manual finds nothing about it. (end rant, and Python3 docs are a bit better :)

    What it might be possible to do is to have the system and user geanypy plugin directories set up as modules (by having an empty __init__.py in them). Geanypy could load them as modules with known names eg system_plugins and user_plugins. Then the plugins can load stuff in their directory and below by

    import user_plugins.franks_plugin.module.whatever

    I have used this technique on Python 3, but I'm not sure if it works on Python 2.

    And as Enrico would say "somebodys go to do it, PRs are welcome" :)

  8. deusyss commented on Mar 3, 2015

    @deusyss
    Author

    Hi, thanks to watch this issue. I use Python 2. For the moment, i juste have to import the module once, but i think it could be a problem for another dev.

  9. deusyss commented on Jul 13, 2015

    @deusyss
    Author

    Hi robblue2x,

    Thanks for this information. I didn't know imp, but after doc reading, it seems very nice. Thank you for this tip :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions