Numpy/PIL/etc in Krita python

Want to share experience (for the sake of google search) of the using python libs inside Krita with scripting (in addon/scripter/etc). There is several threads on this, but no definitive cross-platform solution - since pip is not present on some platforms (MacOs, Windows) and copying packages into Krita have problems on platforms with appimage.

So there is a description of another approach, not mentioned before - start Krita from bash script. Probably especially helpful for addon developers. Script deletes developing addon folder and replaces it with fresh copy and then getting site-package folder path of local python 3.9 installation and save it into text file inside krita addons folder. then addon read it on initialization and adding to sys.path - and all locally installed packages became visible inside Krita without additional efforts

from bash script:

PATH_KRITAEXE_MAC="/Applications/krita510.app/Contents/MacOS/krita" # Your path here

# python must be set to proper version!
# Krita 5.1.5 == Python 3.9
# conda activate py39
# python -m pip install Pillow
# python -m pip install numpy
# python -m pip install scipy
# python -m pip install scikit-image
module_path=`python -m pip show pip | grep 'Location:'`
module_path="${module_path:10}"
if [[ -z "$module_path" ]]; then
	echo "- python: no pip/etc installation found"
	exit 0
fi
echo "$module_path" > "$PATH_PYKRITA/wplstudio/python_site_packages.txt"

"$PATH_KRITAEXE_MAC" 

from addon initialization:

info_file = os.path.join(os.path.dirname(__file__), 'python_site_packages.txt')
try:
	# reading site-packages file, if any
	python_site_packages = open(info_file).read()
	python_site_packages = python_site_packages.strip()
	print("- site-packages:", python_site_packages)
	sys.path.insert(0,python_site_packages)
except Exception as e:
	print("- site-packages: no data", info_file, e)

this is for savy users, of course, but would like to know possible drawbacks

4 Likes

Sounds interesting.

When Python is installed via installer on Windows, PIP is automatically a part of the installation, but you can deselect it during custom installation.
Whether PIP is part of the embedded version of Python is unknown to me.
If it is a part of the standard-installation on macOS, I don’t know either.

Python 3.9? Then everyone with an up-to-date Python has to hold a Python 3.9 in a different folder to point to, or adapt the script after every Python-Update.

Michelist

Whether PIP is part of the embedded version of Python is unknown to me.

Unfortunately there is no pip at least on macos and Krita do not “see” installed pip/packes out of the box. And i failed to install it via get-pip.py or similar tricks that worked for blender for example (although recent versions have pip from the start). sys.path expansion works both on MacOs and on Windows

Python 3.9? Then everyone with an up-to-date Python has to hold a Python 3.9 in a different folder

Yep, Krita 5.1.5 uses 3.9 (for windows this is valid for 5.2.0. 5.1.5 on windows still uses Python 3.8) and packages added with sys.path expansion should match it. But there is conda/virtual envs to keep different versions at the same time, so this is somewhat managable, both on mac and win

1 Like