Import function in Rhino 8 from external python file

Hello, I am working on Rhino 8 and using a Visual Studio Code as editor. I could ask if there is a way to import a file python (on VS Code) where I define a function and then Run the code on Rhino with the command Run in Rhino WIP. Because, I import the file without any warnings on VS Code, however when I try to run in Rhino I get back this: ModuleNotFoundError: No module named 'Module' (Module is the name of the folder where I have the .py file with the function) Thank you!

@Alessio_Pelagalli Where are these files located? Would you mind sharing an example that replicates the error?

Hello @eirannejad, thank you for your reply.

The python file where the function that I defined is located at ‘C:\Users\Alessio Pelagalli.rhinocode\py39-rh8\site-stubs\rhino3d-8.5.24072.13001’ inside a new folder, called Module, that I made. The python file where I need to recall the function is located on an external storage: E:\Alessio_Pelagalli.…\python file. This is the error message when I try to run in rhino 8

Hey, you can try to use ScriptSync, to test quicker from VSCode to Rhino.

Normally it’s better practice to keep all your modules in the same directory, but if you have a to cherry picking locally you have to add the path to where the module is:

import os
sys.path.append("the path where your module is")

import my_module

This way the interpreter should know from where to import even in rhino. I believe there is no need to place it in the /site-stubs with this method, it can be everywhere locally.

@Alessio_Pelagalli

The folder that you are pointing to is automatically generated by Rhino scripting tools. Is the Module that you are having problems with something you copied into this folder manually?

Ideally you would not want to make any changes to the contents of these folders.

@eirannejad

the folder is created manually and I moved it in the .…\site-stubs\rhino3d-8.5.24072.13001 because the rhino script module works so I thought if I put my module file inside the same path could work. Anyway, I would like to have the module folder under another path, such as an external hard disk. However, even if I use this when I try to run in Rhino the terminal give me back the same error. The idea is to have a python file with a several def function that I can recall inside my script to avoid a long code. Thank you for the helping.

import os
sys.path.append(“the path where your module is”)

import my_module

Hi @Alessio_Pelagalli , and all,

I have been fiddling with this today, and I have found that the following works:

#install the packages that the custom module depends on, for ex numpy
# r: numpy
# env: "path/to/customExternalDirectory/myModule.py"

import myModule

a = myModule.function(var)


Granted all the dependencies need to be pip installed into that script, which opens its own bag of problems

which leads me to ask @eirannejad if there could be a #r : requirements.txt type of automation possible

Hope it helps -