Installing packages into kicad's venv - how?

As a practicle example of my issue, the plugin “KiMesh” requires shapely to run. If i run “pip install shapely” in my base venv, it returns: Requirement already satisfied: shapely in /usr/local/Caskroom/mambaforge/base/lib/python3.10/site-packages (2.1.0), so it should work, right?
No. Infact, even if I go into Settings → Plugins and change the python interpreter to the one in my venv explicitly, it still does not detect the installed package. According to this thread you simply install it in the base environment, but that does not seem to work for me. The issue was also discussed here with no real resolution that works for me either.

Here is an example for a Windows environment:

This opens the command prompt.

  • “pip install shapely” Execution result

  • KiCad 9.99 Command Prompt

You may now invoke python or pip targeting kicad’s install

%USERPROFILE%\Documents\kicad\9.99>pip install shapely
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: shapely in %USERPROFILE%\Documents\kicad\9.99\3rdparty\python311\site-packages (2.1.1)
Requirement already satisfied: numpy>=1.21 in c:\program files\kicad\9.99\bin\lib\site-packages (from shapely) (2.3.1)

  • Analysis

%USERPROFILE%\Documents\kicad\9.99>pip show numpy
Name: numpy
Version: 2.3.1
Summary: Fundamental package for array computing in Python


Requires:
Required-by: KiKit, shapely

  • action

%USERPROFILE%\Documents\kicad\9.99>pip uninstall shapely
%USERPROFILE%\Documents\kicad\9.99>pip uninstall KiKit
%USERPROFILE%\Documents\kicad\9.99>pip install shapely
%USERPROFILE%\Documents\kicad\9.99>pip install KiKit

When I started the plugin, a pop-up message appeared saying that the library being called was missing.

  • Further action

%USERPROFILE%\Documents\kicad\9.99>pip install matplotlib

The plugin started up using the above steps. This is my first time using this plugin, so I don’t have any experience with it or whether it works or not.

  • For other operating systems

If you’re using a platform other than Windows, you might be able to achieve the desired behavior with environment variables by referring to this thread. If there’s a conflict between plugins, it should probably work if you switch the environment variables via a script that restarts KiCAD for each plugin. I don’t have the environment right now to check, but I think it’s worth a try.

  • memo

dev-docs : APIs and Bindings → KiCad IPC API → For Add-on Developers Plugin Action registration

On macOS and Windows, KiCad will continue to ship with its own Python interpreter,

On Linux, KiCad uses the first version of Python found in the user’s PATH by default. Executable plugins are run as separate processes with an arbitrary command line, and can be used to launch plugins written in compiled languages, or any other kind of external tool that does not require a Python environment.

  • memo2:

Deprecation notice

The SWIG-based Python bindings in KiCad are deprecated as of KiCad 9.0 and will be removed in a future version. …

Looks like you are on linux. You should install packages into system python environment, not in any venv.

The python interpreter selector in kicad’s settings only affects new plugins that work through IPC api, KiMesh is likely not one of them so it still uses system python.

this is recommended against by the python comittee and all major distros since PEP 668, and has a habit of breaking the entire OS due to the fact that most OS’s rely on python packages for core functionality… is there any other way?

On Ubuntu you probably need to execute this command:

sudo apt install python3-shapely

It is one of the many reasons why the “default python api” in KiCad is deprecated and the IPC api replaces it.

But yes there is no other way and will be no other way since we are removing it.

For stuff that actually interferes with other programs using system python there are usually system packages that provide required python package, like dsa-t suggested. For minor stuff it’s pretty safe to install them system-wide using pip.

Alternative would be to install the package into a fresh venv (created from system python) and then start kicad with modified PYTHON_PATH and if required LD_LIBRARY_PATH to add venv directories to python lib lookup paths.

But yeah, all of that is going away at some point.