How to install libraries like SciPy into Rhino 8 beta script editor?

Only NumPy works there as far as I know…and nowhere did I find the option to install other libraries…how to do it please… :slight_smile:

Inside the Python3 editer,

import sys
print(sys.path)

You will find where python is installed.
They actually get a separated python 3.9 for Rhino8 for you (from what I see).

Then go to the directory of that python3.9, inside folder \Scripts you will find pip.
Then go to cmd, cd to the directory of the \Scripts folder (The Rhino8 one).
Then you can pip install whatever you want. I got matplotlib successfully installed in my case.

8 Likes

GREAT!! you are awesome :slight_smile: regarding matplotlib … have you found the way how to show the plot?

plt.show() wouldnt work.
But you can use plt.savefig() to save the image somewhere.
Then use whatever battery you have in GH to read the image by the path. I can’t remember if GH has a native one.

1 Like

This is a real time multiplot you can use as example.
Hope it helps.

Guys you are the best! thank you so much.

I would suggest to use the # r:modulename approach instead, since that will be easier to distribute since you won’t have hard-coded paths.

3 Likes

Hello @martin.petrik ,

How did it go? Could you successfully install pandas and matplotlib on you gh (Rh8)?

Hi Danny -
Can I pls confirm the following 2x points to align my issues on set-up?
i) When you search for the Python executable within the Rhino Script Editor, it renders the Rhino.exe file, i.e.
print(“Python system location”, sys.executable)

C:\Program Files\Rhino 8\System\Rhino.exe

ii) Whether you have any .\activate folder in the Scripts directory?

I’ve got a few issues:

  1. When running pip install, it continues to default to my primary machine installation
  2. When forcing pip install into […] .rhinocode\py39-rh8\ folders (i.e. site-env, site-packages, lib, etc.) --OR–
  3. Appending my system python path into the Script Editor, I receive an error (let’s say calling import numpy) of
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

Any ideas or feedback is greatly appreciated!

Cheers,

1 Like

Hi Nathan – I’m confused on this one:

Any # prefix comments out the line inside my Script Editor, rendering this approach useless (unless I’m fundamentally missing something)!

It is a special way for the Python engine to know that such modules are required, and it will install them if they aren’t already installed.

The format is specifically to start with a # so that it isn’t actually executable code.

Thank you for sharing Danny, it is great.
I successfully install opencv-python and it works like charm.
Thanks heap !

1 Like

Hi @darkstream22

Sorry for the late reply, I wasn’t understanding what you meant until today.

Today I tried to install numpy into the site-env folder, and I got exactly the same error while trying to import numpy.

If you do what I said previously and install packages with pip in the \Scripts folder. Every thing should works fine. The only shortcome is you have to reinstall every packages when you update Rhino.

Create a folder outside the rhino install and use #env to add that path to sys.path.

# env: C:/Path/To/Where/My/Library/Is/Located/

import mylibrary

mylibrary.do_something()

That folder will not be effected by updates.

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

2 posts were split to a new topic: Loading Tkinter in Python for Rhino 8

Hi Danny,

I tried using our approach. I installed Abaqus successfully inside of the script folder.
But when I use “from abaqus import*” in ghpython, I get error to ex. init.py and run.py

Do you know how to fix this problem?

-Seb

Hi @Sebastian_Rasmussen,

Apologies that I’m not familier with Apaqus module.

And I’m not sure if it still works outside the Abaqus CLI, it could be helpful if you can show the error content.

I downloaded the Abaqus Package using terminal (GitHub - haiiliin/abqpy: Type Hints for Abaqus/Python Scripting), which gives me access to Abaqus’s API. I then created a script in Visual Studio where everything from creating a part to running the analysis worked perfectly.

I wanted to try to make the same Abaqus Package work in GhPython. Therefore, I followed your method, which worked (it at least downloaded abqpy to the script folder).

When I try to use it in GhPython, it seems like either it is installed incorrectly, or there is another error that I cannot fix.

I have attached a couple of pictures.


@Sebastian_Rasmussen

I am not 100% familiar with abaqus or abqpy but I made this script in Grasshopper and looked into the error. Here is what I am understanding:

  • abqpy is a “Type Hint” python library.
  • when import abaqus happens, it wants to run a command line application that seems to be providing the type hint service for the python script that is importing the module
  • To do this, it looks up the __main__ module in sys.modules and checks the __file__ attribute on this module.

Here is the problem:

When you are running your script from vscode or other editors, your script is the only logic that python will be executing so there is only ONE __main__ module defined in that environment.

When running scripts in rhino, each scripts runs in its own scope. It does set the __name__ to __main__ for allow the conventional if __name__ == '__main__': checks inside scripts, but the scope is NOT the __main__ scope. Therefore the abqpy logic fails to get the location of the file.

Furthermore, Grasshopper scripts are embedded in their components and not saved to an external file so this does not even work with Grasshopper.

I hope this helps understanding the problem.

2 Likes