Problem using CPython Libraries

Hello,

I have some issues using Cpython libraries. I would like to use libraries in python as usual by installing them with pip, but even the pre-existing exemple with numpy doesn’t work.

I get this error :
Traceback (most recent call last):
File “file:///C:/Users/**/test.py", line 3, in
File "C:\Users*
.rhinocode\py39-rh8\site-envs\default-HL8Gx7po\numpy_init_.py”, line 164, in
raise ImportError(msg) from e
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.

I tried to install numpy in the proper folder with the pip in rhino’s python install and it seems to be already installed :

Requirement already satisfied: numpy in c:\users*******.rhinocode\py39-rh8\lib\site-packages

And when i check the sys.path i got many path inculding this one :

‘C:\Users\********\.rhinocode\py39-rh8\lib\site-packages’

Also if i run “import numpy” directly throught the python.exe in .rhinocode it work perfectly !

So if somebody has a solution it would be really helpfull !

Thanks

I was working on it for a few days and i finally found a way !

When i run :

import sys
print(sys.path)

Among many path there is this one :

‘C:\Users\******\.rhinocode\py39-rh8\site-envs\default-HL8Gx7po’

And it’s this one which causes the probleme, because python should not look in this directory for modules. So I have to remove it from sys.path like this :

import sys
sys.path.remove(‘C:\Users\******\.rhinocode\py39-rh8\site-envs\default-HL8Gx7po’)

I have to do it at the beginning of each code because i dont want to mess with the pythonpath.

I hope it will help someone !

1 Like

@Besson_Thomas Numpy sometimes throws this error and I have a ticket to look deeper at this. For any of pip packages that are giving you troubles you can specify the line below in your script and the package installer will install in site-packages instead

venv: site-packages

RH-80494 ImportError on importing numpy from source directory

@Besson_Thomas
Which version of Rhino are you running?

@Besson_Thomas

I would appreciate if you can check something for me. Install numpy normally in the script editor (not in site-packages). This should error on your machine as shown above.

Then go to this folder: (%HOMEPATH% is your home directory and default-* is the default environment in rhino python 3 deployment):

%HOMEPATH%\.rhinocode\py39-rh8\site-envs\default-*\numpy\core

Check to see if all these .pyd files are available.

explorer_Uk1FsvvAXf

The primary reason that this ImportError is happening, should be either one of these .pyd files are missing or security policies setup on your machine that stops them from being loaded. I am trying to confirm this

Rhino 8.5

All of these file are in the folder :confused:

1 Like

@eirannejad I also get this error. It goes away if I delete the environment that I am pointing in [Options Menu]. On the other hand. I realized that whatever modification I do in the Options Menu never gets saved, i.e., I have to it over again for every single component that I create. The button Restore Defaults is not working here either. I am using Rino 8.6 SR6

ImportError: Error importing numpy: you should not try to import numpy from its source directory;
please exit the numpy source tree, and relaunch your python interpreter from there.

To come back to the solution pointed by @eirannejad, my default-* folder is empty. How do I add the missing .pyd files there?

I am having the same issue, it came after updating Rhino8 to the latest version. Has anyone found a solution?

I’m looking at this deeper today and will do my best to put a fix in next 8.8 RC build :smiley:

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.

@eirannejad
Thank you for including these updates in RC 9
The only way I can make pandas and numpy work still is by doing this

#! python 3

import locale
locale.setlocale(locale.LC_ALL, 'en_US')
print(locale.getlocale())

import os
import os.path as op
import sys
import ctypes

CONDA_ENV = r'C:\Users\AdityaKa\.conda\envs\py030900'
sys.path.append(op.join(CONDA_ENV, r"Lib\site-packages"))
os.add_dll_directory(op.join(CONDA_ENV, r'Library\bin'))
import numpy as np

import pandas as pd

Is there a work around yet for:

  1. Having to use locale
  2. having to use sys.path instead of just pointing to a virtual environment.

I saw your comments regarding issues with trying to use multiple environments on another thread. I’m just trying to use one single environment.
Thanks

Open the editor, and the log viewer and look for these two lines and share here. this detects what your Rhino locale setting is when opened.

There it is.

That exactly matches mine :thinking:

Hi,

The Pandas import references the LC_TIME locale setting. Regardless of the default and current locale settings in the log, when Rhino’s Python starts up LC_TIME (and others) is set to en-US. In Windows 11 (and iirc 10, but don’t know about older) this needs to be en_US, ie underscore rather than hyphen. I believe this is different from the MS documentation and from locale settings on other platforms.

Alternatively, it could be English_United States (as you see in the log) which is more in keeping with what Windows actually does.

And personally, I would prefer all the five values to match the current locale setting (like LC_CTYPE) rather than have the mish-mash we currently see. I want to use the locale appropriate to my domicile (which is English_United Kingdom or en_UK).

HTH
Jeremy

So should I continue to keep the setlocale in all rhino8 scripts?

I guess you have to, at least until this is sorted out.

My own choice is to include setlocale(LC_ALL, ".UTF8") which combines the use of Unicode with the current locale so that I’m operating the same way as the major Windows apps and not like some win32 throwback.

Hi folks,

I have been trying to follow along with the suggestions in this thread, but I am still in a bit of a mess here.

I have been using numpy and scipy in cpython 3 in gh which has been working fine until I mistakenly accepted the rhino 8.8 update on my return from holiday yesterday! I spent yesterday trying to solve this and have just updated to RC 8.9 in the hope that this would magically go away, to no avail! I’m a bit stressed as I need to use the scripts I had worked on before my holidays to produce output for a deadline next week - great way to come back from the holidays!!

So I am running Version 8 SR9 (8.9.24171.3001, 2024-06-19)

Here is the code I’ve been running to try and verify numpy and scipy:

"""Grasshopper Script"""
#! python 3
# venv: site-packages
# r: numpy
# r: scipy
# r: networkx

__author__ = "Brian"
__version__ = "2024.06.22"

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino.Geometry as rg
import ghpythonlib.treehelpers as th
import csv
import os
import math
import scipy as sp
import numpy as np
import networkx as nx
#import scipy.stats as spst

numpy_version = np.__version__
scipy_version = sp.__version__

print(f"numpy version is {numpy_version}\n")
print(f"scipy version is {scipy_version}\n")

dist = sp.stats.norm()

output:

numpy version is 2.0.0

scipy version is 1.13.1

Traceback (most recent call last):
  File "rhinocode:///grasshopper/1/3f34b412-d348-41b4-a514-a5443794ff16/bd5293a5-7390-4539-9d9c-4c83d0c842c8", line 31, in <module>
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\__init__.py", line 162, in __getattr__
    return _importlib.import_module(f'scipy.{name}')
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\stats\__init__.py", line 606, in <module>
    from ._stats_py import *
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\stats\_stats_py.py", line 37, in <module>
    from scipy import sparse
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\sparse\__init__.py", line 294, in <module>
    from ._base import *
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\sparse\_base.py", line 5, in <module>
    from scipy._lib._util import VisibleDeprecationWarning
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\_lib\_util.py", line 18, in <module>
    from scipy._lib._array_api import array_namespace
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\_lib\_array_api.py", line 17, in <module>
    from scipy._lib.array_api_compat import (
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\scipy\_lib\array_api_compat\numpy\__init__.py", line 1, in <module>
    from numpy import *
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\numpy\f2py\__init__.py", line 19, in <module>
    from . import f2py2e
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\numpy\f2py\f2py2e.py", line 23, in <module>
    from . import crackfortran
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\numpy\f2py\crackfortran.py", line 159, in <module>
    from .auxfuncs import *
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\numpy\f2py\auxfuncs.py", line 19, in <module>
    from . import cfuncs
  File "C:\Users\brian\.rhinocode\py39-rh8\lib\site-packages\numpy\f2py\cfuncs.py", line 19, in <module>
    errmess = sys.stderr.write
AttributeError: 'NoneType' object has no attribute 'write'

Any advice gratefully received!