ScriptEditor and GH python component randomly fail to import numpy

I’m working on a python script in a GH component.

When I run the typical:

# r: numpy

import numpy

I sometimes get the following error message :
“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.”

The only resolution I’ve found so far is to manually remove all numpy related files from the .rhinocode directory. They are then reinstalled with the “# r:” method the next time I run the script.
Then, everything works for a few days, and then the message reappears.
The error occurs both in GH and in ScriptEditor

To call this issue aggravating is an understatement.
Is there a fix I don’t know about?
Is there any reason this should happen spontaneously?

I have seen this happen a few times and made a ticket to look into this and try to see if I can replicate it consistently:

RH-80494 ImportError on importing numpy from source directory

Add this line to your script and see if it behaves better:

# venv: site-packages

This will force editor to install package under Libs\site-packages path instead of the default environment under site-envs\default*

1 Like

Thank you. I’ll try this the next time numpy won’t load and report back.

1 Like

Adding this worked. Thank you.

1 Like

I had the same problem and it worked for me too. Thanks! This error was driving me crazy

1 Like

Hi @eirannejad I’m testing for another library but it won’t install (based on package(s))? Is this a software bug?

 tensorflow
python -m pip install tensorflow

Instead of import TensorFlow use import tensorflow.

Also, why not use # r: tensorflow right after the # venv line, before you import tensorflow?

thanks @nathanletwory
Is this what you mean?# venv: # r: tensorflow

No, on the next line.

# venv: site-packages
# r: tensorflow

And remember tensorflow module is all lower-case

2 Likes

As @nathanletwory responded above, pip package name for tensorflow is tensorflow lowercase. So if you are using the Install Package window make sure to type tensorflow and not TensorFlow

1 Like

hi @eirannejad all library install successfully but after restart Rhino get new error for numpy ?1. Error running script: 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. [2:1]
But adding this line to the script fixed the error :point_down: :point_down:


# venv: site-packages
# r: tensorflow


*And another error I can’t succeed in installing :arrow_forward: turtle :point_down: :point_down: :point_down: :point_down:

Error 11/01/1403 08:41:59 ق.ظ [RhinoCode] Error installing package | Rhino.Runtime.Code.Environments.EnvironException: subprocess-exited-with-error

   at Rhino.Runtime.Code.Languages.PythonNet.CPythonEnviron.AddPackages(IEnumerable`1 pkgSpecs, IProgress`1 progress)
   at Rhino.Runtime.Code.Environments.Environ`1.Rhino.Runtime.Code.Environments.IPackageEnviron.AddPackages(IEnumerable`1 pkgSpecs, IProgress`1 progress)
   at RhinoCodeEditor.Editor.Dialogs.InstallPackageDialog.<>c__DisplayClass16_0.<InstallPackages>b__0()



turtle.gh (9.6 KB)

I believe turtle is included in the Rhino distribution (it is in my py39-rh8\lib folder) and all you need is an import turtle as tu statement.

It looks like there will be some thrashing out needed to manage the Python Turtle Graphics window’s destruction elegantly. And I find turtle can crash grasshopper. But then I don’t know what I’m doing with it.

Turtle is based on tkinter and does not work on Rhino at the moment. I have a ticket to get this implemented at some point:

RH-67505 Implement support for tkinter, turtle

I have also logged a ticket for the numpy import problem and will look into it:

RH-80494 ImportError on importing numpy from source directory

You can simplify your script. Firstly, you only need one # venv: statement. Secondly # requirements: is a synonym for # r:, so you don’t need both tensorflow statements. Try this:

ghenv.Component.Message = "library python" 
ghenv.Component.NickName= "library python" 
ghenv.Component.Name = "library python" 
"""Grasshopper Script"""
# venv: site-packages
# r: sympy
# r: tensorflow
# r: luminoth 
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import Grasshopper as gr
import  sympy as s
import numpy as nu
import tensorflow as te
import turtle as tu
import bob as bo
import matplotlib as mt
#import luminoth as lm
import ghpythonlib as gh
import Rhino as r
import pendulum as pmmm
b=pmmm.day.annotations
a = "Hello Python 3 in Grasshopper!"
a=nu.cos(nu.deg2rad(60));
a=s.acos(0)
a=te.sin(3.14)
print(a)

Regards
Jeremy

3 Likes

It would be great if any of you having problem installing and importing numpy would be able to check this:

Update: