Why is that on Windows, you can directly use executables - that have been enabled for shell use, with an environment path -, in an IronPython subprocess ( e.g. subprocess.Popen(["mytool", ...]) )?
On macOS, you need to specify the absolute path of the tool ( e.g. subprocess.Popen(["/usr/local/bin/mytool", ...]) ), which in many cases isn’t very handy! For instance, you can’t even check if the tool was installed or not, when you don’t know its path!?
To be clear, the tool also works on macOS, if you call it in bash by name (without the absolute path)!
In principal, I get why this doesn’t work. IronPython is installed as a separate, encapsulated Python installation within Rhino, but why does it work on Windows? And is there a way to fix this on macOS?
I don’t know the answer to your question about why this does work, I could only speculate.
Can you subprocess.Popen(["which", "mytool"] on macOS? That should return /usr/local/bin/mytool if that tool is your path (obviously /usr/local/bin should be). A return value from which gives you some assurance that the tool is installed and you can pass that path into your subsequent subprocess calls.
Thanks for your reply, Dan! I had nearly given up.
No, that’s not possible, although the tool is exactly installed there and readily available in macOS shell.
What I believe is happening is that the subprocess module from IronPython doesn’t run a system Unix shell per se (?), but some kind of quarantined (the irony ), emulated (?) shell, that has no access to or knowledge of the bash profile, where the $PATH variables are defined (?).
I’m sure you know this, but the bash profile, which resides in the home directory on Unix based systems, defines where the shell environment should look for tools.
On Windows, the subprocess module seems to handle things differently. When properly installed Command Line tools can be called by name. Maybe the roots of IronPython run deeper on its maker’s territory, or it even runs natively (since Windows doesn’t come with a default CPython installation, like macOS or Linux distros do)?
Man, I really wish McNeel would turn its back on .Net and IronPython, but I guess at this state we are at a point of no return.
Damn. It was worth a shot. I’m looking for the :sad_trombone: emoji here, but not finding it. Well, I’m out-of-ideas on this one. Perhaps @stevebaer can think of a way around it.
you’ll find that PATH is very, very minimal when Rhino is started from the launcher.
If on the other hand you start Rhino from a terminal (that has PATH already correctly set up), you’ll find that you’ll get a whole lot more environment variables - including the PATH you need.
I don’t know if it is possible to have Rhino start up with the environment from the default shell set up, but currently iit doesn’t get that from the launcher.
Yes, I’d already tried that, but it did not make a relevant difference. When reading through the documentation of the subprocess module, I also found a note indicating that using a shell is a potential security risk, because malicious code could be injected between processes.
Thanks for your reply, Steve!
You’re absolutely right!
Ah, I recognise this from my Blender scripting workflow, where I needed to make an alias and start Blender from a shell to get a console output for debugging.
(On Windows, you can open a Command Line window from Blender, but on macOS you can’t.)
I can confirm that it’s possible! You just have to add an alias to your bash profile: alias rhino="/Applications/Rhinoceros.app/Contents/MacOS/Rhinoceros"
Rhino crashes though if you close the shell before exiting the app.
The EditPythonScript and RunPythonScript commands seem to only output to the Rhino command history (not the bash shell that’s still running), but you’re right the path information extracted from os.environ is much more detailed now.
Thanks for sharing this! Great stuff! Much appreciated!
I don’t know if it’s useful for this case though, since I’m developing a Grasshopper component and I can’t really ask of futur users to go through all of this.
Great information though thank you very much!!
I guess I have to come up with something entirely different.
The easiest way would be to provide your tool executable alongside the gha. Then you can figure out the location of the gha and use that to set up the absolute path on both Windows and MacOS.