Using PyCharm for editing RhinoPython

Has anyone got PyCharm to work with Rhino. I am having trouble importing the Rhino Python Libraries.

Hi,

To add the path to the Rhino Python lib that Giulio mentions above in PyCharm

  • select “Settings…” from the “File” menu.
  • click on Project Interpreter / Python Interpreters on the left.
  • make sure IronPython is your selected interpreter (top right).
  • click the “Paths” tab (bottom right)
  • add the path mentioned by Giulio. If the dialog doesn’t let you browse there, you can cut and paste the path from explorer.

Hope that helps
Cheers

2 Likes

I’ve just tested the method proposed by Alain, and it will work great for import rhinoscriptsyntax, which is already helpful. It will not work with import Rhino (RhinoCommon.dll) as it stands. The reason is that there are a couple of quirks in that editor to make IronPython assembly load, and even then not everything will be perfect, but still much better than nothing. If you wish to use Rhino there, do this:

  • add also the path to RhinoCommon.dll (32-bit or not according to the IPy interpreter that you use) to the list of paths (same as in Alain’s answer above). On my system, this is:
    C:\Program Files (x86)\Rhinoceros 5\System\
  • add these few lines of code to your script:
import clr
clr.AddReference("RhinoCommon")
import Rhino
import Rhino.Geometry

  • place the caret (click on) the word “Rhino” besides import
  • press Alt+Enter
  • select “Create skeleton implementation for binary import” (or very similar)
    Now repeat this for all missing imports. You can also use my skeletons and place them in the ‘binary skeletons’ folder. I’ve pre-exported all RhinoCommon namespaces as of today for you in this zip.

Rhino.zip (299.4 KB)

This is what you get:

I hope this might also be helpful,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Do we have to use IronPython as our interpreter? Can we use Anaconda’s python.exe?

IronPython is the interpreter that Rhino uses.

I managed to get all the hints to the methods to appear but when I run my debugger I get an error. See screenshot.


Error Message:
“C:\Program Files (x86)\IronPython 2.7\ipy.exe” C:/Users/marafi/PycharmProjects/RhinoTest/Test.py
Traceback (most recent call last):
File “C:/Users/marafi/PycharmProjects/RhinoTest/Test.py”, line 3, in
File “C:\Users\marafi\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscriptsyntax.py”, line 2, in
File “C:\Users\marafi\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript_init_.py”, line 8, in
File “C:\Users\marafi\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\application.py”, line 1, in
File “C:\Users\marafi\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\scriptcontext.py”, line 2, in
AttributeError: ‘LightException’ object has no attribute ‘Host’

Of course. You need a running instance of Rhino to execute Rhinoscripts. You might be able to run remote debugging in Rhino, though. With this method.

I hope this helps,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

And I believe only the Professional edition of PyCharm supports remote debugging.

I’m still getting this error trying to set up Rhinoscripting with Eclipse + Pydev:
AttributeError: ‘LightException’ object has no attribute ‘Host’

I have Rhino 32-bit up when running the script in Eclipse, but still getting the error.

Basically, I have Exlipse (JUNO) + Pydev + IronPython set up with all the right paths and the builtin as Steve suggested in his tutorial: Configuring Pydev for Rhino.Python | Steve Baer's Notes
After set up, I create a new Pydev project and a new module within where I paste in a piece of python code I’ve successfully ran in Rhino’s built-in Python editor. An instance of Rhino 32-bit is also open at the same time. But still getting the error.

Any ideas what I’m doing wrong?

Hi.
You can edit the script in PyCharm or PyDev but you can’t execute it directly from there. The script has to run in the context of Rhino. You can setup your IDE for remote debugging to step into your code like described above or in my reply to your other post.

Hi @Alain,

I want to use Pycharm Community edition to write and edit python code I write for GhPython components in grasshopper. I stumbled upon this thread during my search for possible solutions. I want to follow the steps you mentioned. However, in the list of project interpreters I don’t see IronPython. I tried to use it from my local drive, but coould not successfullly locate a .exe file for IronPython. I could only locate a .dll file.

can you please help?
Thanks,
Devang

@Devang_Chauhan it would be better to start a new topic. This one was about _EditPythonScript. If you want to reply, please choose the link icon -> then New Topic . If your goal is just writing, then it would be easy to just add a File Reader, and input the content into the ‘Code’ input of a GhPython component.

Hi all,
I have started using Pycharm Community Edition to edit my python scripts for Rhino and then executing them as normal in Rhino 5 using EditPythonScript or RunPythonScript.
I forked this repo https://github.com/gtalarico/ironpython-stubs and imported it into Pycharm (VCS - checkout - github) to get ‘stub’ files giving the autocomplete and quicklook <ctrl + Q> information for rhinoscriptsyntax, Rhino, clr, scriptcontext, etc. Please note that the currently included stubs are for Rhino 5 and include only documentation, but no code so you can’t execute them. I havent tried using it to generate my own stubs but it should be possible to do this for Rhino 6.
I then started a new project in Pycharm, went to ‘project structure’ in settings and pointed to the /release/stubs folder as a ‘Content Root’. This lets Pycharm know where to look for docstrings
Finally under Inspections I checked Python 2.7 and unchecked all other versions of Python.
Seems to be working well so far… Any advice / comments welcome on this if you have another way of doing thing.

3 Likes

alternatively…

2 Likes

Hi @piac @Alain,

thanks for the instructions. I can get rhinoscriptsyntax autocomplete to work in pyCharm with this method, but I still get warning ‘no module named rhinoscriptsyntax’ in the ‘problems’ tab. Is this normal, or is there a way to solve this?

thanks!

I am trying to use PyCharm 2021 to edit RhinoPython and rhinoscript. rhinoscript is working, but not Rhino. I followed some of the suggestions above, but am not sure if they are relevant since they are a few years old. From a different thread suggestion, I tried to pip install Rhino-stubs.

I couldn’t find an IronPython exe file (Rhino 6 or 7), but also don’t know if the interpreter matters just for doing code completion.

Are there some updated steps to follow to get PyCharm code completion working with Rhino?

Ah. I needed to add Rhino-stubs as a package.

Hi Mitch,

Did you have problem of importing Rhino in Pycharm? I installed Rhino-stubs sucessfully, but when I write “import Rhino.Geometry” in the code. it always tells me ModuleNotFoundError: No module named ‘Rhino’

I’m curious how did you import this package and make it work sucessfully.

Thanks

It’s been a while. But, in my project, I just tried importing Rhino.Geometry and it looks like that works fine.

When I look in Settings…Project…Python interpreter, I see Rhino-stubs in the list of packages. Do you see that?

Sometimes, it’s a bit mysterious how to get PyCharm to recognize imports and do the syntax highlighting. Random things which might work are File…Invalidate caches or restarting PyCharm. I don’t think I have anything else to suggest. Good luck.

Thanks for the suggestion.

Yes I can see Rhino-stubs in the list of packages.

I installed another package “rhinoinside” and wrote this:
import rhinoinside
rhinoinside.load()

not sure if thses two packages are relevent, but importing Rhino works okay now.