- How to Run a Python Script in Windows
- Your First Python Script
- Run Python Code from the Command Prompt
- Run Python Code from an IDE
- Run a Python Script from the File Manager
- Running Python Scripts in Windows: Next Steps
- How to Run or Execute Python Program on Windows
- How to Execute Python Program Using Command Prompt
- Interactive Mode to Execute Python Program
- Using IDLE(Python GUI) to Execute Python Program
How to Run a Python Script in Windows
When it comes to operating systems, there are three main ones for personal computers: Microsoft Windows, Apple’s macOS, and Linux. You can use any of these operating systems to develop and run your Python programs. However, there are subtle differences you need to keep in mind while developing your projects. In this article, we’ll outline how to run a Python script in Windows.
In general, we recommend you work with Python 3, which is the latest version. If you need some help getting Python installed, check out our article How to Install Python on Windows. To make sure you’re up to speed with the terminology we use in this article, take a look at Python Terms Beginners Should Know – Part 1.
If you’re new to programming, a good place to start is our Python Basics track, which contains three useful courses to teach you the fundamentals. We have more course suggestions in the article How to Learn Python Online for Free.
Your First Python Script
To follow along with this article, you’ll need to create your own Python script. This script doesn’t have to be fancy – a ‘Hello, World!’ program will do. Simply open a text editor (such as Notepad) and type:
Just make sure there aren’t any spaces before the print() statement. Then save it as ‘script.py’.
Run Python Code from the Command Prompt
The first way to work with Python in Windows is through an interactive session. To get an interactive session started, just open the Command Prompt. Search for cmd on the toolbar, then hit Enter. Once the command prompt is open, simply type python and hit Enter again.
When you’re in an interactive session, every Python statement is executed immediately and any output is displayed directly beneath. Try typing:
>>> print('Hello, World!') Hello, World!
From here, you can write any Python code you want. To exit the interactive session, type quit() or exit() .
To run the script that we created above, you can call the Python program from the Command Prompt and tell it which file to execute. From the Command Prompt, simply type:
You’ll see the Hello, World! output printed directly to the screen. For more details on this method, check out How to Open and Run Python Files in the Terminal.
This is the most basic method of executing scripts that you should be familiar with. You could develop your whole project in Notepad and run the script in this way. This is totally legit, but for many applications it’s not the easiest method.
Run Python Code from an IDE
A much more useful way to develop and run your Python projects is using an Integrated Development Environment (IDE). For some background reading, check out our article 4 Best Python IDE and Code Editors. My personal recommendation is to use Spyder (which has a nice mix of features and is user-friendly) but any IDE will do. You can download Spyder here.
Once you’ve downloaded and installed it, open Spyder to get started. Since you already have your script written, you just need to open it in the editor. To run the script, simply click the play button in the top toolbar. You should see the output displayed in the interactive console.
Using an IDE to develop and run your Python projects is the most convenient way to work.
Run a Python Script from the File Manager
The final way of running your Python script that we’ll discuss is double-clicking the .py file. This is possibly the least useful option, but it could be convenient once you have fully developed and tested your project. It could also require a little bit of configuration.
You need to ensure your .py file is associated with python.exe. Right click the .py file, select Open with > Python. Now try double-clicking the .py file. You’ll see the Command Prompt flash briefly, then close again. To avoid this, you can add the following line to the end of your script:
input('Press Enter to Continue')
Now the script will stop when you hit the Enter key.
Running Python Scripts in Windows: Next Steps
In this article, we discussed three ways to run your Python scripts in Windows. This is something you’ll need to do often to develop and test your programs.
Learning anything new can be challenging. For some tips on the best ways to learn Python, check out our article 5 Tips for Learning Python from Scratch. With a little bit of consistent effort, you’ll become a Python master in no time!
How to Run or Execute Python Program on Windows
In this tutorial, learn how to execute Python program or code on Windows. Execute Python program on Command prompt or use Python IDLE GUI mode to run Python code.
Create your file in .py extension and execute using the step-step process given here. The steps are given here with pictures to learn in the easiest way.
How to Execute Python Program Using Command Prompt
If you want to create a Python file in .py extension and run. You can use the Windows command prompt to execute the Python code.
Here is the simple code of Python given in the Python file demo.py. It contains only single line code of Python which prints the text “Hello World!” on execution.
So, how you can execute the Python program using the command prompt. To see this, you have to first open the command prompt using the ‘window+r’ keyboard shortcut. Now, type the word ‘cmd’ to open the command prompt.
This opens the command prompt with the screen as given below. Change the directory location to the location where you have just saved your Python .py extension file.
You can use the cmd command ‘cd’ to change the directory location. Use ‘cd..’ to come out of directory and “cd” to come inside of the directory. Get the file location where you saved your Python file.
To execute the Python file, you have to use the keyword ‘Python’ followed by the file name with extension. See the example given in the screen above with the output of the file.
You can also execute the code directly in the interactive mode with the next section.
Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.
Interactive Mode to Execute Python Program
To execute the code directly in the interactive mode. You have to open the interactive mode. Press the window button and type the text “Python”. Click the “Python 3.7(32 bit) Desktop app” as given below to open the interactive mode of Python.
You can type the Python code directly in the Python interactive mode. Here, in the image below contains the print program of Python.
Press the enter button to execute the print code of Python. The output gives the text ‘”Hello World!” after you press the enter button.
Type any code of Python you want to execute and run directly on interactive mode.
However, you can also execute the Python program using the Python GUI. You have to use the below section and follow the examples with pictures.
Using IDLE(Python GUI) to Execute Python Program
Another useful method of executing the Python code. Use the Python IDLE GUI Shell to execute the Python program on Windows system.
Open the Python IDLE shell by pressing the window button of the keyboard. Type “Python” and click the “IDLE(Python 3.7 32 bit)” to open the Python shell.
Create a Python file with .py extension and open it with the Python shell. The file looks like the image given below.
It contains the simple Python code which prints the text “Hello World!”. In order to execute the Python code, you have to open the ‘run’ menu and press the ‘Run Module’ option.
You can also use the keyboard shortcut ‘F5’ to run the Python code file.
A new shell window will open which contains the output of the Python code. Create your own file and execute the Python code using this simple method using Python IDLE.
A Python IDLE mode can also execute the direct code.
To execute the direct code, you have to write the Python code directly on the Python IDLE shell. Here, a print function of Python prints the text “Hello World” on execution.
Hope, you like this tutorial of how to run Python program on windows.