Same exact results on my macOS running Monterey
So how do we mirror the terminal and do shell script environment? That was the original question I was trying to get an answer to it self.
it doesn’t call any external shell script. But it does use
import os
import csv
import subprocess
import shutil
Specifically shutil and subprocess where it executes commands and imports the results.
ex. du -sk, find,ffprobe
I’m going to assume that the PATH variable is the culprit for your errors.
Lets say I need “/usr/local/bin” added to my do shell script.
the default PATH for do shell script is “/usr/bin:/bin:/usr/sbin:/sbin”
to add the folder to my PATH variable before my command i would do this
do shell script "PATH=$PATH:/usr/local/bin ; echo $PATH"
or in your script
do shell script "PATH=$PATH:/usr/local/bin ; cd ~/Desktop/test/ ; python3 test.py"
There is no way to permanently modify the variables, so this would need to be done for every ‘do shell script’ usage.
du
, find
, and ffprobe
are indeed external shell scripts (the python script is calling these external shell scripts). Where subprocess
is being used, if its env
argument is left to the default it will inherit the current environment, which would be what do shell script
provides. In that case, if full paths are not used, anything expecting to be looked up using PATH may fail.
I have repeatedly mentioned source
and export
, which can be added to do shell script
to alter the environment (for that particular invocation). Since your .zshrc
file doesn’t have PATH in it, you can create a configuration file with the desired environment settings and source that, or if all you are going to use is PATH you can just export or set it as previously mentioned, for example:
set envPath to "/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/Library/Apple/usr/bin" -- from Terminal env
set theResult to (do shell script "export PATH=" & quoted form of envPath & "; env #or whatever") -- get 'do shell script' environment variables
Didn’t work. Same results as #2 in the original post. Script executed but the results were not updating the csv as desired like in the terminal app execution.
thanks for sharing this. THIS WAS THE SOLUTION.
Final working code
set envPath to "/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/Library/Apple/usr/bin" -- from Terminal env
set theResult to (do shell script "export PATH=" & quoted form of envPath & "; cd ~/Desktop/test/; python3 test.py") -- get 'do shell script' environment variables
@All - thanks everyone with their responses
@red_menace - thanks for the solution and patience.
edit:
Final working code
set envPath to do shell script "zsh -l -c 'echo $PATH'"
set theResult to (do shell script "export PATH=" & quoted form of envPath & "; cd ~/Desktop/test/; python3 test.py") -- get 'do shell script' environment variables