How to import numpy package into my plugin?

Hello. I’d just like to import numpy package into my plugin. But when I add “import numpy” to the py file, the plugin doesn’t work. Also, I read the article below.

https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739
But I’m not a developer so i couldn’t understand perfectly. But I tried the instructions(copy/paste site-package directory to krita’s python directory and use pip), and the plugin didn’t work.

Windows10
Krita 5.1.3

Please, help

Thank you very much.

Hi

Yes, numpy library is not provided with Krita

What you’ve made is not clear, and “plugin didn’t work” is not really helpful
You should provide more information.

You’re on windows, there’s extra step to do

  • Did you installed Python3 on windows?
  • Did you installed pip on your Python3 installation?
  • Did you made a copy/paste of site-package directory (from Python3 installation) to krita’s python directory?

If yes, then, you have to execute this code from Krita’s scripter:

Did it returned errors?
If yes, please provide errors message

Grum999

  • Did you installed Python3 on windows? Yes
  • Did you installed pip on your Python3 installation? Yes
  • Did you made a copy/paste of site-package directory (from Python3 installation) to krita’s python directory? Yes. ‘C:\Users\myName\AppData\Local\Programs\Python\Python39\Lib\site-packages’ directory to ‘C:\Program Files\Krita (x64)\python’ directory

and Krita’s scripter returned like:

==== Warning: Script not saved! ====


Try to load PIL

Failed No module named ‘PIL’

Try to load numpy

Failed No module named ‘numpy’


ImportError: No module named pip

In file: runpy.py

In function: _get_module_details at line: 137. Line with error:


Thank you

And how do I fix the script to install other packages as well? Can I change the package name to something else instead of “PIL”?

Now what you need is to install the lacking modules. In this case Pillow and numpy.
I know, Krita asked for PIL not Pillow. It just that Pillow is the ‘continuation’ of PIL, so it still is used from within Python as PIL.

For this you will use the terminal. Just to be safe here a minimal step-by-step How to:

  • On the start menu open cmd or PowerShell
  • On the open terminal type: pip install Pillow wait for to install
  • Type pip install numpy

Done. You should now have both numpy and PIL installed

For what I see:
‘C:\Users\myName\AppData\Local\Programs\Python\Python39\Lib\site-packages’

You’ve installed Python 3.9

But Krita 5.1.3 use Python 3.8.1
There’s maybe a problem here, because Krita is not able to import pip module
ImportError: No module named pip

Try to install Python 3.8

Please take care, Krita 5.2 will use Python 3.10 :slight_smile:

@Daishishi no, provided script execute PIP to install PIL and numpy modules automatically; problem here is the pip module is not recognized by Krita so it’s not possible to install anything
Also executing pip outside Krita won’t do anything as Krita load its own python environment; pip modules installation must be made within Krita with functions provided in my script

Grum999

Somehow I was unaware of this :face_with_raised_eyebrow: Intriguing

I installed them but still not working…

I installed Python 3.8, did copy/paste directory, executed codes and Krita scripter returned:

==== Warning: Script not saved! ====


Try to load PIL

Failed No module named ‘PIL’

Try to load numpy

Failed No module named ‘numpy’


ImportError: No module named pip

In file: runpy.py

In function: _get_module_details at line: 137. Line with error:


did you closed/restarted krita?

Grum999

No, I didn’t

I restarted krita, but it doesn’t working

re-reading what I’ve written:

Seems it’s not working from scripter a then should activate and execute PIP through a plugin executed at startup :sweat_smile:
Unfortunately, it seems the plugin I’ve written when I tested pip install on windows is not on my windows anymore, not sure I still have old virtual-disk of my previous windows VM installation…

Grum999

Does that mean I can’t import packages?

I’m so confused. I always though that every python3 instance shared the same pip installation folder.
In the case of my Linux machine: /usr/lib/python3 So every subsequent python installation would reference this folder.
Maybe isn’t like this for krita because it is inside its own environment?


Inside Krita using the scripter I looked where krita’s Python is located

import os

print(os.path)

I got: tmp/.mount_krita-pp1i19/usr/lib/python3.8

@Grum999 Can’t you install the packages with pip inside this folder?

It means that additional work is required to be able to use PIP within krita
I currently don’t have the time to check that and rewrite plugin example for windows :confused:

Grum999

You’re using an appimage
So no :slight_smile:
As appimage recreate systematically all krita sandbox at startup; if you close Krita you’ll see, the folder doesn’t exist anymore

Also, here problem is relative to windows

If you go in this topic:

You’ll see that for Linux appimage it’s simpler and it’s explained where pip modules are installed :wink:

Grum999

You might want to tell this to my Pop! OS 22.04 then :rofl:
Here is what I got for running your initial code from the topic above:

==== Warning: Script not saved! ====
-------------------------------------------------------------------------
Try to load PIL
Failed No module named 'PIL'
Try to load numpy
Ok!

**********************
ImportError: No module named pip

In file: /tmp/.mount_krita-pp1i19/usr/lib/python3.8/runpy.py
In function: _get_module_details at line: 137. Line with error:
raise error("No module named %s" % mod_name)
**********************

Also this is wayyyyy above my head and I would only be guttering this topic with my unrelated remarks. So I will excuse myself and whish you good luck @drive.programming0 and @Grum999.

P.S.: Weirdly enough if I simply do: import numpy on my Krita 5.1.3 it just works, this is why I initially assumed the pip installation were shared.

Cheers

Oh… If so, what can I do to import the packages?

If you’re using Krita installed from OS package, it’s different, it normally use python from system; but take care about incompatibilities…
It’s provided on PopOS! through a flatpak?

To import package you have to create a plugin that will load PIP and then install modules.
Plugin have to be executed at startup…

It’s not “difficult” to do everything but it take some time and need some development skills
If you’re a total beginner it will probably be a pain and you should try to understand how to code a plugin and how plugins works in krita before trying to use external libraries… :smiling_face_with_tear:

Grum999