mieki256's diary



2025/12/07() [n年前の日記]

#1 [gimp] Python-Fuで使えるモジュールを一覧表示

GIMP上の Python console で help("modules") と打ち込むと、同梱されているモジュールの一覧を表示してくれる。らしい。

_GIMP 3.0 Python Plugins - Justin's Blog

そんなことができるのか…。知らなかった。Windows11 x64 25H2 + GIMP 3.0.4 Portable Rev 2 で打ってみたら、以下が表示された。

GIMP 3.0.4 Python Console
Python 3.12.10 (main, Apr 18 2025, 06:12:22)  [GCC UCRT Clang 20.1.3 64 bit (AMD64)]
>>> help("modules")

Please wait a moment while I gather a list of all available modules...

__future__          _tracemalloc        hmac                secrets
__hello__           _typing             html                select
__phello__          _uuid               http                selectors
_abc                _warnings           idlelib             setuptools
_aix_support        _weakref            imaplib             shelve
_ast                _weakrefset         imath               shlex
_asyncio            _winapi             imathnumpy          shutil
_bisect             _xxinterpchannels   imghdr              signal
_blake2             _xxsubinterpreters  importlib           site
_bz2                _xxtestfuzz         importlib_metadata  smartypants
_codecs             _zoneinfo           inflect             smtplib
_codecs_cn          abc                 inspect             sndhdr
_codecs_hk          aifc                io                  socket
_codecs_iso2022     antigravity         ipaddress           socketserver
_codecs_jp          argparse            itertools           sqlite3
_codecs_kr          array               jinja2              sre_compile
_codecs_tw          ast                 json                sre_constants
_collections        asyncio             keyword             sre_parse
_collections_abc    atexit              lib2to3             ssl
_compat_pickle      audioop             libxml2             stat
_compression        autocommand         libxml2mod          statistics
_contextvars        backports           linecache           string
_csv                base64              locale              stringprep
_ctypes             bdb                 logging             struct
_ctypes_test        binascii            lzma                subprocess
_curses             bisect              mailbox             sunau
_datetime           builtins            mailcap             symtable
_decimal            bz2                 mako                sys
_distutils_hack     cProfile            markdown            sysconfig
_elementtree        cairo               markupsafe          tabnanny
_functools          calendar            marshal             tarfile
_hashlib            cgi                 math                telnetlib
_heapq              cgitb               mesonbuild          tempfile
_imp                chunk               mimetypes           test
_io                 cmath               mmap                textwrap
_json               cmd                 modulefinder        this
_locale             code                more_itertools      threading
_lsprof             codecs              msilib              time
_lzma               codeop              msvcrt              timeit
_markupbase         collections         multiprocessing     tkinter
_md5                colorsys            netrc               token
_msi                compileall          nntplib             tokenize
_multibytecodec     concurrent          nt                  tomli
_multiprocessing    configparser        ntpath              tomllib
_opcode             contextlib          nturl2path          trace
_operator           contextvars         numbers             traceback
_osx_support        copy                opcode              tracemalloc
_overlapped         copyreg             operator            tty
_pickle             crypt               optparse            turtle
_py_abc             csv                 os                  turtledemo
_pydatetime         ctypes              packaging           typeguard
_pydecimal          curses              pathlib             types
_pyio               dataclasses         pdb                 typing
_pylong             datetime            pickle              typing_extensions
_queue              dbm                 pickletools         typogrify
_random             decimal             pipes               unicodedata
_sha1               difflib             pkg_resources       unittest
_sha2               dis                 pkgutil             urllib
_sha3               doctest             platform            uu
_signal             drv_libxml2         platformdirs        uuid
_sitebuiltins       email               plistlib            venv
_socket             encodings           poplib              warnings
_sqlite3            ensurepip           posixpath           wave
_sre                enum                pprint              weakref
_ssl                errno               profile             webbrowser
_stat               faulthandler        pstats              wheel
_statistics         filecmp             pty                 winreg
_string             fileinput           py_compile          winsound
_strptime           fnmatch             pyclbr              wsgiref
_struct             fractions           pyconsole           xdrlib
_symtable           ftplib              pydoc               xml
_sysconfigdata__win32_ functools           pydoc_data          xmlrpc
_testbuffer         gc                  pyexpat             xxlimited
_testcapi           genericpath         pygments            xxlimited_35
_testclinic         getopt              pygtkcompat         xxsubtype
_testconsole        getpass             python-console      zipapp
_testimportmultiple gettext             queue               zipfile
_testinternalcapi   gi                  quopri              zipimport
_testmultiphase     gidocgen            random              zipp
_testsinglephase    glob                re                  zlib
_thread             graphlib            reprlib             zoneinfo
_threading_local    gzip                rlcompleter         
_tkinter            hashlib             runpy               
_tokenize           heapq               sched               

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".

cairo がある…。ということは cairo を使ってキッチリと描画するプラグインも作れそうかな…。

アレ? tkiniter という文字列もあるぞ? もしかして入ってたの? import できるのかな…?

>>> import tkinter as tk

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\Prog\Gimp-3.0.4-Portable_Rev_2\GIMPPortable\App\gimp\lib\python3.12\tkinter\__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing _tkinter: 指定されたモジュールが見つかりません。

エラーになる…。どゆこと? 実際に入ってるわけではないモジュールも並んでるということなのかな…。

この場合、Python から Tk を呼び出すための Pythonスクリプトは入ってるけれど、Tk の DLL自体は入ってないから呼び出せないということだろうか?

でもまあ、help("modules") と打てば、Python-Fu でどんなモジュールが使えそうか、ざっと調べることはできそう。

余談。GIMP 2.10.38 Portable で同じことをしたら、pathlib が入ってなかった。pathlib が標準モジュールとして導入されたのは Python 3.4 かららしい。GIMP 2.10 までは Python 2.7 が使われてるから、入ってないのだろうな…。

#2 [gimp][python][editor] Python-FuをVSCodeで編集する

Windows11 x64 25H2 + GIMP 2.10.38 Portable の環境で、テキストエディタ Visual Studio Code (VSCode) を使って Python-Fuスクリプトを編集したい。

そのままだと、from gimpfu import * とか pdb. とか gimp. とか書かれてる部分でエラーが出る。

以下のページで、VSCode の設定ファイルを作成してやれば GIMP の Python-Fu のモジュールも参照してくれるようになる、と紹介されていた。

_VS Code - configure linting for PythonFu - GIMP Chat

ざっと引用。

Python-Fuスクリプトと同じ場所に、.vscode というフォルダを作成。その中に、settings.json と .env というファイルを作成。

settings.json の内容は以下。Python 2.7 の場所を指定。
{
    "python.pythonPath": "D:/Python/Python27/python.exe",
    "python.envFile": "${workspaceFolder}/.vscode/.env",
}

.env の内容は以下。自分の場合は GIMP Portable をインストールしてあるので、"(GIMP Portableインストール場所)/App/gimp/lib/gimp/2.0/python" を指定する。要するにGIMPインストール場所の、lib/gimp/2.0/python を指定するのだろう。
PYTHONPATH="D:/Prog/Gimp-2.10.38-Portable/GIMPPortable/App/gimp/lib/gimp/2.0/python"

これで VSCode が GIMP関係のモジュールを認識してくれるようになった。

あるいは、.env は作らずに settings.json だけ作っておいて、以下にするのもアリだろうか。
{
    "python.pythonPath": "D:/Python/Python27/python.exe",
    "python.analysis.extraPaths": [
        "D:/Prog/Gimp-2.10.38-Portable/GIMPPortable/App/gimp/lib/gimp/2.0/python"
    ],
    "python.autoComplete.extraPaths": [
        "D:/Prog/Gimp-2.10.38-Portable/GIMPPortable/App/gimp/lib/gimp/2.0/python"
    ]
}

"python.analysis.extraPaths" もしくは "python.autoComplete.extraPaths" で指定した場所を VSCode が参照してくれるようになるっぽい。

ただ、エラーは出ない状態になったけど、インテリセンス/自動補完は全然効いてくれない…。もしかすると .pyi なるファイルも必要になるのだろうか?

以上、1 日分です。

過去ログ表示

Prev - 2025/12 -
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project