ScriptEditor (Python 3) quick questions

  1. For being able to use accented characters (utf-8) in Python 2.7 we needed to put this at the top:
#!/usr/bin/env python
# -*- coding: utf-8 -*

Is this still valid for Python 3?

  1. No scroll bar on the right hand side of the editor window?

  2. I think someone already asked about script search paths - I already set mine in the EditPythonScript editor and this seems to work for toolbar buttons with paths to run scripts. There does not seem to be an option for this in ScriptEditor (yet) but I guess if one can set them in EditPythonScript it’s not critical?

Replying to my own question here after some testing…

So, one of the changes to Python 3 is that the print statement in P2 is now a function in P3 - requiring the line to be printed to be enclosed in parentheses

Py2:

print "Hello Rhino"

Py3:

print("Hello Rhino")

OK, fine, but as I am in a country where the various languages use àçcénted characters, in Py2 where I had localized prompts and user feedback, I had to put

#!/usr/bin/env python
# -*- coding: utf-8 -*

at the top to keep it from erroring out. Apparently Py3 does not need that, and in testing that with the new ScriptEditor, I have run into a few funny things. The ScriptEditor can run either Py2 or Py3 scripts by putting #! python 2 or #! python 3 at the top. So I tried first this:

#! python 2

print "àï çéé üû"

The first time I run it, I get the correct thing printed at the command line - and this despite that I did not put the utf-8 thing at the top:

image

What’s interesting is that I see the following in the Terminal window:
image

Where’s that second line (missing the first character) coming from?

Then if I run the script again immediately after I get this:
image

Huh? The script output is concatenated to the weird second line… Every succeeding run prints the same thing.
image

Weird. If I reset the script engine, it runs once correctly and then the same error. Adding the utf-8 argument at the beginning doesn’t help.

OK, so let’s try Py3:

#! python 3
print "àï çéé üû"

I know this is the wrong syntax for Py3, but I want to see what it does…

I get an error “Runtime is not configured”

System.Exception: Runtime is not configured
   at Rhino.Runtime.Code.Languages.PythonNet.CPythonCode.ExecutePython(ExecuteContext context)
   at Rhino.Runtime.Code.Code.Run(ExecuteContext context)

OK I thougt I would just get a compilation error but whatever… Let’s fix the code:

#! python 3
print("àï çéé üû")

Oops - same error… Something else is wrong. Resetting the Python 3 engine doesn’t help.

OK, will close Rhino 8 and reopen to see if that helps. That re-deploys everything, have to wait a couple of secs. Run the above again. Correct result the first time, and each succeeding run, so that’s good. No utf-8 initiation needed.

Let me un-fix the code to see if I can make it error out again

#! python 3
print "àï çéé üû"

This time I get a “Compile Error” as I should. OK, almost done here, going back to Py2 to repeat the error:

#! python 2
print "àï çéé üû"

Now I get a compile error. It should run, but it doesn’t.

Running with parentheses (like Py3 format) works:

#! python 2
print("àï çéé üû")

And multiple times correctly…

So - there seems to be some crosstalk between running Py2 and Py3 scripts and the new editor is getting confused. It seems to be more than a simple affair of putting #! python 2 or #! python 3 at the top…

@eirannejad …

@Helvetosaur Currently when you change the # python specifier to a different version, you have to close and open the script. See the status bar for the actual language that is being used to run the script.

I have a ticket to improve this.

@eirannejad It seems that there is more to it than this…

I created a new template via the Templates section hitting the plus sign and selecting IronPython 2 from the dropdown and naming it “Basic P2”. It creates a blank template with
#! python 2
at the top. I see IronPython 2 @2.7.12 in the status bar.

I then just added the following to the template and saved it:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

so the whole thing looks like this:

#! python 2
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

OK, so I closed all that out. However, I do still have one tab open, that is my “boilerplate” page that has a lot of frequently used code snippets. That one is set up as Python 3, so when the tab is active, I see Python 3 @ 3.9.10 in the status bar.

Now, I go into the templates left hand window and double click on my newly created “Basic P2” template to open a new, blank, untitled Python 2 script with my header I created above.

It opens the file correctly, but - Python 3 @ 3.9.10 is still showing in the status bar. So opening a new script from a template that has been saved as P2 does not work in this case. After closing that and the boilerplate tab that was open- so that I do not have anything open at all, I re-tried opening my P2 template. It still opens showing P3 in the status bar. Even after closing the ScriptEditor and re-opening it, then repeating trying to open a file from the P2 template, it is still on P3.

So, I tried simply closing Rhino 8 and re-opening it… Opened the ScriptEditor and… Templates are gone, the left side panel which had the template files I created listed is now blank. :confused:

Looks like it actually just forgot the last used directory. Navigated back to it. Opened my Basic P2 template. Still shows Python 3 @ 3.9.10 in the status bar. :roll_eyes:

OK, I realized all of the above was with last weeks version - this weeks version did not download automatically. Downloaded manually and installed. Let’s test all that in this week’s version…

So this is on a fresh install of the manually downloaded this week’s Beta, nothing has been done, just opened a blank Rhino file and opened ScriptEditor.

OK, it still lost my Templates directory… sorta. There was nothing shown in the window, but when I hit the button to change the directory, the correct path was still in there - I had to just hit the “Select folder” button again.

Looks like this:

Double clicked on the Basic P2 entry and got this:

So, looks like a couple of issues still need fixing…

1 Like

Ya that definitely looks like a bug. I’ll look into it today. For now change the file extension to .py2. That would make it open with IronPython.

RE RH-77189 Opening a template with #! python 2 hashbang opens the script with python 3

This is maybe not the right thread to put this, but in V7 and earlier, pressing F1 while in the editor window brought up the Rhinoscriptsyntax help in your web browser. This does not currently work in the V8 ScriptEditor. It does still work in EditPythonScript.

At first I thought OK, I understand, this is no longer just about rhinoscriptsyntax or even Python. However, going to the Help menu in the ScriptEditor window and selecting “Help topics…” brings up… the Rhinoscriptsyntax help in your web browser. :stuck_out_tongue_winking_eye:

So, the question is, what should F1 be doing now…?

Maybe the Help menu needs separate entries for both the rhinoscriptsyntax help doc and the RhinoCommon API page (F1 and F2?). Also while you’re there, I think the “Show Help” item in the same menu should read “Show Help Panel” - like the tooltip already says.