- 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
- Run Python Script – How to Execute Python Shell Commands in the Terminal
- What is a Shell?
- What is a Terminal?
- What is the Python Shell?
- How to Use the Python Shell
- What Can You Do in the Python Shell?
- How to Run Python Scripts
- Conclusion
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!
Run Python Script – How to Execute Python Shell Commands in the Terminal
Suchandra Datta
When you’re starting out learning a new programming language, your very first program is likely to be one that prints «hello world!».
Let’s say you want to do this in Python. There are two ways of doing it: using the Python shell or writing it as a script and running it in the terminal.
What is a Shell?
An operating system is made up of a bunch of programs. They perform tasks like file handling, memory management, and resource management, and they help your applications run smoothly.
All the work we do on computers, like analyzing data in Excel or playing games, is facilitated by the operating system.
Operating system programs are of two types, called shell and kernel programs.
Kernel programs are the ones who perform the actual tasks, like creating a file or sending interrupts. Shell is another program, whose job is to take input and decide and execute the required kernel program to do the job and show the output.
The shell is also called the command processor.
What is a Terminal?
The terminal is the program that interacts with the shell and allows us to communicate with it via text-based commands. This is why it’s also called the command line.
To access the terminal on Windows, hit the Windows logo + R, type cmd, and press Enter.
To access the terminal on Ubuntu, hit Ctrl + Alt + T.
What is the Python Shell?
Python is an interpreted language. This means that the Python interpreter reads a line of code, executes that line, then repeats this process if there are no errors.
The Python Shell gives you a command line interface you can use to specify commands directly to the Python interpreter in an interactive manner.
You can get a lot of detailed information regarding the Python shell in the official docs.
How to Use the Python Shell
To start the Python shell, simply type python and hit Enter in the terminal:
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>print("hello world!")
The interactive shell is also called REPL which stands for read, evaluate, print, loop. It’ll read each command, evaluate and execute it, print the output for that command if any, and continue this same process repeatedly until you quit the shell.
There are different ways to quit the shell:
- you can hit Ctrl+Z on Windows or Ctrl+D on Unix systems to quit
- use the exit() command
- use the quit() command
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("HELLO WORLD") HELLO WORLD >>> quit() C:\Users\Suchandra Datta>
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit() C:\Users\Suchandra Datta>
C:\Users\Suchandra Datta>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z C:\Users\Suchandra Datta>
What Can You Do in the Python Shell?
You can do pretty much everything that the Python language allows, from using variables, loops, and conditions to defining functions and more.
The >>> is the shell prompt where you type in your commands. If you have commands that span across several lines – for example when you define loops – the shell prints the . characters which signifies that a line continues.
>>> >>> watch_list = ["stranger_things_s1", "stranger_things_s2", "stranger_things_s3","stranger_things_s4"] >>> >>>
Here we defined a list with some TV show names via the Python shell.
Next, let’s define a function that accepts a list of shows and randomly returns a show:
>>> def weekend_party(show_list): . r = random.randint(0, len(show_list)-1) . return show_list[r] .
Note the continuation lines ( . ) of the Python shell here.
Finally to invoke the function from the shell, you simply call the function the way you would do in a script:
>>> weekend_party(watch_list) 'stranger_things_s1' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s3' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s2' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s2' >>> >>> >>> weekend_party(watch_list) 'stranger_things_s3' >>>
You can inspect Python modules from the shell, as shown below:
>>> >>> >>> import numpy >>> numpy.__version__ '1.20.1' >>>
You can see what methods and attributes a module offers by using the dir() method:
>>> >>> x = dir(numpy) >>> len(x) 606 >>> x[0:3] ['ALLOW_THREADS', 'AxisError', 'BUFSIZE']
Here you can see that Numpy has 606 methods and properties in total.
How to Run Python Scripts
The Python shell is useful for executing simple programs or for debugging parts of complex programs.
But really large Python programs with a lot of complexity are written in files with a .py extension, typically called Python scripts. Then you execute them from the terminal using the Python command.
All the commands we executed previously via the shell, we can also write it in a script and run in this way.
Conclusion
In this article, we learnt about the shell, terminal, how to use the Python shell. We also saw how to run Python scripts from the command line.
I hope this article helps you understand what the Python shell is and how you can use it in your day to day lives. Happy learning!