Introduction
Once you have installed Python and VS Code, the next step is to run your first Python script. This guide covers how to create, write, and execute a simple Python program inside VS Code.
Step 1: Create a New Python File
- Open VS Code.
- Click File → New File.
- Save the file with a
.py
extension, such ashello.py
.
Step 2: Write a Simple Python Script
- Open the newly created file and enter the following code:
print("Hello, Python!")
Step 3: Run the Python Script in VS Code
There are multiple ways to run a Python script in VS Code:
Option 1: Using the Integrated Terminal
- Open the terminal in VS Code using
Ctrl + `
(Windows/Linux) orCmd + `
(macOS). - Ensure you’re in the correct directory using
cd
Example:cd path/to/your/project
- Run the script with:
python hello.py
Option 2: Using the “Run” Button
- Click the Run ▶ button at the top-right of VS Code.
- Ensure “Python File” is selected in the dropdown.
- Your script will execute, and the output will appear in the terminal.
Step 4: Verify the Output
- If everything is set up correctly, the terminal should display:
Hello, Python!
- If you see an error like
python: command not found
, ensure Python is installed and correctly added to the system PATH.
Final Thoughts
Running your first Python script in VS Code is an exciting step toward learning Python development. Whether using the terminal or the run button, this method ensures smooth execution of your code.
Next Step: Learn how to debug Python scripts in VS Code in our next guide: How Do I Create and Activate a Virtual Environment?