Visual Studio Code (VS Code) is a lightweight and powerful source code editor that supports various programming languages. With the Python extension installed, it provides excellent support for Python development through features like debugging, intelligent code completion, linting, and more. This guide will walk you through installing Python and setting it up in VS Code on Windows, macOS, and Linux.
Table of Contents
Prerequisites
Before you begin, make sure you have:
- A computer running Windows, macOS, or Linux
- Administrator access to install software
- A reliable internet connection
Install Python
First, install the latest version of Python (3.7 or higher) if you don’t already have it.
On Windows:
- Download the Python installer from python.org. Run the installer and check the box to Add Python to PATH during installation.
On macOS:
- Install Python using Homebrew with the command:
brew install python
On Linux:
- Use your distribution’s package manager to install Python. For example on Ubuntu/Debian:
sudo apt install python3
Verify Python is installed properly by opening your terminal and running:
python --version
You should see the installed Python version printed.
Install Visual Studio Code
Next, download and install VS Code for your operating system. Open VS Code after it finishes installing.
Install the Python Extension
The Python extension adds support for Python development in VS Code. To install:
- Open the Extensions view by clicking the Extensions icon in the Activity Bar or press
Ctrl+Shift+X
. - Search for Python and click Install on the top result by Microsoft.
The extension will automatically detect and select your Python interpreter if installed properly.
Select the Python Interpreter
To check or change your selected Python interpreter:
- Open the command palette with
Ctrl+Shift+P
. - Start typing Python: Select Interpreter and choose that command.
- Select your desired Python interpreter from the list.
The path to the interpreter will be displayed in the bottom left corner when viewing a Python file.
Create a Python File
- Open VS Code in an empty project folder using File > Open Folder.
- Create a new file called
hello.py
using File > New File and paste this code:print("Hello, VS Code!")
- Save the file with
Ctrl+S
. The Python extension is now activated for this folder.
Run the Code
To run the code:
- Click the Run Python File in Terminal play button at the top right.
- A terminal opens running the Python file and prints “Hello, VS Code!”.
That covers the basics of setting up a Python environment in VS Code! Continue reading for more advanced configuration.
Configure Linting
Linting analyzes your code to catch errors and potential bugs early. To enable linting:
- Open User Settings with
Ctrl+,
. - Search for
linting
. - Enable both Linting: Enabled and Python › Linting: Pylint Enabled with the checkbox.
Now linting will run automatically as you type and indicate issues right in the editor. I recommend also installing the Pylance and pylint extensions for enhanced linting.
Set Up Debugging
The Python extension includes an interactive debugger to step through your code. To configure:
- Create a
.vscode/launch.json
file by clicking the debug icon in the Activity Bar then create a launch.json file. - Replace the default config with:
{ "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal" }
- Set a breakpoint by clicking next to a line number.
- Press
F5
to start debugging and step through code execution.
See the debugging documentation for advanced debugging features.
Install Useful Extensions
Along with the base Python extension, here are some other great extensions to boost productivity:
- Pylance – Enhanced language support and auto-complete
- Kite AutoComplete AI – Intelligent code completions
- Jupyter – Jupyter notebook support
- Python Test Explorer – Run and debug tests
- Python Docstring Generator – Quickly generate docstrings
- Visual Studio IntelliCode – AI-assisted development
Conclusion
With Python and the Python extension installed, Visual Studio Code provides an excellent open-source Python IDE. Configuring linting, debugging, and useful extensions gives you a highly-productive environment for Python development on any platform.
The Python extension and VS Code offer many more features to explore like unit testing, code formatting, Jupyter notebook support, refactoring, and more. The documentation has additional details on customizing your setup further.
Let me know in the comments if you have any other questions!