How can users enable Python virtual environment in Python-fu in GIMP3?

Hi,
In recent Python3, it is recommended that after enabling virtual environment users run the pip command to install Python optional packages. However, in Python-fu of GIMP3, it seems that virtual environment is not enabled. Is there any way to enable virtual environment in Python-fu to use Python optional packages from Python-fu?

Or using pipx may be the solution?

you would need to build your gimp against that virtual env.

the gimp python3 plugin is not running a python3 executable but loading libpython itself. but be warned that also means building all the python gtk bindings and so on in that virtual env.

Something that I haven’t tried is to set the shebang of the Python executable to the Python executable in the virtual env. This would likely work on Linux and perhaps OSX, but on WIndows? And of course you are more or less hard-coding the path to the venv in the executable itself.

Personally I don’t expect the general Gimp user to be able to install such things properly. It could be more productive to include all required modules directly with the plugin, and tweak the Python path before including them.

1 Like

Thank you for your advice! It is more reasonable and practical.

gimp links libpython to load the script. why would the shebang line have anything to do with that?

Because AFAIK Gimp doesn’t load libpython. The plugin is an independant executable and Gimp doesn’t really care what language it is written in.

On Linux, when passed an executable, the loader checks if it is a binary, and if not, reads the first line (a.k.a. “shebang”) to see how to run the file. This is normally the path/name of a plain binary with optional parameters to which the name of the executed file and its arguments are passed as additional parameters. A typical shebang is #! /usr/bin/bash (for shell scripts), another is #! /usr/bin/env python3(*).

So by changing the shebang in the file, you can define what interprets the file. In fact you can perhaps do even better.

(*) Which is what is used in the Python script in the screenshot. but the env command, after setting up the environment, uses one of the exec... system calls to “become” the python3 interpreter (the code of the process is replaced by the code of the target executable, but other characteristics, such as process id and file handles are kept). Which is why the screenshot shows the plugin as running python3 and not env.

1 Like

HI, Ofnuts.

I have tested changing shebang script and it works.

I use “#!/home/USERNAME/.venv/bin/python3” instead of “#!/usr/bin/env python3” and it works. However it is needed to install gi to $HOME/.venv/lib/python3.12/site-packages/ .