RhinoCode and Python modules that are in development

Dear @eirannejad ,

In ironpython old editor I can use conda packages that are currently in the development.

What is the current workflow for RhinoCode?
Context:

  1. I do not have yet a package published to pip or conda and it is in state of constant development
  2. the package itself uses some Python3 libraries specified in requirements .txt and their installation is handled by conda when creating the environment
  3. I need Rhino to constantly test functions because the code interacts with rhino geometry.

How can I use such workflow in RhinoCode?
For sure by typing import “my_package_name” it is not recognized in RhinoCode editor.

You can either add the search path to sys.path in your script, and then import the package

or even better, specify the path using the # env: directive somewhere at the top of the script like this

# env: /path/to/where/packages/are/installed

This path will be automatically added to sys.path

Could you please provide a full example from your computer?

My current environment is in the following path:
C:\Users\petrasv\.conda\envs\assembly2

Also I tried such path:
C:/brg/2_code/compas_assembly2/src/compas_assembly2/__init__.py

Which does not work in this rhinocode example:

"""
NOTE:

- Reference to RhinoCommmon.dll is added by default

- You can specify your script requirements like:

    # r: <package-specifier> [, <package-specifier>]
    # requirements: <package-specifier> [, <package-specifier>]

    For example this line will ask the runtime to install
    the listed packages before running the script:

    # requirements: pytoml, keras

    You can install specific versions of a package
    using pip-like package specifiers:

    # r: pytoml==0.10.2, keras>=2.6.0

    # env: C:/Users/petrasv/.conda/envs/assembly2/
"""
#! python3

import rhinoscriptsyntax as rs
import scriptcontext as sc

import System
import System.Collections.Generic
import Rhino
import compas_assembly2

Not in the docstring. In your scripts as in:

"""
This is the docstring
"""
#! python3
# env: C:\Users\petrasv\.conda\envs\assembly2

Also note that the # env: directive needs to be within the first 16 lines on the script file

I tried different paths, it does not find the library.

What files normally should be in this directory?
Could you please give an example on your machine?

The directory should contain python modules

test/ in this example is the directory you want to point to

in includes libtest/ that contains __init__.py and therefore is a python module

Rhino_2UhrfEcEmT

Thank you, and could you please give a full python example with the path from your pc in the python code?
Sorry for asking this multiple times, I want to be sure.

Probably I am doing something wrong. But none of the above works…
Is is strange that I see the possible imports but after running them they do not work.

This link is importing the current anaconda environment: compas2

sys.path.append("C:/Users/petrasv/AppData/Local/anaconda3/envs/")

But the packages/libraries inside it, are not recognizable.


#! python3
# this does not work
# env: C:/Users/petrasv/AppData/Local/anaconda3/envs/

import sys
sys.path.append("C:/Users/petrasv/AppData/Local/anaconda3/envs/")

import rhinoscriptsyntax as rs
import scriptcontext as sc
print("Hi")
import System
import System.Collections.Generic
import Rhino
import compas2
import compas_assembly2 #error here

I tried to import these paths, but they do not do anything:

The python version in coda is:

(compas_dev) C:\brg\2_code\compas>python --version
Python 3.9.10

And rhinocode one:

Python Version:
3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]

image

I guess things are more complicated than specifying the sys path…

For ironpython I run command python -m compas_rhino.install -v 8.0

That runs in the background this file:
install.py (13.7 KB)

That creates symlinks in this folder:

If you know how to do a similar process to your editor, it would be a big help.

I installed conda and conda rhino using instructions here

the added the path using # env: in my script.

#! python3
# env: C:\Users\ein\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts

import compas

print(compas.__version__)

and it works:

Does this not work for you?

Also if compas is the only package you need, you can specify that in your script and it will get installed

#! python3
# r: compas

import compas

print(compas.__version__)

To clarify a bit, it is for local repositories, not the one that is pushed to conda or pip.

I did the same, but it is not recognized on RhinoCode (Rhino8 beta) while pythoneditor prints the version (screenshot below). Compas is installed by building the forked newest repository rather than installing it from conda. If I write this # r: compas, it works, but not for local repositories.

From scratch, this is what I write in Anaconda prompt, for a fresh setup:

git clone: https://github.com/compas-dev/compas.git
conda create -n compas_dev python=3.9.10
cd C:\brg\2_code\compas
conda install shapely
conda activate compas_dev
pip install -r requirements-dev.txt
python -m compas_rhino.install -v 8.0 compas

Is there any indication that the path in env: comment is actually triggered?

On another note, how can I remove installed libraries from the RhinoCode?

#! python3
# env: C:\Users\petrasv\AppData\Roaming\McNeel\Rhinoceros\8.0\scripts

Ok, I deleted folder from here according to other posts in mcneel forum:
C:\Users\petrasv\.rhinocode\py39-rh8\site-envs

Old stuff got removed and the scripts folder is initiated.
What I see now:

  1. I can import the library and print its version

  2. but the conda libraries seems to be not loading, e.g. if you copy paste this line:

from compas.geometry import (
    Frame,
    Plane,
    Vector,
    Geometry,
    Transformation,
    Polyline,
    Polygon,
    Point,
    Box,
    Line,
    Pointcloud,
    bounding_box,
    convex_hull,
    distance_point_point,
    cross_vectors,
    centroid_points,
    distance_point_plane_signed,
    intersection_plane_plane_plane,
    centroid_polyhedron,
    volume_polyhedron,
    transform_points,
)

it requires scipy. which is weird because conda environment already has this library installed.
And if I write: # r: scipy, RhinoCode tries to install the library but actually nothing happens and the code does not run.

Any ideas how to solve this?

It feels that RhinoCode does not read libraries from conda environment:

![image|690x362](upload://lOvQkPqrjitJ2Yv7ugeewx9EIJv.png)
Error 13.10.2023 15:10:55 [RhinoCode] Error executing code | Traceback (most recent call last):
  File "file:///C:/Users/petrasv/Desktop/Untitled.py", line 9, in <module>
  File "C:\Users/petrasv/AppData/Roaming/McNeel/Rhinoceros/8.0/scripts\compas\geometry\__init__.py", line 192, in <module>
    from ._core.transformations_numpy import dehomogenize_and_unflatten_frames_numpy, dehomogenize_numpy
  File "C:\Users\petrasv\AppData\Roaming\McNeel\Rhinoceros\8.0\scripts\compas\geometry\_core\transformations_numpy.py", line 7, in <module>
    from scipy.linalg import solve  # type: ignore
  File "C:\Users/petrasv/AppData/Local/anaconda3/envs/compas_dev/Lib/site-packages\scipy\linalg\__init__.py", line 208, in <module>
    from ._basic import *
  File "C:\Users/petrasv/AppData/Local/anaconda3/envs/compas_dev/Lib/site-packages\scipy\linalg\_basic.py", line 13, in <module>
    from ._decomp import _asarray_validated
  File "C:\Users/petrasv/AppData/Local/anaconda3/envs/compas_dev/Lib/site-packages\scipy\linalg\_decomp.py", line 26, in <module>
    from scipy._lib._util import _asarray_validated
ModuleNotFoundError: No module named 'scipy._lib._util'

Full list of conda environment dependencies:

To put it more simply, this does not work:

If you look into the C:\Users\petrasv\AppData\Roaming\McNeel\Rhinoceros\8.0\scripts folder, only compas libraries are symlinked into the conda environment that you have installed these libraries in.

If scipy is installed in that environment, add another line of # env: and add the parth to where packages are installed in the conda environment as well.

On my machine it would be C:\Users\ein\.conda\envs\research\Lib\site-packages for my conda environment named research

Hi,

In my case it does not work. Any ideas why?

Also I think the 1.17 version you are using is probably installed from pip or conda, not the local repository.