Run python - Terminal app vs do shell script - inconsistency

Question

  • How do I run the AppleScript code that runs perfectly in the terminal but not in the “do shell script”?

Code scenarios that work not work

  1. WORKING:
    This code works but don’t want to use terminal to run it.
tell application "Terminal"
activate
do script "cd ~/Desktop/test/; python3 test.py"
end tell

  1. RUNNING but NOT WORKING:
    This code runs but the desired output is not occurring…
do shell script "cd ~/Desktop/test/; python3 test.py
  1. NOT RUNNING
    This code doesn’t run but does give some potential insight.

set desktop_folder to "$HOME/Desktop"

do shell script "python3 " & desktop_folder & "/test/test.py"

> Dialog error:

Traceback (most recent call last):
File “/Users/myUser/Desktop/test/test.py”, line 276, in
main()

> Result error:

error “Traceback (most recent call last):
File "/Users/myUser/Desktop/test/test.py", line 276, in
main()
File "/Users/myUser/Desktop/test/test.p", line 260, in main
generate_detailed_file_s(root_directory, all_folders, detailed_output_csv)
File "/Users/myUser/Desktop/test/test.p", line 107, in generate_detailed_file_s
save_to_csv(detailed_data, detailed_output_csv, append=True)
File "/Users/myUser/Desktop/test/test.p", line 114, in save_to_csv
with open(output_csv, mode=mode, newline=‘’) as file:
OSError: [Errno 30] Read-only file system: ‘OUTPUT LIsting.csv’” number 1

Environment variables are different among Terminal and do shell script command.
If you execute “env” command in each environment, you’ll get another results.

https://piyomarusoft.booth.pm/items/3892616

Yes, that is clear they are different. I’m trying to find out how to invoke the terminal environment with the do shell to get the same results.

How would you set this ENV command in AppleScript?

could you post the script “test.py”

also, what environment variables does the python script need that differ in each environment?

Unfortunately I can’t since it has some things I can’t share publicly.

but the real question I’m trying to understand is using the terminal app vs do shell script command how come they aren’t behaving the same results?

some shared there is some setting in the terminal app that the do shell command isn’t using?

is there a way to use the terminal setup in the do shell script?

The usual syntax is

set desktop_folder to POSIX path of (path to desktop)
do shell script "python3" & space & desktop_folder & "test/test.py"

space is syntactic sugar, it does the same as the literal space character.

Then I recommend you peruse your script and make a list of all the Environment variables it is using.

@StefanK unfortunately that gave the same " > Result error:" posted in the initial message.

@robertfern, sure I could do that but to what benefit? It seems like the do shell script env is different that the terminal app… Since the terminal app is working but the do shell script isn’t.

So from what I can see there is something the terminal app is using that the do shell script isn’t.

Can I add some parameters so that the do shell script can use the same as the terminal app?

Because they are different, at least we can determine what variable is missing in the “do shell script” environment, and then fix it.

Would the variable you’re mentioning the variable from the python script or an AppleScript variable?

I suspect something In how the do shell script declaration is the issue. Just don’t know how to figure it out. Is there a way to some how use the setup in terminal app and use it to pass it through AppleScript to run the command similar to the terminal app?

I’m referring to a Unix environment variable.
You used one in your script above.
$HOME is one of them

The env command tells you what the shell environment is.

In applescript, you would run it using do shell script.

do shell script "env"

Then you can compare the result with that which you get when you run env in the terminal.

You might consider bringing this to the attention of someone at your company who has some experience working with the shell. There can always be security implications to consider.

For some insight into how applescript interacts with the shell, apple has provided a technote: Technical Note TN2065: do shell script in AppleScript

ENV output for terminal and script editor
the only thing I see is PWD is different.

Terminal

__CFBundleIdentifier=com.apple.Terminal
TMPDIR=/var/folders/vj/f2jl3p0569z35b48pvy64sk40000gp/T/
XPC_FLAGS=0x0
TERM=xterm-256color
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.vzqXZkN2aw/Listeners
XPC_SERVICE_NAME=0
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=445.1
TERM_SESSION_ID=594AC63D-AD23-4982-9253-0BBB4B7C55CE
SHELL=/bin/zsh
HOME=/Users/
LOGNAME=
USER=
PATH=/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
SHLVL=1
PWD=/usr/local/bin
OLDPWD=/Users/
LANG=en_US.UTF-8
_=/usr/bin/env

Do shell script

SHELL=/bin/zsh
TMPDIR=/var/folders/vj/f2jl3p0569z35b48pvy64sk40000gp/T/
USER=
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.vzqXZkN2aw/Listeners
__CF_USER_TEXT_ENCODING=0x1F6:0x0:0x0
PATH=/usr/bin:/bin:/usr/sbin:/sbin
__CFBundleIdentifier=com.apple.ScriptEditor2
PWD=/
XPC_FLAGS=0x0
XPC_SERVICE_NAME=application.com.apple.ScriptEditor2.1152921500311981950.1152921500311981955
SHLVL=1
HOME=/Users/
LOGNAME=
_=/usr/bin/env

The result error looks like a read/write error. Does Script Editor have Full Disk Access?

Where is your python3 executable located? On my system, it’s in /usr/local/bin, which is part of your PATH in Terminal but not when you try to run via do shell script. You might need to specify the path to the executable in your AS.

in do shell script, the PATH variable is
PATH=/usr/bin:/bin:/usr/sbin:/sbin

When I run 'do shell script “Whereis python3” ', I get /usr/bin/python3.
So it is in the PATH

I’m on Ventura

The Technical Note link provided by @Mockman documents what do shell script uses. Your do shell script environment listing shows a different PATH than what Terminal is using - /opt/local/bin, /opt/local/sbin, /usr/local/bin and /Library/Apple/usr/bin will not be searched unless your shell script adds them to its default PATH (source, export, etc) or you use explicit paths.

A sub shell usually gets environment variables from the parent shell, so does your Python script perhaps also call shell scripts?

Yes it does and I did verify it also.
Screen Shot 2025-02-19 at 10.28.43 AM

which python3

/usr/bin/python3

/usr/bin/python3 or python3 give the same error " > Result error:" in the first post using 3 code.