Python scripts (
.py
files) can be executed in different ways, depending on your operating system and setup. Here’s a step-by-step guide for running a Python file in Windows.
🔹 Step 1: Create a Python File
- Open any text editor (IDLE, Notepad++, VS Code, PyCharm, etc.).
- Write your Python code:
print("Hello, World!")
- Save the file as
hello.py
(Make sure the extension is.py
).
🔹 Step 2: Run the Python File
🖥️ Method 1: Using IDLE
- Open IDLE.
- Click “File” → “Open” and select your
hello.py
file.- Click “Run” → “Run Module” (
F5
).- The output appears in the IDLE shell.
💻 Method 2: Using Command Prompt (CMD)
- Open Command Prompt (
Win + R
, typecmd
, hit Enter).- Navigate to the folder where your script is saved. Example:
cd C:\Users\YourName\Documents
- Run the script using Python:
python hello.py
- Output:
Hello, World!
📂 Method 3: Double-Click the File
- If Python is installed properly, double-clicking
hello.py
will execute it.- The Command Prompt may close immediately after running, so use
input()
at the end:print("Hello, World!") input("Press Enter to exit...")
This keeps the window open until you press Enter.
🖥️ Method 4: Running in VS Code
- Open VS Code and install the Python extension.
- Open
hello.py
.- Press
Ctrl + Shift + P
→ Select “Python: Select Interpreter”.- Click the Run button (
▶
) or pressF5
.
🔹 Running a Python File with Arguments
If your script requires arguments, pass them in CMD:
import sys print("Arguments:", sys.argv)
Run it:
python hello.py arg1 arg2
Output:
Arguments: ['hello.py', 'arg1', 'arg2']
✅ Conclusion
You can run Python scripts using IDLE, CMD, VS Code, or by double-clicking the file. CMD is the most common method for running scripts.
Would you like help setting up automation for running scripts? 😊
Leave a Reply