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+X
for 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+P
on Windows/Linux,Cmd+Shift+P
on 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_project
on 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_project
folder. The.py
extension 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 a new terminal in VS Code (Terminal>New Terminal).
- Create a virtual environment inside your project folder
python_project
from the terminal.
This creates a# On macOS/Linux
python3 -m venv .venv
# On Windows
python -m venv .venv.venv
folder - Activate it in VS Code.
- Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
- Type
Python:Select Interpreter
. - Choose the interpreter from the
.venv
folder that appears in the list (e.g.,venv/bin/python
on Mac/Linux or.\.venv\Scripts\python.exe
on Windows).
- Activate the virtual environment in the integrated VS Code terminal.
# For macOS/Linux
source .venv/bin/activate
# For Windows
.\.venv\Scripts\activate
Once activated, the terminal prompt displays the environment name (e.g., venv or .venv) at the beginning of the line. You can now run Python commands and install packages, all isolated within the virtual environment.
New Approach from here:
Set up a Virtual Environment (Recommended)
- Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
- Type
Python: Create Environment
and hit Enter. - Choose your environment type. A prompt will appear asking you to choose between
Venv
andConda
. 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
.venv
inside your project directory. - Select the new interpreter. VS Code will automatically select the interpreter from the new environment for your current workspace. If it doesn’t, you can manually select it:
- Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac).
- Type
Python:Select Interpreter
. - Choose the interpreter from the
.venv
folder that appears in the list (e.g.,venv/bin/python
on Mac/Linux or.\.venv\Scripts\python.exe
on Windows).
VS Code will automatically select the interpreter from the new environment for your current workspace. To confirm, open a new terminal (Terminal>New Terminal) in VS Code, the virtual environment’s name will appear in the parentheses at the start of the command prompt (e.g., (.venv) C:\your_project>).