Set Up Visual Studio Code (VS Code) for Python
To write Python code, we will use Visual Studio Code (VS Code) – a modern, lightweight, open-source text editor. It makes development efficient and faster with features like syntax highlighting, autocomplete, error detection, and debugging all in one place.
Here is a step-by-step guide to set up Python in VS Code on Windows, macOS, or Linux:
Step 1: Install Python
First, make sure Python is already installed in your system using the following command:
python --version # Windows
# or
py --version # Windows
# or
python3 --version # macOS/Linux
If it returns a version number like Python 3.x.x (e.g. Python 3.13.7), it means Python is already installed on your system.
If it is not already installed, you can follow the guide “Download and Install Python on Windows, macOS, and Linux” to install Python.
Step 2: Install Visual Studio Code
Download and install Visual Studio Code (VS Code) for your operating system from https://code.visualstudio.com/.
Step 3: Install the Python Extension
- Open VS Code.
- Click the Extensions icon on the left Activity Bar (or press
Ctrl+Shift+Xfor Windows/Linux orCmd+Shift+X for Mac). - Search for
python. - Find the official “Python” extension by Microsoft (it has the highest download count) and click Install.
Step 4: Select the Python Interpreter
An interpreter is the program that reads and executes your Python code. You need to tell VS Code which one to use.
- Open the Command Palette (
Ctrl+Shift+Pon Windows/Linux,Cmd+Shift+Pon Mac). - Type Python: Select Interpreter.
- A list of available Python interpreters will appear. Choose the one you want to use for this project (e.g.
Python 3.13.7).
Step 5: Create and Run a Python File
- Create a new folder
python_projecton your Desktop for your project and open it in VS Code (File>Open Folder…). - Create a new file(File>New Text File).
- Save it as
app.py(File>Save) in yourpython_projectfolder. The.pyextension is crucial. - Copy and paste the following code.
print("Hello from VS Code!") - Click the Run button ▶ in the top right.
- You should see the output
Hello from VS Code!in your terminal.
Set up a Virtual Environment (Recommended)
A virtual environment is a self-contained directory that holds a specific version of Python and all the packages your project needs. This prevents conflicts between projects.
- Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
- Type
Python: Create Environmentand hit Enter. - Choose your environment type. A prompt will appear asking you to choose between
VenvandConda. SelectVenv. - Select a Python interpreter you want to use for this environment from the list (e.g., Python 3.13.7).
- Wait for the environment to be created. VS Code will create a virtual environment, typically in a folder named
.venvinside your project directory.
VS Code will automatically use the interpreter from your new environment in the current workspace. To confirm, open a new terminal (Terminal > New terminal) in VS Code – you should see the virtual environment’s name in parentheses at the start of the command prompt (e.g., (.venv) C:your_project>).
If it doesn’t appear, you can manually select it:
- Open the Command Palette (
Ctrl+Shift+Pon Windows/Linux,Cmd+Shift+Pon Mac). - Type
Python:Select Interpreter. - Choose the interpreter from the
.venvfolder that appears in the list (e.g.,venv/bin/pythonon Mac/Linux or.venv\Scripts\python.exeon Windows).