Skip to content

Module 1 · Introduction to Python — topic 9 of 14

Setting up VS Code

Install Visual Studio Code, open a project folder, create a Python file, choose the interpreter and run code, so you have a comfortable editor for the rest of the course.

Beginner 15 min read·Lesson 9 of 306 in Python Programming

Learning objectives

  • Install VS Code and open a folder as a workspace
  • Create and save a .py file
  • Select the Python interpreter and run a file

Definition

Install Visual Studio Code, open a project folder, create a Python file, choose the interpreter and run code, so you have a comfortable editor for the rest of the course.

Introduction

You can write Python in any text editor, but a code editor makes it far easier. Visual Studio Code (VS Code) is free, runs on Windows, macOS and Linux, and has excellent Python support.

A good editor colours your code, warns about mistakes as you type and runs programs with one click.

Why it matters

Programmers spend hours in their editor every day. Learning a few editor features early, such as the integrated terminal and the Run button, saves time on every exercise and project in this course.

Detailed explanation

Install: download VS Code from code.visualstudio.com and run the installer. On Windows, tick “Add to PATH” and “Open with Code” options if offered.

Open a folder: use File → Open Folder and choose (or create) a folder such as python-course. VS Code treats this folder as your workspace, and the Explorer panel shows its files.

Create a file: in the Explorer click the New File icon and name it hello.py. The .py extension tells VS Code it is a Python file.

Choose the interpreter: press Ctrl+Shift+P (Cmd+Shift+P on macOS), type “Python: Select Interpreter” and pick the Python 3 you installed.

Run: click the ▶ Run Python File button at the top right, or open the integrated terminal with Ctrl+` and type python hello.py.

Save often

Turn on File → Auto Save so the file on disk always matches what you see.

Syntax

Terminal
cd python-course
python hello.py
  • cd moves the terminal into your project folder.
  • python hello.py runs the file with the interpreter.
  • Use python3 instead of python on macOS and Linux.

Code examples

Your first file in VS Code (hello.py)

Python
print("Running from VS Code")
Output
Running from VS Code

Several lines run in order

Python
print("Step 1: open folder")
print("Step 2: create file")
print("Step 3: run it")
Output
Step 1: open folder
Step 2: create file
Step 3: run it

More examples

A file that reports where it runs (about.py)

Python
import os
import sys
print("Working folder exists:", os.path.isdir(os.getcwd()))
print("Python major version:", sys.version_info.major)
print("Running as a script:", __name__ == "__main__")
Output
Working folder exists: True
Python major version: 3
Running as a script: True

Step-by-step explanation

  • os.getcwd() returns the folder the terminal was in when you ran the file; os.path.isdir confirms it exists.
  • sys.version_info.major confirms VS Code used Python 3.
  • __name__ is "__main__" when a file is run directly, which you will use later to organise programs.

Expected output

Running “A file that reports where it runs (about.py)” prints exactly:

TextOutput
Working folder exists: True
Python major version: 3
Running as a script: True

Practical applications

  • Professional developers use the same VS Code setup with extra extensions for linting and testing.
  • Teams share VS Code settings in a .vscode folder so everyone formats code the same way.
  • Remote extensions let developers edit code that lives on a server as if it were local.

Best practices

  • Keep each project in its own folder and open that folder in VS Code.
  • Use the integrated terminal so you stay in one window.
  • Name files in lowercase with underscores, for example grade_calculator.py.

Common mistakes

Running a file that isn’t saved

Avoid

Editing hello.py and pressing Run with unsaved changes (dot on the tab).

Prefer

Save with Ctrl+S (or enable Auto Save) before running.

The interpreter runs the file on disk, not the unsaved text in the editor.

Terminal in the wrong folder

Avoid

python hello.py → can't open file 'hello.py'

Prefer

cd into the folder that contains hello.py first.

Relative file names are looked up in the terminal’s current folder.

Key takeaways

  • VS Code is a free editor with strong Python support.
  • Open a folder, create a .py file, select the interpreter, then run.
  • Save before running; the interpreter reads the file on disk.

Try it yourself

  1. 1
    Create a folder called python-course, open it in VS Code and run a file that prints three facts about yourself.
  2. 2
    Run the same file from the integrated terminal with python (or python3) and the file name.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

8 questions cover the “Introduction to Python” module. Answers and explanations are revealed after you submit.

  • BeginnerPython is best described as which kind of language?
  • BeginnerWho created Python?
  • BeginnerHow does Python group statements into blocks?
Practise this module