Can't import Rhino.RhinoMath

Why does this not work in Py3?

#! python 3
import Rhino.RhinoMath as rm
Traceback (most recent call last):
  File "file:///C:/Users/---/.rhinocode/stage/g13yamyk.wwc", line 4, in <module>
ModuleNotFoundError: No module named 'Rhino.RhinoMath'

Yeah, simply that invalidates a bunch of scripts that I now have to go back and modify.

This is the syntax to use:

from Rhino import RhinoMath as rm 
print(rm.HalfPI)

I want to say this is part of the Python 3 requiring more explicit: 5 easy tips for switching from Python 2 to 3 | by Practicus AI | Towards Data Science

Yeah, simply that invalidates a bunch of scripts that I now have to go back and modify

1 Like

Scott,

To be picky, as of Python 3.12.2 (the current release version) Python does not require more explicit [whatever], rather it permits traversal of the module hierarchy without explicitly naming all modules by employing a similar dot notation to folder traversal in an OS. (This is only available when using the from… syntax.)

The syntax which Mitch is missing is a legitimate and current syntax as of 3.12 ( 7. Simple statements — Python 3.12.2 documentation). It is not deprecated for future use. Examples from Python Docs:

import foo.bar.baz         # foo, foo.bar, and foo.bar.baz imported, foo bound locally
import foo.bar.baz as fbb  # foo, foo.bar, and foo.bar.baz imported, foo.bar.baz bound as fbb

As Mitch points out, the failure to implement this fundamental piece of Python will cause many people annoyance when working with Python scripts from earlier Rhino versions. It merits fixing.

Regards
Jeremy

@jeremy5

You are correct. And the example @scottd is the correct way to import RhinoMath in Python 3. It’s not really a discussion of conventions.

RhinoMath is a static class (type) and can not sit directly in front of import because it is not a submodule. IronPython allowed this which is confusing. I’ll check Python 3.12 to make sure this is still the case.

So from Rhino import RhinoMath is :+1:

I created a YT to stop suggesting RhinoMath when completing import Rhino. to avoid the confusion :smiley:

RH-81063 Do not suggest static classes on autocompletion

Rhino_BqOijyDiu7

Thanks for reporting this!

1 Like

Tested this in Python 3.12 and it is still the case. classes can not be imported like this:

import test_class.Test

$ python test.py
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    import test_class.Test
ModuleNotFoundError: No module named 'test_class.Test'

This is valid:

from test_class import Test

$ python test.py
<class 'test_class.Test'>
1 Like