Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Running Python Code From a File Manager

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.

00:00 Next, running code from a file manager. First, on Windows. So here’s the Windows desktop. Holding down Windows and tapping A will open Explorer, where you can navigate to any file you want to.

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.

00:35 Now, an issue with Windows is you don’t get a simple Open with IDLE command. You can open it with Python, but IDLE doesn’t appear in the list of Other options by default in Windows 10.

00:47 And if you try looking for another app, it’s not immediately apparent where Python is stored. And in fact, because it was installed using the Microsoft Store, it’s difficult to access anyway.

00:57 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.

01:19 Hitting Enter will now open up IDLE, and what you’ll see here is the import of the time module, and then adding time.sleep() at the end to have the window pause before it closes.

01:34 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.

01:50 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 realpython/, and 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:41 Close that window.

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.

03:09 Then every time you run a file, it will ask whether you want to run it or edit it. So double-clicking it now gets that menu. Let’s see what Run in Terminal does.

03:24 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.

04:08 Let’s test that out. Open it, Display it, and then on the second line, we’ll try import time, and on the last line time.sleep() for 3 seconds.

04:36 Now, double-clicking it and Run in Terminal allows the program to run and us enough time to see what happened.

04:48 Now, running code from the file manager in macOS. Here, you can see the Mac desktop. A new window in Finder with Command + N will allow you to navigate to the file in question.

05:00 Here, hello.py is in Recents.

05:06 A double-click on the file launched IDLE, and clicking on the script and then going into the Run menu allows it to be run with Run Module.

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.

05:42 You’ll see more of this sort of thing in the next section.

horacionesman on April 12, 2020

Hi Darren,

First of all thanks for sharing this course, very useful for setting up solid foundations.

I have a question, I wonder why in Windows you didn’t open the file ‘hello.py’ using Notepad or Visual Studio Code? I hadn’t heard of Idle before, that’s a pretty nice tip.

Thanks!

Darren Jones RP Team on April 14, 2020

Hi. You could open up an a variety of editors (although I would avoid Notepad unless there was no other option!), but Idle is typically installed with Python and is cross-platform, so will be available regardless of the system you’re running on. I’m mostly using Code at the moment, but sometimes Atom, Notepad++, PyCharm or Nano.

horacionesman on April 14, 2020

Ok that makes sense, thanks!

Become a Member to join the conversation.