How do i properly point rhino to an existing Anaconda env?

First. thanks for getting python3 going huge leap forward.

I use Anaconda quite a lot. can i not have rhino use my existing conda environments? meaning i have to double down on recreating those envs into the .rhinocode folder?

I have numpy installed into the .rhinocode env OTB from mcneel and that works to import , I purposely created and anaconda env targeting python 3.9 and installed pandas there. Here is the error i get as well as the code…

#! python3
# env: 'C:\\Users\\<usernameobfuscated>\\anaconda3\\envs\\rhino\\Lib\\site-packages'

import rhinoscriptsyntax as rs
import scriptcontext as sc

import System
import System.Collections.Generic
import Rhino

import numpy
import pandas

def runme():
    print (pandas)
    print (numpy)
    print (rs)

    return 

runme()

image

when i go to using sys.path for locating pandas, it is found however throws the error. (note i removed the # env part)

here is that code

#! python3


import rhinoscriptsyntax as rs
import scriptcontext as sc

import System
import System.Collections.Generic
import Rhino

import sys
sys.path.append(r'C:\\Users\\<usernameobfuscated>\\anaconda3\\envs\\rhino\\Lib\\site-packages')

import numpy
import pandas

def runme():
    print (pandas)
    print (numpy)
    print (rs)

    return 

runme()

when i close rhino and use the python.exe provided in the .rhinocode folder i have no issue pathing and working with the libraries in the anaconda env

Is this maybe a bug with the editor? can i tell rhino to use my anaconda env instead at runtimes?

definitely something up with the pandas library and something else. I can get matplotlib to import form the conda env. which is great! so partially solved. i adjusted the locale thing as discussed in other threads and also inserted the pathing to the front of the sys.path list. I have python standalone outside of anaconda as well for nodejs packages so those were also coming up in the sys.path.

code below


#! python3

import rhinoscriptsyntax as rs
import scriptcontext as sc

import System
import System.Collections.Generic
import Rhino
import getpass
name = getpass.getuser()

import sys
sys.path.insert(0,r"C:\Users\{0}\anaconda3\envs\rhino\lib\site-packages".format(name))

print (sys.path)

import locale
print(locale.getlocale())

locale.setlocale(locale.LC_ALL, 'en_US')

import matplotlib

print (matplotlib)


1 Like

Thank you so much - this is the information that I have been asking for!

1 Like

im still having all sorts of fun stuff happening right now. matplotlib and numpy confusions too. Look like we spoke too soon on this being a partial resolution.

I get the error here now regarding matplotlib wanting numpy imported

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.

It seems that the approach right now is that you treat the python instance that rhino installs as it’s own environment, and install packages using the “#r : numpy, matplotlib” line toward the top. Obviously this is not ideal as we will end up with a massive python environment.

My preferred approach would be to release a package to conda/pypi that allows us to use our python environment to communicate with rhino/grasshopper - this would then be a requirement for any python environment that we set up for rhino/grasshopper to use. The other approach I’ve used is ghhops_server, but not all the Rhino.Geometry methods are exposed, so I was unable to get this to work as intended, also hops in Rhino 8 is non functional at the moment, hopefully they patch it soon.

1 Like

So actually…

Its a moving a target. I removed the sys.path. used conda to install the packages so they worked again and set he #env as described at the top of the script… then return to the previous version of the script with sys.path and then it worked again without having to set the #env at the top. Very odd.

<module ‘matplotlib’ from ‘C:\Users\/…/\anaconda3\envs\rhino\lib\site-packages\matplotlib\init.py’>

totally agree , autodesk did the same stuff with dynamo and local user env’s when they released python3. Managing one source of packages is way easier at the enterprise scale IMHO.

Is there a way forward for conda environments now or still TBD?

Append the libraries folder for the env you want to sys.path.

  1. I’m not a programmer so this maybe dumb, but I see a lot of lib/ library/ site-packages folder in my environment that I just created using mamba/ conda.
    Which one should I use? Lib/ Library/ libs ?

  2. Do I need to do this for individual libraries or only once for each environment?

  3. Is it this # env thing that I need to use?
    Rhino - ScriptEditor Command (rhino3d.com)
    image

@eirannejad
On this post
Rhino 8 Feature: ScriptEditor (CPython, CSharp) - Serengeti (Rhino WIP) - McNeel Forum
I saw the link for “conda” as package manger
Support package management using conda : RH-66284 (myjetbrains.com)
Not sure if this is already in works but using mamba/miniforge with default conda-forge channel will allow for fast environment solving and some license issues.
Just a thought.

It’s not dumb at all - well done for diving in.

I can never remember which sub folder it is precisely either. Either look a little further for a sub folder called pandas with __init__.py in it (it never uses pandas.py, does it?), or open a repl session within your conda env outside Rhino and look at the output from:

import sys
print(‘\n’.join(sys.path))

This is all definitely in hack territory by the way, and assumes conda is just making venvs with some extra stuff it needs. If conda has added its own core libraries, or especially if it uses a site.py for example, libraries installed into conda envs might need to import the folders from those too, from Rhino’s Python.

So the sys.path command gave me the following outputs that I added to the start of

# env: C:\\Users\\AdityaKa\\mambaforge\\envs\\py030910\\python39.zip
# env: C:\\Users\\AdityaKa\\mambaforge\\envs\\py030910\\DLLs\\
# env: C:\\Users\\AdityaKa\\mambaforge\\envs\\py030910\\lib\\
# env: C:\\Users\\AdityaKa\\mambaforge\\envs\\py030910\\
# env: C:\\Users\\AdityaKa\\mambaforge\\envs\\py030910\\lib\\site-packages\\
# env: C:\\Users\\AdityaKa\\mambaforge\\envs\\py030910\\lib\\site-packages\\numpy\\

#! python3

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

import rhinoscriptsyntax as rs
import scriptcontext as sc
import math

import System
import System.Collections.Generic
import Rhino

# import numpy

import numpy

# df = pd.Dataframe()

and I’m running into this now

I don’t know what that # env ... does exactly. Presumably just sys.path.append(…).

I’d try omitting the last path ending in numpy. And then try replacing all the # env: ...s with sys.path.append(...)s

If that doesnt work, then conda’s envs are too particular, to be able to easily import from, from normal Python processes. You’ll have to look into replicating in Rhino, the effects of conda activate your_env (or whatever the precise command is.

You know you don’t need conda just to use numpy, right?

@eirannejad - I can’t make any of this work, I think I have exhausted all permutation and combination. Do you have any suggestions?

@James - The default way of specifying r: package doesn’t work, because of some certificate or timeout issue.
Nevertheless, I would want the script to point to what is already available locally.

@Aditya_Kaushik Please see if this helps

1 Like

@eirannejad
I am on this version of Rhino
image
I activated my conda environment in a console.
image
These are my script editor options
image

This is what I get while trying to run this script
image
This is the logger output

What changes should I make?

Add \Lib\site-packages to the end of the conda environment path. also maybe use \ instead of / just to be sure

image

Only trying to import pandas
image

I also added these paths here
image

ok my bad. # env: <path> not # venv:
does that work?

I am also working on a similar problem on this other thread: