Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Imports in a Python plugin
#1
Hello!

I have been writing some plugins for Gimp. I plan to share them soon since I believe they will be useful. But I have a problem where I wish you can help me.

I explain the general situation first before making my question.

Python allows imports. I have the plugins in two Python files. (One file registrates one Gimp plugin and the other registrates two.) The files contain large common parts. The common parts are currently embedded in both files, but it would be much better if the common parts could be in separate files instead and the two plugin files would just import them.

Now you perhaps think that I might just combine everything in one big file containing three registrations. But I don't want to do that. For future development I would want to have the common parts each in its own file, and I don't like the idea of having to combine everything in one big file every time I make a new version of some part. Besides, it is likely that those common parts will find usage in some future plugins.

Now I pose my question (sorry about the lengthy explanations). Please note that I know very little about computers.

Can the following be realized somehow? Suppose we have a file A.py implementing a plugin. Another file B.py contains material that we want to be imported in A.py, for instance some object called NameDefinedInB. In file A.py we have written

# File A.py
# ---------
from gimpfu import *
import B as imported_B
...
NameInA = imported_B.NameDefinedInB
# Then NameInA is applied in A.py in some way
...


In Gimp's plug-ins folder we put A.py and B.py in the same subfolder A as follows:

plug-ins
   |
   +---A  (subfolder)
   |   |
   |   +----A.py  (file, the plugin)
   |   +----B.py  (file, to be imported in A.py)
   |
   +---some other plugin
   |
   +---some other plugin
   |
  ...
   

This is how I would like to arrange things, hoping that the stuff in B.py will be imported in A.py.

I made a little test plugin about this. I have tried it with the following three installations of Gimp (on Windows 10 64 bits):

1: Gimp 2.10.18 from gimp.org
2: Gimp 2.10.18 portable from Samj
3: Gimp 2.10.18 portable from Partha

and got the following results:

1: The test plugin does not appear in Gimp's menu.
2: The test plugin does not appear in Gimp's menu.
3: Works OK! (And a .pyc file is created.)

The test plugin is in the attached zip file in case you wish to try it. Put the whole folder import_test as a subfolder in Gimp's plug-ins folder and restart Gimp. The plugin should be found as Filters/ImportTest/Import test. If it is, click it and see if anything happens in the error console.

Any ideas? Is this supposed to work at all? Since I want to share the plugins, I need a solution that works for everybody as such. Thanks in advance!


Attached Files
.zip   import_test.zip (Size: 795 bytes / Downloads: 281)
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: