How to run a Python script in Windows? This post from MiniTool Partition Wizard tells you what Python script is, what Python interpreter is, and how to run Python script from command line, Interactive Mode, an IDE, and a text editor.
Introduction to Python Script
What is the script? In computer programming, a script is a program or sequence of instructions usually stored as text. It is interpreted or carried out sequentially when called by the operating system’s command interpreter. It automates interactive operations that would otherwise be done with the keyboard, saving a lot of work.
What is Python script? Python is a scripting language. It can be used to write a script. A Python script is a text file containing Python code. In most cases, the suffix of the Python script file is .py. This suffix doesn’t have any special meaning. It is just used to help users distinguish text files containing Python code from other normal text files containing normal articles.
Introduction to Python Interpreter
To run Python script, a Python interpreter is needed. It is a program that reads and analyzes text files (Python scripts) and translates the code in them into computer instructions according to the rules of the Python language.
However, depending on the Python implementation you use, you need to use different Python interpreters like CPython, Jython, IronPython, PyPy, etc. Why are there so many Python interpreters? The reason is that a program may be programmed in multiple programming languages.
If you need to program software in Python and C, you need to download and install CPython, which is the most popular Python interpreter. If you want Python to work with Java, use Jython; want Python to work with .NET languages, use IronPython. If you only Python language to program your software, you can use PyPy.
How to Run a Python Script on Windows
How to run Python script in Windows? You can use the following ways.
Way 1. Run Python Script from Command Line
If you have a Python script, its name may be #.py or #.pyw. To run this Python script, you just need to open Command Prompt and execute the following command: python #.py. If you are using Python 3, the command should be python3 #.py.
On recent versions of Windows, you can even run Python scripts by simply entering the name of the Python script file at the Command Prompt.
If you want to save the output of a script for later analysis, you can execute this command: python #.py > output.txt. This operation redirects the output of your script to output.txt. If output.txt doesn’t exist, then it’s automatically created. But if the file already exists, then its contents will be replaced with the new output.
Way 2. Run Python Scripts Interactively
Open Command Prompt, type in python or python3, and then hit Enter to start a Python interactive session. Then, you can run your script line by line in a sequence. In addition, you can also use the import command to run a Python script in Interactive Mode.
But note that this method only works when the Python script is located in your current working directory and it is in the Python Module Search Path (PMSP), where Python looks for the modules and packages you import. Besides, this option works only once per session. After the first import, successive import executions do nothing, even if you modify the content of the module.
Way 3. Run Python Scripts from an IDE or a Text Editor
An integrated development environment (IDE) or an advanced text editor usually offers the possibility of running your scripts from inside the environment itself. Python’s standard distribution includes IDLE as the default IDE. You can use it to run your scripts. You can take a look at its documentation to learn how to use it.
Way 4. Run Python Scripts by Double-Click
If you associate the extensions .py and .pyw with the programs python.exe and pythonw.exe respectively, you can run your scripts by double-clicking on them.
Bottom Line
MiniTool Partition Wizard can help you clone the system, manage disks better, and recover data. If you have this need, you can download it from the official website.