0

(I use Mac OS X and Python version 3.4.3).

When I double-click on the Python program (.py), I don't want the source code to load/open in Python IDLE, I just want the program to run in Python Shell?

For example when I click the Spotify Icon App, it just runs the program and doesn't bring up its source code! I understand this maybe a very simple question, but I am really stuck! Any help will be great! If this is unclear feel free to ask me questions.

Greg
  • 123
  • 1
  • 5
  • Possible duplicate of [Easy way to launch Python scripts with the mouse in OS-X](http://stackoverflow.com/questions/14793391/easy-way-to-launch-python-scripts-with-the-mouse-in-os-x) – Ari Cooper-Davis May 20 '17 at 13:19

1 Answers1

-2

TLDR: You don't.

This isn't normally done; applications that you double click are usually compiled so that the source code isn't there anymore, instead it's been turned into machine-readable instructions for the computer. Since python code is source code, it's simply not designed to be run like an app.

You can compile your python code into an app every time you want to use it, using something like py2app but then the source-code inside isn't easily editable and it may be fiddly to get complex imports working with this, or multiple python versions. I wouldn't get into the habit of this.

Instead, to run a python file, use the Terminal, and type python file_location where file_location is the path to the file. Alternatively you could navigate in the Terminal to the directory containing your python file and then just use python file_name where file_name is the name of your file.

Only compile your python work into an app when it's at its final stage and ready for "release".

For example, what if your app prints something to the command line? Without running it from the command line where would python print that information? Or what if something goes wrong in it and it returns an error; where would that error message go? Also the newer versions of Mac and their "System Integrity Protection" can make life difficult for bundled apps.

Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
  • 1
    Hi Ari, Thank you for your prompt reply! I get what you mean, thank you for explaining this to me. If I was to create a GUI based program and it was completely finished and ready for realise could I compile all of the Python code and then use py2app to make it into a makeshift desktop app (so that I double click the icon and the GUI will load, instead of the source code)? Thank you again! – Greg May 20 '17 at 16:47
  • No problem Greg! Yes, absolutely, when your python work is all done you can use [py2app](https://py2app.readthedocs.io/en/latest/) to make a standalone (i.e. the user doesn't need to install python or all of the libraries you import) app that they can just download and double click. You can use [py2exe](https://pypi.python.org/pypi/py2exe/) for windows systems. Py2app takes a bit of playing with the get it to work, and isn't always very intuitive, but check out the [tutorial](https://py2app.readthedocs.io/en/latest/tutorial.html). – Ari Cooper-Davis May 21 '17 at 10:27
  • 1
    Thats excellent news. If the user was to download the 'pc app' I have created, would they need to download py2app/ py2exe as well? That's my last question, honest! Thank you again Ari! – Greg May 21 '17 at 12:02
  • No worries! No, py2app/py2exe will create a completely standalone application, so all you'll need to distribute would be the app. In your course, though, they'll probably expect you to submit the `.py` files so that they can see how you've written your program. – Ari Cooper-Davis May 21 '17 at 12:26
  • 1
    I understand now! Thank you Ari, I hope you enjoy the rest of your weekend! I think that the lecturers do want us to submit the .py files to be honest, it would make sense, after all they're marking the source code! – Greg May 21 '17 at 12:31