简体   繁体   中英

GIMP 2.10 Python - How to import custom module?

I recently updated to 2.10 GIMP from 2.8, and it seems that none of my plug-ins that imported custom (relative) *.py files work anymore. I dissected the code and it's not the contents of the "path_tools.py" file, it's just the act of trying to import a custom module (even if it's an empty file it still won't work). Without this line of code it shows up in GIMP just fine:

from path_tools import *

The plugin and path_tools.py are both in the same folder

\\AppData\\Roaming\\GIMP\\2.10\\plug-ins

I tried using explicit relative by adding periods

from .path_tools import *
from ..path_tools import *
from ...path_tools import *
etc.

I tried turning the file into a plug-in by adding an empty GIMP effect at the bottom of the file, to see if GIMP is just ignoring my file somehow

def path_tools(img, drw):

    return


register(
    "python-fu-path-tools",
    "Path Tools",
    "",
    [...]

And by opening GIMP in command prompt

 "c:\program files\gimp 2\bin\gimp-2.10" --verbose --console-messages

It just gives me a bunch of this kind of message for every plugin that won't load:

  Querying plug-in: 'C:\Users\ \AppData\Roaming\GIMP\2.10\plug-ins\path_tools.py'
  c:\program files\gimp 2\bin\gimp-2.10: LibGimpBase-WARNING: gimp-2.10: gimp_wire_read(): error

but those seem like warnings, especially since there is this actual error for another plug-in that wont load, for similar reasons I'm guessing:

  GIMP-Error: Unable to run plug-in "export-ALL.py"

Is it related to these warnings at the top?

Parsing 'c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp'
GIMP-Warning: Bad interpreter referenced in interpreter file c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp: python

GIMP-Warning: Bad binary format string in interpreter file c:\program files\gimp 2\lib\gimp\2.0\interpreters\pygimp.interp

I just don't know what's going on, or what to do. Is there a directory that I can put the file in that it will be visible to the plugins? Because it does work to import regular modules like 'sys', 'os'.

(Sorry if this is a really stupid question, I've only used Python for GIMP plugins.)

It seems that the Python interpreter embedded with Gimp 2.10 uses / as a path separator whereas Gimp 2.10 uses \\ on Windows.

The issue is discussed here .

Creating a file named C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\sitecustomize.py with the following content seems to fix the issue.

import sys
class Issue1542:
    def __del__ (self):
        if len (sys.argv[0]):
            from os.path import dirname
            sys.path[0:0] = [dirname (sys.argv[0])]
sys.argv = Issue1542 ()

You can hack the running file's containing directory's path into the sys.path before importing the module:

import os
import sys

sys.path.append(os.path.dirname(__file__))
import path_tools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM