# use of external pip libraries in Python plugins

**URL:** <https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739>\
**Category:** Plugins Development\
**Created:** [April 7, 2021, 8:10pm UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739 "2021-04-07T20:10:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 7, 2021, 8:10pm UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/1 "2021-04-07T20:10:24Z")

</div>

Hi

Few days ago I was a little bit irritated to read a comment concerning the difficulties -or impossibility- to use external libraries (particularly from pip) in Krita’s scripting.

And especially, the impression given by these words that the basic problem was a shameful choice on the part of the developers to privilege the current architecture _(ie: publish Krita through appimage for Linux instead of usual distro package)_.

Maybe my english level doesn’t help and I misinterpreted the comment… 🤷‍♂️

> [@Krisenter - A presentation framework for Krita](https://krita-artists.org/t/krisenter-a-presentation-framework-for-krita/21355/13):
>
> I therefore think it is a shame that third party python library use is discouraged instead of changing the infrastructure to accommodate it.

But ok, can’t stay with this without doing anything.

So, as I prefer use _appimage_ because I think that’s the simplest way to ensure that:

- All dependencies are up to date _ **AND** _ fully compatible with Krita
- Krita is always up-to-date
- Krita is available on all Linux without being dependent of distro specific package manager (rpm, apt, yum, … impossible to enumerate all and impossible for Krita’s team to build package for all possible package manager)

I took a look about how to execute _pip_ within Krita’s _appimage_ environment.

For curious, here results of my investigation.

# Krita AppImage

> **In scripter, you can execute this**
>
> ```auto
> from PyQt5.Qt import *
> 
> import sys
> import os
> import runpy
> import re
> 
> def pipInstallPath():
> """Return pip lib path
>     
> Eventually:
> - create directory if not exist
> - add it to sys.path
> """
> returned=os.path.join(QStandardPaths.writableLocation(QStandardPaths.AppDataLocation), 'pykrita', 'piplib')
>     
> if not os.path.isdir(returned):
> os.makedirs(returned)
> 
> if not returned in sys.path:
> sys.path.append(returned)
>         
> return returned
> 
> def pip(param):
> """Execute pip 
>     
> Given `param` is a list of pip command line parameters 
>     
>     
> Example:
> To execute:
> "python -m pip install numpy"
>         
> Call function:
> pip(["install", "numpy"])
> """
> def exitFct(exitCode):
> return exitCode
>     
> pipLibPath=pipInstallPath()
> 
> # keep pointer to original values
> sysArgv=sys.argv
> sysExit=sys.exit
> 
> # replace exit function to be sure that pip won't stop script
> sys.exit=exitFct
> 
> # prepare arguments for pip module
> sys.argv=["pip"] + param
> sys.argv.append(f'--target={pipLibPath}')
>     
> #print("pip command:", sys.argv)
> runpy.run_module("pip", run_name=' __main__')
>     
> sys.exit=sysExit
> sys.argv=sysArgv
> 
> def checkPipLib(libNames):
> """Import a library installed from pip (example: numpy)
>     
> If library doesn't exists, do pip installation
> """
> pipLibPath=pipInstallPath()
>     
> if isinstance(libNames, list) or isinstance(libNames, tuple):
> installList=[]
> for libName in libNames:
> if isinstance(libName, dict):
> libNameCheck=list(libName.keys())[0]
> libInstall=libName[libNameCheck]
> elif isinstance(libName, str):
> libNameCheck=libName
> libInstall=libName
>                 
> try:
> # try to import module
> print("Try to load", libNameCheck)
> __import__ (libNameCheck)
> print("Ok!")
> except Exception as e:
> print("Failed", str(e))
> installList.append(libInstall)
> 
> if len(libInstall)>0:
> pip(["install"]+installList)
> elif isinstance(libNames, str):
> checkPipLib([libNames])
> 
>         
> print('-------------------------------------------------------------------------')
> 
> checkPipLib([
> {"PIL": "Pillow"}, # module name (PIL) != pip installation name (Pillow)
> "numpy" # module name = pip installation name
> ])
> 
> try:
> import numpy
> print("Numpy version", numpy.version.version)
> except Exception as e:
> print("Can't import numpy", str(e))
>     
> 
> try:
> import PIL
> print("PIL version", PIL. __version__ )
> except Exception as e:
> print("Can't import PIL", str(e))
> 
> print('-------------------------------------------------------------------------')
> 
> ```

Results:

 ![image](https://krita-artists.org/uploads/default/original/3X/5/d/5d5bb162ee57906c3125e3765f668d59f92f3498.png)

- `print("Numpy version", numpy.version.version)`  
_Numpy version 1.20.2_
- `print("PIL version", PIL. __version__ )`  
_PIL version 8.2.0_

Modules are installed and ready to be used 🙂  
(note: I didn’t tested them yet, but I consider that if module is installed and loaded, that should be ok)

Scripter is interesting but it’s better to proceed installation of modules from a Plugin (finally it’s the target)  
Provided code can easily be copied/pasted in a plugin:

- If a required module is not installed, it will be installed at Krita’s startup (can be long according to your internet connection speed)
- If a required module is already installed, installation will be just ignored
- Modules installed from pip are located in `~/.local/share/krita/pykrita/piplib` directory (so, it’s local to user’s Krita installation)

# Krita for Windows

Ok for windows things are more tricky… I don’t why, but in _appimage_ the pip module is available by default, but not with windows.. (problem of license??)  
_**Note: that my installed Python3 environment on windows (not related with Krita) don’t have pip module too!**_

So it need a little bit more of work..  
First, I had to install pip on my Python3 environment  
Once installed, I made a copy/paste of `site-package` directory (from my Python installation) to krita’s `python` directory.

Now, in scripter it doesn’t work: the only way I found to use pip is through a Plugin executed at Krita’s startup:

 ![image](https://krita-artists.org/uploads/default/original/3X/8/4/849bf5513aef61e9470b33e1ce3e24ebb5f30ae1.png)  
(yes, on Windows I still have a 4.4.2 😅)

And then in scripter:

 ![image](https://krita-artists.org/uploads/default/original/3X/b/c/bc8728f7ae9c795328404d23812dcf91e3d41271.png)

# Conclusion

It’s possible to install properly pip modules for Krita 🙂

- For _appimage_ it’s easy, as everything is already available
- For Windows, I don’t know: technically, it might be possible to zip binaries and provide a plugin ready to use that will install everything without complex action for user (clearly, what I did can’t be asked for a basic user) but I’m wondering about licenses (even under an open source license, providing pip windows binaries might be more complex than just providing source code.. 🤔)

Maybe some additional work have to be made to let the provided code being more generic and easier to use for plugins, but curently I don’t have time.  
I keep this for the day I’ll need to use a pip library for a plugin…

Concerning pip module, maybe developpers can tell us more about why this library is available for _appimage_ but has been excluded from windows binaries.  
I’m curious 🙂

Grum999

---

<div class="post-metadata">

**Author:** ![AhabGreybeard](https://krita-artists.org/user_avatar/krita-artists.org/ahabgreybeard/32/2969_2.png) [@AhabGreybeard](https://krita-artists.org/u/AhabGreybeard)\
**Post date:** [April 7, 2021, 9:22pm UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/2 "2021-04-07T21:22:43Z")

</div>

For the Windows ‘difficulties’, is there a difference between the installer package and the portable .zip package or is this purely a local Python plugin thing?

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 7, 2021, 9:39pm UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/3 "2021-04-07T21:39:37Z")

</div>

Hi

I usually use portable version in windows when possible, because I don’t really like to see softwares installing things in the registry and other places on which I have no control 🙂

But I uncompressed installer and took a look on resources that are normally installed, in `python` directory the `site-packages` sub-directory content is empty (only a README like in portable version)

So I guess the same problem will occurs on an installed version.

On my side, the plugin part can easily be solved, just need some work to produce something that could be compatible for Windows and Linux AppImage.

The “blocking” part is pip binaries.  
Don’t know why they’re missing in Krita **and** in python but it might be related to a license thing, because I simply copy/paste what I installed in a python environment into Krita’s Python directory (+fix sys.path to add new directory in list of directories’ modules).  
If it’s “as simple as this” to install pip binaries with Krita, there’s might be a reason to not embed it with windows 🤷‍♂️

Note, didn’t tried for MacOS because I have no possibility to test it 🙂

Grum999

---

<div class="post-metadata">

**Author:** ![tiar](https://krita-artists.org/user_avatar/krita-artists.org/tiar/32/22_2.png) [@tiar](https://krita-artists.org/u/tiar)\
**Post date:** [April 7, 2021, 9:42pm UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/4 "2021-04-07T21:42:41Z")

</div>

> [@Grum999](#):
>
> because I don’t really like to see softwares installing things in the registry and other places on which I have no control

Krita doesn’t install anything in the registry, only in the install folder. Other than that it accesses AppData, but that’s something the portable Krita accesses as well.

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 7, 2021, 9:45pm UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/5 "2021-04-07T21:45:59Z")

</div>

Ah ok

Thank for info 🙂

But you never know what a software will really do on system until you’ve installed it 😅  
So that’s a reflex to get a portable version when it’s available 😉

After I’m only using my windows VM for testing Krita so I guess that I can try to pass over my habits 😅

Grum999

---

<div class="post-metadata">

**Author:** ![halla](https://krita-artists.org/user_avatar/krita-artists.org/halla/32/18581_2.png) [@halla](https://krita-artists.org/u/halla)\
**Post date:** [April 8, 2021, 8:13am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/6 "2021-04-08T08:13:40Z")

</div>

> [@Grum999](#):
>
> Concerning pip module, maybe developpers can tell us more about why this library is available for _appimage_ but has been excluded from windows binaries.

Most likely, it’s just because the Python we package with Krita has it, or doesn’t have it by default. There’s no conscious decision involved.

---

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [April 8, 2021, 8:33am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/7 "2021-04-08T08:33:57Z")

</div>

Just my random thoughts about the topic

What I have been doing to install extra resources on Blender per example is create a folder in my system Python \> Blender and then do pip installations inside it. The thing is with Blender and Gimp or any other program that you can script for for that matter you have something that is the “Environment” that is independent of the OS installation folders _visible frustration with Krita_. My folders per example are on my portable drives. So I install Blender and just tell it where to find extra resources to be used so I don’t need the be reinstalling everything concerning PIP or place it on the OS system. Having to work on the system hidden folders … is just not good policy and then I am criticized every time for my weird workflow and saving habits when I talk about it.

Open source examples

Blender:

 ![blender](https://krita-artists.org/uploads/default/original/3X/7/8/789cc0026fbc053433c398f960d4d019db6995dc.png)

Gimp:

 ![gimp](https://krita-artists.org/uploads/default/original/3X/8/5/85fb05e0e6aa84dc4a816f3b0e05fdc651bcb597.png)

You can simply say where your Numpy is in your system you can use it in Blender or in Gimp like this.

I know this solution is not for the everyday user but it is by far tested and works and much needed when scripting is allowed. I have spoke of this before and no one really gave any importance to it which I find perplexing to this day.

Not to mention this solution would just mean redirecting Krita’s attention to another set of folders to try and find resources of a given type and not load them if they where not workable. It is not as hard as to try and fix it with PIP and possibly have redistribution issues. This would work in any system and with any appimage and people that are used to this already know what to do.

in Maya’s case it was a text file that held all the folder variables that redirected everything and remained on the hidden folders.

 ![f57d3742a8478ad5cd2be5c993b6b500](https://krita-artists.org/uploads/default/original/3X/9/f/9f8ee03fb012151844b45ec8380195fb561477f1.png)

After I heard what he said on that thread honestly I agreed with him.

---

<div class="post-metadata">

**Author:** ![halla](https://krita-artists.org/user_avatar/krita-artists.org/halla/32/18581_2.png) [@halla](https://krita-artists.org/u/halla)\
**Post date:** [April 8, 2021, 8:53am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/8 "2021-04-08T08:53:19Z")

</div>

> [@EyeOdin](#):
>
> After I heard what he said on that thread honestly I agreed with him.

I get honestly a bit tired reading these threads. If people want an improved version of some feature in Krita, they should work on improving Krita, instead of complaining that something “is discouraged” into the void.

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 8, 2021, 9:20am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/9 "2021-04-08T09:20:38Z")

</div>

I think you’re wrong and just don’t understand how all of this works.

Blender Appimage have exactly the same problem than Krita’s appimage: you can’t access to OS resources without problem because that’s an appimage.  
Blender Appimage have a problem that Krita’s appimage don’t have: pip module is not available for blender appimage, so here it’s just impossible to use pip modules from Blender 's appimage

But as it’s Blender it’s better than Krita? 🤔

Didn’t tried blender on windows, but I think the problem might be the same, I have to test…

Grum999

---

<div class="post-metadata">

**Author:** ![tom](https://krita-artists.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://krita-artists.org/u/tom)\
**Post date:** [April 8, 2021, 9:23am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/10 "2021-04-08T09:23:49Z")

</div>

> Blender appimage

Blender doesn’t distribute appimages, from what I can tell. They distribute native binaries.

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 8, 2021, 9:25am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/11 "2021-04-08T09:25:19Z")

</div>

Oh ok

As my Python for windows is not provided by default with pip module, this might be the same source 😅

Thanks, in this case I’ll try to understand why python for windows is not provided with pip

Grum999

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 8, 2021, 9:27am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/12 "2021-04-08T09:27:10Z")

</div>

> **[Blender AppImages Released!](https://www.blendernation.com/2020/08/19/blender-appimages-released/)**
>
> Songtech\_3D writes…

It’s not official maybe 🤷‍♂️  
But it exists 🙂  
And as it’s an appimage, **even** Blender can’t do more than appimage allows to do…

Grum999

---

<div class="post-metadata">

**Author:** ![tom](https://krita-artists.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://krita-artists.org/u/tom)\
**Post date:** [April 8, 2021, 9:29am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/13 "2021-04-08T09:29:37Z")

</div>

> [@Grum999](#):
>
> And as it’s an appimage, **even** Blender can’t do more than appimage allows to do…

Perhaps I’m misuderstanding, but I fail to see how this argument supports the conclusion… Regardless, that’s not an official appimage.

---

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [April 8, 2021, 9:30am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/14 "2021-04-08T09:30:00Z")

</div>

> [@Grum999](#):
>
> Blender Appimage have exactly the same problem than Krita’s appimage: you can’t access to OS resources without problem because that’s an appimage.

Your not following what I am trying to say then.

Blender does not have access to the system folder resources either. But you can use pip to manually install the resources in any folder. Installing those resources in a installation OS folder is not good idea.

I have done this before to get PyQt5 in Blender. But you need to access a folder where that Pip installation is present. Yes it is a manual installation and you can’t automate it by just telling Blender to get the dependencies but you will read them because they are properly installed. All my Blender installations have PyQt5 once I set up my environment no more work needed since it is all in my portable drive. It is literally plug and play now.

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 8, 2021, 9:36am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/15 "2021-04-08T09:36:44Z")

</div>

> [@tom](#):
>
> but I fail to see how this argument supports the conclusion…

Just that problem is not on Krita’s side, but more about how appimage are working.

Even if it’s not an official appimage, Blender running in appimage have exactly the same problem than Krita concerning access to external resources.

Grum999

---

<div class="post-metadata">

**Author:** ![tom](https://krita-artists.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://krita-artists.org/u/tom)\
**Post date:** [April 8, 2021, 9:55am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/16 "2021-04-08T09:55:50Z")

</div>

> [@Grum999](#):
>
> Even if it’s not an official appimage, Blender running in appimage have exactly the same problem than Krita concerning access to external resources.

… But this doesn’t have relevance to @EyeOdin 's thoughts. The matter is about libraries installed into user folders, not system folders. I don’t see how appimages even have relevance in this discussion when EyeOdin is a Windows user…

---

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [April 8, 2021, 10:05am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/17 "2021-04-08T10:05:50Z")

</div>

Windows Python installation does provide Pip.

But you need to do add to the windows environment variables so Python works properly so it is not just install and your done. I do Pip installations per example now that it is running.

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 8, 2021, 10:07am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/18 "2021-04-08T10:07:04Z")

</div>

> [@tom](#):
>
> I don’t see how appimages even have relevance in this discussion when EyeOdin is a Windows user…

Because when I initiated this topic, it was about appimage problem and how to solve problem of external libraries with appimage.

My error was to also try to find a solution for windows users…  
Next time, I’ll just take care of linux users to avoid discussion about windows, on which problem is different.

Grum999

---

<div class="post-metadata">

**Author:** ![Grum999](https://krita-artists.org/user_avatar/krita-artists.org/grum999/32/66488_2.png) [@Grum999](https://krita-artists.org/u/Grum999)\
**Post date:** [April 8, 2021, 10:09am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/19 "2021-04-08T10:09:20Z")

</div>

> [@EyeOdin](#):
>
> Windows Python installation does provide Pip.

Don’t know why, on my side it was not provided and I had to follow manual installation process of pip on windows…

Grum999

---

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [April 8, 2021, 10:12am UTC](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739/20 "2021-04-08T10:12:27Z")

</div>

I think when your installing Python you need to check one of the boxes to have it right away.  
Something like that, i know I messed it up the first time too.

And don’t worry if it works just for Linux there is no worry it still has validity still.

I just feel there are other paths to reach the same goal too that are more inclusive so you don’t waste your work time if that is not what your aiming for. Or might be worth considering I donno.

If I knew how to fix what I spoke this would have been done a good couple of months ago instead of me just talking, but I am barely a scripter let alone a developer to do anything at the moment. I think most people around forget I started learning Python just last year right after I arrived at this forum asking tons of stupid questions, I am a literal noob.

[Next page](https://krita-artists.org/t/use-of-external-pip-libraries-in-python-plugins/21739.md?page=2)
