Back to Tutorials

Loading video...

10/31/2025
Cederic Schmid

Actions

Watch on YouTube

Tutorial Details

Category
AI & Development
Difficulty
Beginner

How to Set Up Visual Studio Code for Python Development

Learn how to configure VS Code for Python development with extensions, virtual environments, debugging, and essential features for productive Python coding.

Visual Studio Code (VS Code) has become the most popular code editor for Python development, offering powerful features like IntelliSense, debugging, version control integration, and extensive customization options. Setting up VS Code properly for Python development can significantly improve your productivity and coding experience. This comprehensive guide covers everything from installing essential extensions to configuring virtual environments and debugging tools.

Whether you're a beginner learning Python or an experienced developer switching to VS Code, this guide will help you create a professional Python development environment that rivals dedicated IDEs like PyCharm.

Why VS Code for Python Development

VS Code offers compelling advantages for Python developers:

  • Lightweight: Fast startup and low resource usage
  • Extensible: Thousands of extensions for customization
  • Cross-Platform: Works on Windows, macOS, and Linux
  • Integrated Terminal: Built-in command line access
  • Git Integration: Version control built-in
  • Free: Open-source with no licensing costs

Prerequisites for VS Code Python Setup

  • VS Code Installation: Latest version from code.visualstudio.com
  • Python Installation: Python 3.7+ with PATH configured
  • Internet Connection: For downloading extensions and packages
  • Basic Python Knowledge: Understanding of Python fundamentals

Solution 1: Install Essential Python Extensions

Extensions enhance VS Code's Python capabilities significantly.

Step 1: Open Extensions Marketplace

  1. Launch Visual Studio Code
  2. Click the Extensions icon in the left sidebar (square icon)
  3. Or use keyboard shortcut: Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (Mac)
  4. The Extensions marketplace will open

Step 2: Install Python Extension

  1. In the search bar, type "Python"
  2. Find the official "Python" extension by Microsoft
  3. Click "Install" (it should be the first result)
  4. Wait for the installation to complete
  5. The extension provides IntelliSense, debugging, and more

Step 3: Install Pylance Extension

  1. In the Extensions search, type "Pylance"
  2. Find the "Pylance" extension by Microsoft
  3. Click "Install"
  4. Pylance provides fast, feature-rich language support for Python
  5. It's the default language server for the Python extension

Step 4: Install Python Debugger Extension

  1. Search for "Python Debugger"
  2. Find the "Python Debugger" extension by Microsoft
  3. Click "Install"
  4. This provides enhanced debugging capabilities

Step 5: Install Additional Useful Extensions

  1. Jupyter: For notebook support (ms-toolsai.jupyter)
  2. Python Docstring Generator: Auto-generates docstrings
  3. Python Test Explorer: For running and debugging tests
  4. autoDocstring: Generates Python docstrings
  5. Code Runner: Run code snippets quickly

Solution 2: Verify Python Installation

Ensure Python is properly installed and accessible.

Step 1: Open Integrated Terminal

  1. In VS Code, go to View → Terminal
  2. Or use keyboard shortcut: Ctrl+` (backtick)
  3. The integrated terminal will open at the bottom

Step 2: Check Python Version

  1. In the terminal, type: python --version
  2. Press Enter
  3. You should see Python version (e.g., "Python 3.11.5")
  4. If not recognized, check Python PATH configuration

Step 3: Test Pip Installation

  1. Type: pip --version
  2. Verify pip is working (Python package installer)
  3. Update pip if needed: python -m pip install --upgrade pip

Solution 3: Create Python Virtual Environment

Virtual environments keep project dependencies isolated.

Step 1: Create New Python File

  1. In VS Code, create a new file with .py extension (e.g., test.py)
  2. This tells VS Code you're working with Python
  3. Save the file to create a workspace

Step 2: Open Command Palette

  1. Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac)
  2. Type "Python: Create Environment"
  3. Select the option when it appears

Step 3: Choose Environment Type

  1. Select "Venv" (virtual environment)
  2. Choose your Python interpreter version
  3. VS Code will create a .venv folder in your workspace
  4. A notification will appear when creation is complete

Step 4: Activate Virtual Environment

  1. VS Code should automatically activate the virtual environment
  2. Look for "(.venv)" in the terminal prompt
  3. If not activated, use Command Palette: "Python: Select Interpreter"
  4. Choose the virtual environment interpreter

Solution 4: Configure Python Interpreter

Ensure VS Code uses the correct Python version.

Step 1: Select Python Interpreter

  1. Press Ctrl+Shift+P to open Command Palette
  2. Type "Python: Select Interpreter"
  3. Choose from the list of available interpreters
  4. Select your virtual environment or system Python

Step 2: Verify Interpreter Selection

  1. Look at the bottom-left corner of VS Code
  2. You should see the Python version and environment
  3. Click it to change interpreters if needed

Solution 5: Set Up Python REPL

The Read-Evaluate-Print Loop allows interactive Python execution.

Step 1: Start Python REPL

  1. Open Command Palette (Ctrl+Shift+P)
  2. Type "Python: Start REPL"
  3. A new terminal pane will open with Python prompt

Step 2: Test Interactive Python

  1. Type: print("Hello, VS Code!")
  2. Press Enter
  3. You should see the output immediately
  4. Try calculations: 2 + 2
  5. Exit with exit() or Ctrl+Z

Solution 6: Configure Debugging

Set up debugging for Python code execution.

Step 1: Add Breakpoint

  1. Open a Python file
  2. Click in the gutter (left margin) next to a line number
  3. A red dot appears indicating a breakpoint
  4. Alternatively, press F9 with cursor on the line

Step 2: Start Debugging

  1. Go to Run → Start Debugging
  2. Or press F5
  3. Select "Python File" for debug configuration
  4. Execution will pause at your breakpoint

Step 3: Use Debug Controls

  1. Continue (F5): Resume execution
  2. Step Over (F10): Execute current line and move to next
  3. Step Into (F11): Enter function calls
  4. Step Out (Shift+F11): Exit current function
  5. Use the Variables panel to inspect values

Essential VS Code Shortcuts for Python

Master these keyboard shortcuts for efficient Python development:

Code Editing

  • Ctrl+/: Comment/uncomment lines
  • Alt+Shift+Down: Duplicate line
  • Ctrl+D: Select next occurrence
  • Ctrl+Shift+L: Select all occurrences
  • Alt+Up/Down: Move line up/down

Navigation

  • Ctrl+P: Quick open files
  • Ctrl+Shift+P: Command Palette
  • Ctrl+Tab: Switch between open files
  • Ctrl+G: Go to line number
  • F12: Go to definition

Python Specific

  • Ctrl+Shift+P → "Python: Run Python File in Terminal": Execute current file
  • Ctrl+Shift+P → "Python: Select Interpreter": Change Python version
  • Ctrl+Shift+P → "Python: Start REPL": Open interactive Python

Advanced VS Code Python Configuration

Customize VS Code for optimal Python development experience:

Settings Configuration

  • Format on Save: Automatically format code when saving
  • Linting: Enable pylint or flake8 for code quality
  • IntelliSense: Configure autocomplete behavior
  • Testing: Set up pytest or unittest integration

Workspace Settings

  • Create .vscode/settings.json for project-specific settings
  • Configure Python interpreter per workspace
  • Set up custom tasks and launch configurations

Troubleshooting Common VS Code Python Issues

Resolve frequent problems with VS Code Python setup:

Issue: Python Extension Not Working

Problem: Python features not available in VS Code.

Solutions:

  • Ensure Python extension is installed and enabled
  • Reload VS Code window (Ctrl+Shift+P → "Developer: Reload Window")
  • Check Python is installed and in PATH
  • Verify interpreter is selected correctly
  • Try reinstalling the Python extension

Issue: IntelliSense Not Working

Problem: No autocomplete or code suggestions.

Solutions:

  • Ensure Pylance extension is installed
  • Check Python interpreter is selected
  • Reload VS Code window
  • Try different language server (Pylance vs Jedi)
  • Check if file is saved with .py extension

Issue: Virtual Environment Not Activating

Problem: VS Code not using virtual environment.

Solutions:

  • Use Command Palette to select interpreter
  • Check if .venv folder exists in workspace
  • Try recreating the virtual environment
  • Ensure Python: Venv Path setting is correct
  • Check terminal for activation status

Issue: Debugging Not Working

Problem: Can't start debugging Python files.

Solutions:

  • Install Python Debugger extension
  • Ensure debugpy is installed in environment
  • Check launch.json configuration
  • Try running without debugger first
  • Check for syntax errors in code

Issue: Module Import Errors

Problem: Can't import installed packages.

Solutions:

  • Ensure packages are installed in active environment
  • Check if virtual environment is activated
  • Try installing packages with pip in VS Code terminal
  • Verify Python interpreter selection
  • Check PYTHONPATH settings

Best Practices for VS Code Python Development

Follow these guidelines for professional Python development:

Project Organization

  • Virtual Environments: Use separate venv for each project
  • Requirements Files: Maintain requirements.txt for dependencies
  • Git Integration: Use built-in Git features for version control
  • Workspace Settings: Configure project-specific settings

Code Quality

  • Linting: Enable pylint or flake8 for code analysis
  • Formatting: Use black or autopep8 for consistent formatting
  • Type Hints: Add type annotations for better IntelliSense
  • Docstrings: Write comprehensive function documentation

Productivity Tips

  • Keyboard Shortcuts: Learn and customize shortcuts
  • Snippets: Create custom code snippets
  • Extensions: Install productivity-enhancing extensions
  • Themes: Choose comfortable color schemes

Essential Python Libraries Setup

Install commonly used Python libraries for development:

Data Science Libraries

  1. NumPy: pip install numpy - Numerical computing
  2. Pandas: pip install pandas - Data manipulation
  3. Matplotlib: pip install matplotlib - Data visualization
  4. Jupyter: pip install jupyter - Notebook support
  5. Scikit-learn: pip install scikit-learn - Machine learning

Web Development Libraries

  1. Flask: pip install flask - Lightweight web framework
  2. Django: pip install django - Full-featured web framework
  3. Requests: pip install requests - HTTP library
  4. FastAPI: pip install fastapi - Modern API framework

VS Code Python Alternatives

Consider these alternatives if VS Code doesn't meet your needs:

PyCharm

  • Professional IDE: Full-featured Python IDE
  • Advanced Debugging: Powerful debugging capabilities
  • Django Support: Excellent web framework support
  • Database Tools: Built-in database integration

JupyterLab

  • Notebook Interface: Advanced notebook environment
  • File Browser: Better file management
  • Extensions: Rich extension ecosystem
  • Terminal Integration: Built-in system terminal

Spyder

  • Scientific Computing: Designed for data science
  • Variable Explorer: Interactive variable inspection
  • IPython Console: Enhanced Python console
  • Matplotlib Integration: Built-in plot viewer

Related Tutorials

Conclusion

Setting up Visual Studio Code for Python development creates a powerful, customizable environment that can rival dedicated Python IDEs. By installing the essential extensions, configuring virtual environments, and learning the debugging features, you'll have a professional development setup that enhances productivity and code quality.

The combination of VS Code's lightweight design, extensive extension ecosystem, and Python-specific features makes it an excellent choice for Python development. The integrated terminal, version control support, and debugging capabilities provide everything needed for modern Python programming.

As you become more comfortable with VS Code, explore advanced features like custom settings, workspace configurations, and additional extensions. The flexibility of VS Code allows you to tailor the environment to your specific workflow and preferences.

Whether you're working on data science projects, web applications, or automation scripts, a properly configured VS Code environment will significantly improve your Python development experience. Start coding with confidence!

Tags

vs code python
visual studio code setup
python extensions
python virtual environment
python debugging
vscode python
python development
code editor setup