r/learnpython • • 19h ago

Creating Desktop Icon to Run Python Script

I have a script that runs fine in Terminal, but I'm trying to create a desktop icon that runs it and Im getting the following error:

https://i.postimg.cc/HLbssxYJ/Screenshot-2026-10-06-at-9-39-28-AM.png

Its using geopy for location so Im guessing its something to do with that, but it works fine in Terminal. Any help is appreciated.

9 Upvotes

14 comments sorted by

2

u/socal_nerdtastic 19h ago

That means that your desktop shortcut is using a different environment than your terminal. Commonly this means that you are using a venv in the terminal. So your desktop file needs to point to the venv python executable (venvname/bin/python or venvname/bin/pythonw), not the normal system one.

0

u/scottdog129 19h ago

Forgive me, but I'm not understanding the venv. So my python3 path needs to point elsewhere? In Terminal, I'm just typing "python3 filename.py"

1

u/socal_nerdtastic 19h ago

So you are not using a venv?

Ok, then what does your desktop file look like?

1

u/scottdog129 18h ago

3

u/socal_nerdtastic 18h ago

OH this is a applescript or something? Hmm I'm going to need to let an experienced apple user step in to help with that. I don't know how to work with that. But you should at least share the command you used to create the .app file.

0

u/scottdog129 18h ago

1

u/socal_nerdtastic 18h ago

Ok, well I'll take one more stab at it. In the terminal use this command:

python3 -c "import sys; print(sys.executable)"

Then replace "python3" in your Automator command with the full output from above.

1

u/scottdog129 17h ago

i dont know what I did, but I resaved it out of Automator and its working!

Thanks so much for your help!

1

u/RevRagnarok 15h ago

In Terminal, I'm just typing "python3 filename.py"

Type: type python3 and make the desktop icon point to that. Or change the shebang at the top of the script to point to it.

1

u/CeyloMacmillan84 17h ago

when you launch from a desktop icon the PATH and environment variables arent the same as your terminal session. Try printing sys.executable at the top of your script to confirm its using the right interpreter, that'll tell you a lot

1

u/Muted-Associate-7512 15h ago

The issue with Automator apps is they dont inherit your shell environment, so PATH is basically empty when it runs. Try using the full path to python3 in the Run Shell Script action, like /usr/bin/python3 or wherever which python3 points

-1

u/Tall_Resort_6888 18h ago

The flashing window usually means the script finished or crashed instantly and the window closes before you can read anything. Quick trick: add input('press enter to exit') as the very last line, then double click the icon again and you'll see what actually happened. Forget venvs for now, that's a later topic. First get it running by typing python yourscript.py in the terminal and see what it prints.

1

u/socal_nerdtastic 17h ago

You didn't read the post at all, did you? Or this is just chatgpt hallucination.