Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

VS Code - configure linting for PythonFu

Sun Jul 26, 2020 4:04 pm

Had a bad day setting up Microsoft VS Code Python Extension to Lint PythonFu scripts properly :gaah.

So thought I would document the process for posterity.

Symptom...
pylint import-error: Unable to import 'gimpfu'
This meant there were loads of other warning about pdb, gimp, etc.

Software...
  • Windows: v10.0.18362
  • Python 2.7.18 (Optional - could also use version installed in Gimp folder)
  • Pylint v1.9.5
  • VS Code: v1.47.2
  • Python extension for Visual Studio Code: v2020.7.96456 (ms-python.python)

Solution...
  • Install pylint to Python2: C:\Python27\python.exe -m pip install pylint
  • Open your plug-ins folder with VS Code (...\AppData\Roaming\GIMP\2.10\plug-ins\)
  • Add the following to ${workspaceFolder}/.vscode/settings.json
    Code:
    "python.pythonPath": "c:/Python27/python.exe",
    "python.envFile": "${workspaceFolder}/.vscode/.env"
  • Add the following to ${workspaceFolder}/.vscode/.env
    Code:
    PYTHONPATH="C:/Program Files/GIMP 2/lib/gimp/2.0/python"
  • Exit VSCode

Some comments & tips...
  • I've chosen to implement this in the workspace folder so it doesn't affect my Python3 apps, but you could also do it in your VS Code user (global) settings.
  • Note the forward slashes even though I'm on Windows. Alternative is to use escaped back-slashes... \\
  • VS Code will automatically look for the .env file in ${workspaceFolder}... but this is NOT the .vscode folder (I wanted it out of the way, so I explicitly set path under .vscode).
  • python.pythonPath pointing to Python2 location is required so the Linter does it's stuff to Python2 standards.
  • PYTHONPATH tells the Linter where to look for modules for import - gimpfu in our case.
  • If you don't have stand-alone Python 2.x installed, you can change the Python27 paths above to the python.exe in C:\Program Files\GIMP 2\bin (Only quickly tested this though).
  • You can add # pylint: disable=unused-wildcard-import to the top of your plugin script to hide some warnings related to the from gimpfu import * line.
  • I was hoping the VS Code "python.autoComplete.extraPaths" pointing to gimpfu.py would allow auto-complete for pdb functions etc, but it only seemed to recognise the a few enums etc. Might look at adding Gimp support to "typeshed" on GitHub next!
  • I use WinPDB, so have not looked at how to attach VS Code debugger to Pythonfu plugin.

If anyone knows an easy way to get VS Code auto-complete working for gimp/pdb or how to debug plugins via VS Code - please document it here!

Cheers.
Post a reply