Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Imports in a Python plugin
#3
(03-07-2020, 08:38 PM)Ofnuts Wrote: See attached.

Code:
Modules
├── common.py
├── module1.py
├── module2.py
├── module3.py
└── Modules.py

This contains a "main" file (Modules.py) that registers three plugins that use function in the module*.py files, while all three use code from common.py. Unzip and put the Modules directory in your plugin directory.

The trick is to retrieve the directory of installation (in "Modules") and insert it in the Python path where python looks for imports.

Remark: in Linux, macOS/OSX and other Unixen, Gimp only looks for files with the executable flag set (and only Modules/Modules.py has it). On Windows it will try to register all the *.py files, and since only Modules.py gets registered the other ones will be rerun on ebery Gimp startup.

Which bring us to why this is not such a good idea...
  • If your common code evolves (and if you are a decent (read: unsatisfied) programmer this will happen) you will likely do things in your new modules that would make them incompatible with old version of plugins. Lots of explanations to your users in perspective.
  • Of course you can have a directory for each plugin, each with its own version of the module. But this is not enforced and users got in the habit of putting everything in the same directory, directed by countless tutorials that tells them do so. 
So in the end it could be easier to have a small packaging utility that lumps all your code in one single file before publishing.

Thank you so much, Ofnuts. I shall think about this and come back then. I am no real programmer. I have much to learn, and I take your advice seriously.
Reply


Messages In This Thread
Imports in a Python plugin - by Ottia Tuota - 03-07-2020, 09:31 AM
RE: Imports in a Python plugin - by Ofnuts - 03-07-2020, 08:38 PM
RE: Imports in a Python plugin - by Ottia Tuota - 03-07-2020, 09:11 PM
RE: Imports in a Python plugin - by Ofnuts - 03-07-2020, 10:19 PM
RE: Imports in a Python plugin - by Ottia Tuota - 03-10-2020, 09:24 AM
RE: Imports in a Python plugin - by Ottia Tuota - 03-12-2020, 12:53 PM

Forum Jump: