Home Coding & Scripting Setting Up VS Code for Python Development

Setting Up VS Code for Python Development

292
0

Visual Studio Code (VS Code) has quickly become the go-to editor for Python developers. It’s lightweight, highly customizable, and comes with a rich extension ecosystem. If you want to write, test, and debug Python code efficiently, setting up VS Code properly makes all the difference.

This post will guide you step by step—from installing the right tools, to running your first program, and finally fine-tuning your environment for productivity.


1. Install Python and VS Code

Before anything else, make sure you have both installed:


2. Essential Extensions for Python

VS Code is powerful IDEA on its own, but extensions unlock its true potential by adding additional functionality. For the best experience with python development, we will install some extensions in this section. You can go to the extensions page by clicking on the extensions icon (marked as red in below screenshot) on the left sidebar where it will show all installed extensions and option to install new extensions:

For the best experience with python development, install the following extensions:

  • Python (Microsoft) – Core Python support (running code, testing, linting hooks). Installing this also pulls in the Python Extension Pack, which includes:
    • Pylance – fast IntelliSense, autocomplete, and static type checking.
    • Python Debugger – powerful debugging with breakpoints, step-through execution, and variable inspection.
    • Python Environments – helps detect, manage, and switch between interpreters and virtual environments.
  • Jupyter (Microsoft) – (Optional) Run and edit notebooks directly in VS Code. Installing this also pulls in the Jupyter Extension Pack, which includes:
    • Jupyter Keymap – keyboard shortcuts for working with notebooks.
    • Jupyter Notebook Renderers – support for rich outputs (Plotly, Vega, images, etc.).
    • Jupyter Slide Show – present notebooks as interactive slide decks.
    • Jupyter Cell Tags – tagging support for organizing and controlling cells.
  • Ruff (published by Astral Software)– A lightning-fast linter and formatter that can replace Flake8, isort, pyupgrade, and even Black when enabled.

👉 Tip: Jupyter and VS Code may seem like two different tools — notebooks for interactive exploration and VS Code for structured coding — but together they give you the best of both worlds. You can prototype in notebooks (great for data science, teaching, and quick experiments) and then refactor into .py scripts in the same editor when you’re ready for production.

👉 Tip: Black has long been the gold standard for consistent Python formatting, but Ruff builds on that foundation with Black-compatible formatting, lightning-fast linting, and a unified toolchain. Today, it powers projects like Pandas, FastAPI, Airflow, Superset, and Hugging Face. If you prefer Black instead of Ruff, install the Black Formatter (Microsoft) extension — but don’t use both together. Face. If you prefer Black instead of Ruff, install the Black Formatter (Microsoft) extension — but don’t use both together.


3. Creating Project FoLder & Set Up a Virtual Environment (Best Practice)

It’s tempting to just use the global interpreter, but that quickly leads to dependency conflicts between projects. The best practice is to create a virtual environment inside your project folder.

  1. Create a project folder and open it in VS code
    • You can make a new folder (e.g. hello-python-vscode) directly on your computer outside of VS code. Alternatively, you may also create the folder directly from within VS Code.
    • Open this folder in VS Code (File → Open Folder).

💡 Tip for Windows Users

Open PowerShell (or Command Prompt) directly in your project folder by right-clicking on the folder and selecting Open in PowerShell/Command Prompt, or navigate to the folder using the cd command. Once inside the project folder, type:

code .
  1. Create a virtual environment

Open the VS Code terminal using the shortcut Ctrl + ` (tilde) or go to the Terminal menu from the top and click New Terminal.

👉 Hint: If the Terminal menu is not visible at the top, use the three dots (⋮) to make it visible.

Run the following command from Terminal in VS Code:

python -m venv .venv

This creates a .venv folder containing a project-specific Python interpreter.

  1. Activate the virtual environment
    In VS Code, the activation command depends on the terminal type you select in the integrated terminal (run only one of these depending on your terminal):
    Git Bash (Windows):
    source .venv/Scripts/activate
    PowerShell (Windows):
    .venv\Scripts\Activate.ps1
    Command Prompt (Windows):
    .venv\Scripts\activate.bat
    bash/zsh (macOS/Linux):
    source .venv/bin/activate

👉 Tip (Troubleshooting): If activation still doesn’t work, double-check which shell you’re using in VS Code. You can switch between PowerShell, Command Prompt, or Git Bash using the dropdown next to the + in the terminal tab.

👉 Tip (Troubleshooting ): If you see an error like “Could not find platform independent libraries <prefix>” when creating a virtual environment, it usually means your Python install is broken or incomplete. The easiest fix to try is to uninstall and reinstall Python from the official python.org downloads page. Make sure you install the regular installer, not the embeddable zip, and check the option Add Python to PATH during installation.

📘 Read more: For a detailed guide on virtual environments in Python — what they are, how they work, and how to set one up step-by-step — check out this reference: Virtual Environment in Python and How to Set One Up


4. Create Your First Python Program and Setting Interpreter

Now that your environment is ready, let’s write and run a simple Python program inside VS Code.

  1. Create a Python file inside project folder
    • In the Explorer sidebar, make sure your project folder (e.g., hello-python-vscode) is open. Click the “New File” icon (📄 with a +) within that folder in Explorer, and name the file hello.py. Alternatively, you can right-click the folder → New File, or use File → New File → Save As inside the folder. Alternatively, you can create the file directly in the project folder outside of vs code.
  2. Write and save your first program: Click inside the file and type or copy paste:
    print("Hello, World!")
    After typing the code, save the file using Ctrl + S (Windows/Linux) or Cmd + S (Mac).
  3. Select the Python interpreter
    • In the bottom-right corner of VS Code, click the interpreter selector (as marked in red in the below pic) and choose the Python virtual environment you set up earlier. This is needed to use the run (play) button later.

👉 Tip: Once the .venv interpreter is selected, VS Code will keep using it for your project, so you don’t need to select it again for every file.


5. Run Your Program

Click the “Run Python File” button at the top right (as marked in red in below pic).

We should see in the terminal: Hello, World!

We can also run from the integrated terminal as: python hello.py

👉 Tip: Always open your project folder in VS Code, not just individual files. This ensures features like virtual environments, Git, debugging, and linting/formatting (Ruff or Black) work correctly as your project grows.


6. Recommended VS Code Settings

To keep your code clean and consistent, configure VS Code to auto-format and lint your code on save. This is done by editing your settings.json.

  1. Open the Command Palette (Ctrl + Shift + P or Cmd + Shift + P on Mac within top search bar), and search for and select Preferences: Open User Settings (JSON).
  2. Add the following block inside the existing braces { ... }:
  // Format on save
  "editor.formatOnSave": true,

  // Lint and fix with Ruff on save (new extension)
  "editor.codeActionsOnSave": {
    "source.fixAll.ruff": true,
    "source.organizeImports": true
  },

  // Enable type checking with Pylance
  "python.analysis.typeCheckingMode": "basic",

  // Consistent line endings and indentation
  "files.eol": "\n",
  "editor.tabSize": 4

👉 Note when editing settings.json:

  • Don’t add another { } — just insert new settings inside the existing braces.
  • If the last setting in your file doesn’t end with a comma ,, add one before pasting new lines.
  • If a setting (like "editor.formatOnSave": true) already exists, don’t duplicate it — keep just one copy.
  • JSON doesn’t allow duplicate keys or trailing commas, so merge carefully.

👉 Tips:

  • Format on save keeps your code tidy automatically.
  • Ruff fixes and organize imports on save enforce style and cleanup every time you save.
  • Type checking with Pylance helps catch mistakes before running your code.
  • Consistent line endings and indentation prevent cross-platform issues when collaborating.
  • If you prefer Black instead of Ruff, replace the Ruff settings with: "python.formatting.provider": "black", "python.formatting.blackArgs": ["--line-length=88"]
  • ⚠️ Don’t enable both Ruff and Black at the same time — pick one to avoid conflicts.

Conclusion

With the right extensions and a few settings, VS Code transforms into a full-featured Python IDE that remains lightweight and customizable. Whether you’re writing your first print("Hello, World!") or building production systems, this setup ensures you’re working efficiently from day one.


Bonus: Going Beyond Basics

Once you’re comfortable with the essentials, VS Code offers powerful features and extensions to take your development setup further:

  • Debugging in VS Code – Set breakpoints by clicking beside line numbers, run in debug mode (F5), and step through your code to inspect variables and program flow.
  • Jupyter notebooks – Great for data science workflows and interactive coding.
  • Docker extension – Manage and develop inside containers without leaving VS Code.
  • GitHub Copilot – AI-assisted coding to suggest completions and generate boilerplate.
  • Workspace-specific settings – Keep rules and environments project-specific for clean multi-project setups.

👉 Note: The debugger is one of VS Code’s strongest features:

  • Click beside a line number to set a breakpoint.
  • Run in debug mode (F5).
  • Step through your code and inspect variables.

This makes troubleshooting and understanding program flow far easier than using print() statements alone.

Previous articleData Structures in Python (Lists, Dictionaries, Tuples, Sets)
Next articleAnaconda and Miniconda Essentials
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here