Best Practices for Running Python Scripts in Rhino Compute

I’ve made some Grasshopper Python components that I’m running on a VM that I followed via the official tuts using Rhino Compute but having some issues with a few scripts. They all work fine locally on my local machine.

I believe the problem is related to external Python modules. I install these modules at the top of the component using the syntax # r: pydelatin, numpy, rasterio, scipy and locally it works great.

Now I tried running this script on my Rhino Compute instance and it’s not working, and I imagine it has something to do with using these external modules because I have another Python component where I just use built-in modules and there were no issues. I also don’t know how to properly debug what’s going wrong.

If you think this could be the issue, what is the proper way to install external Python modules on a Rhino Compute server. I read somewhere that I can do this syntax at the top of my Python Component instead and might have more luck:

#! python 3
# venv: my-grasshopper-file
# r: pydelatin, numpy, rasterio, scipy
 
import pydelatin, numpy, rasterio, scipy

Should I do this instead?

I’m used to creating a venv at the project level for whatever Python project I’m working on, so in that case, should all Python components in a Grasshopper file point towards the same venv?

Say one Grasshopper Python node does this at the top:

#! python 3
# venv: my-grasshopper-file
# r: pydelatin, numpy, rasterio, scipy

import pydelatin, numpy, rasterio, scipy

And in another node further along in the graph I want to reuse numpy do I just do:

#! python 3
# venv: my-grasshopper file

import numpy

So I point it at the same venv file? Or do I reimport with # r.

And then if this is the issue and it does start working, now does every Rhino Compute instance on startup reinstall the modules?

Sorry for all the questions.

Thanks,

Aidan

Anybody? Just looking for best practices on running Python in general in Grasshopper with external modules. It almost always seems to create bugs for me.

Are you self hosting/deploying rhino.compute yourself? If so, you might just need to manually download the packages yourself on the machine.

I’m more confused as to how to not create conflicts. Because currently say I have different scripts that install different libraries via the# r: declarative at the top of the file, well anything that’s installed this way from my understanding installs that version of the library to the exact same Python environment. This means other scripts could potentially install other versions of these libraries to the same folder creating a mess. So I’ve noticed there is this venv you can put at the top of a script to force downloads into their own folder, which makes sense, but what’s confusing is like it’s per node versus per script. So is this by design? Do I have to make sure every node in my script points towards the same venv? That kind of seems sketchy and so I’m wondering what the actual best practice is for this and serious developers.