Attaching Python Script to Icon (ASCII & Syntax Issues)

Hi everyone,

I have a Python script that runs perfectly in the Script Editor (Tools > Script > Run). However, when I try to attach it to an icon, I get two errors:

  1. First, it complains about a non-ASCII character. After removing special characters,
  2. Then, it gives a syntax error on print(f"Moved {total_moved} grips to Z=0").

Why does this happen? Does the icon interpreter use a different Python version?

Any help would be appreciated!

Rhino 8 can run scripts in either IronPython 2.7 or CPython 3.x. If you do not specify, I think it runs Python 2 by default. To specify one or the other, you need to add the following at the top of the script:

#! python 2 for Python 2

or
#! python 3 for Python 3

In order to run non-ASCII characters in Python 2, you need to have this at the top of the script.

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

In principle the default coding for Python 3 is already UTF-8, so the above is not necessary, but it can’t hurt.

The syntax error looks like you are trying to pass Python 3 formatting code while running in Python 2.

1 Like

Non-Ascii fix: Add this to the header of the script:
# -*- coding: utf-8 -*-

IronPython v2.7 don’t supports f-strings. They are supported only by the Python3.

Here is a version of your f-string code converted for Python 2.7:
print "Moved {} grips to Z=0".format(total_moved)

Thank you, both.