Different result from print

I extended @Pascal s script by a single line that prints the current DisplayMode:
print ('Aktiver Anzeigemodus:',sc.doc.Views.ActiveView.ActiveViewport.DisplayMode.LocalName)

Simple thing.

When I run the script from the ScriptEditor, the result in the command area is:
Aktiver Anzeigemodus: Arctic

When I run the same script from a button or shortcut by _-RunPythonScript “T:\Desktop\CycleDisplayModes.py”, this is the result:

(‘Aktiver Anzeigemodus:’, ‘Arctic’)

Are there different Python engines in use?

I need clarification.

CycleDisplayModes.py (1.8 KB)

Without checking I might imagine that running from the ScriptEditor it is running in Py3 whereas from _-RunPythonScript it might be running in Py2…

Why the difference in printing, will have to test…

It’s because Python 2 reads the brackets as a tuple after the print statement, where as in Python 3 that’s a normal function call.

[edit] bracketed expression(s) separated by a comma outside of a function call are a tuple.

Ah ok.
That’s not the problem as long as we know which Python gets used.

In the meantime I verified which version is used when:
_-RunPythonScript → 2.7.12 ( [.NETCoreApp,Version=v3.1 on .NET 7.0.17 (64-bit)]
_-ScriptEditor → 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
_EditPythonScript → 2.7.12 ( [.NETCoreApp,Version=v3.1 on .NET 7.0.17 (64-bit)]

Means _-RunPythonScript doesn’t execute 3.9 scripts.



>>> A B



>>> ('A',  `B`)

I see that. So in order to have a py2 and py3 compatible print statement with multiple items in one line…? This seems rather laborious:

print("{} {}".format("A","B"))

It does, I have at least one script here that uses libraries only available in Py3 and it works fine from an alias using _-RunPythonScript. You need to have #! python 3 at the top of the script.

I tried:

#! python 3
#!/usr/bin/env python 3

No effect when using _-RunPythonScript.

I missed to type the !.

That was me at first, sorry, I edited that later.

While the current confusion is cleared, this issue shouldn’t exist.

When I use the ScriptEditor I expect the script runs exactly the same when calling it by RunPythonScript.

Otherwise it is a bug or glitch.

I don’t know who the Python master is, so please @pascal .

The thing is ScriptEditor can run either Py2 or Py3 depending on which template/model you use. The active version is shown in the lower right hand corner.

RunPythonScript needs to be told which version to use. I think it defaults to Py2 if nothing is specified at the top of the script. Edit: confirmed.

Inside a script, how does one determine which version of Py is running the script? I have a couple of scripts which I can maybe add a switch to do things differently depending on which is running.

I never thought to try it before, but the new IronPython 2 components support __future__, so it’s also possible to:

#! python2
from __future__ import print_function

print('a', 'b', 'c', sep = '_^_', end="\n\n And we're done!!\n\n")

import sys