Problem using CPython Libraries

A major case where this error happens seems to be related to package install failures in pip. Sometimes when pip is trying to install a package that has a numpy dependency (or any other module for that matter), it tries to delete the existing numpy installation and WILL fail if numpy is already loaded inside of Rhino (e.g. _multiarray_umath.cp39-win_amd64 dll is locked and can not be removed). So we end up with an invalid environment that contains a folder named numpy but with half the internal files deleted. Any futhur import of numpy will result in the error below (and above):

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "~\.rhinocode\py39-rh8\python.exe"
  * The NumPy version is: "1.26.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: cannot import name 'multiarray' from partially initialized module 'numpy.core' (most likely due to a circular import) (C:\Users\ein\.rhinocode\py39-rh8\lib\site-packages\numpy\core\__init__.py)

I made a change in the python runtime to mark the environment as ‘corrupt’ if a package install fails. This way once Rhino is restarted, it will discard that environment and will create a new one. All scripts referencing packages in-script will install their packages fresh.

The editor will show a message related to corrupt environments with a restart hint

Due to the size of this change, I merged for Rhino 8.9 so we can get more testing.