Learn how to run scripts by double-clicking on its icon in a file manager.Running a script from the file manager depends on many factors (such as the operating system, the file manager, execution permissions, file associations), this lesson will show you how to run a script on different platforms and what considerations to make before running the script.
Running Python Code From a File Manager
00:19 With this default installation, double-clicking the file you can see it runs—or appears to run—very briefly, but then just disappears. What’s needed is a modification to the script to make it pause so you can see what it’s done before it closes.
So you need another way to approach the problem. A quick way to do that is to hold down the Shift key and then right-click, and you’ll see the option Open PowerShell. And once you do that, you’ll see a PowerShell window which is already in the right directory, and you can type
idle and the name of the script and hit Tab to get the
.\ filled in for you.
Save with Control + S, close it with the X or with Alt + F4, and close the PowerShell window just for neatness. And now running the script, you can see
Hello World! and it pauses for a few seconds before it closes.
Running from the file manager on Ubuntu. The file manager is on the left of the desktop on Ubuntu, and then you can navigate to the script’s location. Here, it’s in
running_scripts/. It looks like it has a Python icon.
02:07 What happens when you click it? You get an editor, which is kind of useful but not exactly what was needed at this point. So a couple of changes are needed to make the file manager behave in the way we want.
02:23 I’m going to close the text editor. First things first, right-click on the file, go to Properties, then Permissions, and tick the Execute box, which as it says there, allows executing the file as a program.
02:48 Double-clicking it still gets the text editor, so you’re not quite there yet. Next, go to the Files and then Preferences menu to change the file manager preferences. And under Behavior, change Executable Text Files, which Python files are, to Ask what to do.
Nothing, it transpires. You’ve still got one more step to go. So this time, open it, and edit it. And you need to add the following line. So this tells the operating system to use
python3 to interpret the contents of this file.
03:51 And you can see the icon has changed, but now when it’s double-clicked and Run in Terminal, you still don’t see anything. But maybe as we’ve already seen, it’s running so fast we’re not actually seeing what’s happening.
05:17 And you can see the effect of it running in the Python shell. It’s sometimes nice to arrange the windows so you can see the editor and the shell at the same time, and you can make changes to your program and see the effect of those changes straightaway. However, note that really, you’re not running the program straight from the file manager, but actually launching IDLE—the Python IDE—and then running it from there.
Become a Member to join the conversation.