Setting Up Visual Studio Code for Python Development

Jul 13, 2024

Lecture Notes: Setting Up Visual Studio Code for Python Development

Introduction

  • Objective: Learn how to install Visual Studio Code (VS Code), set up a Python development environment, and explore its features.
  • Target Audience: Python developers
  • Host Mention: Talk Python podcast with Michael Kennedy
  • Focus Shift: Increasing popularity of VS Code among developers

Installation and Initial Setup

Visual Studio Code Installation

  1. Access Website: code.visualstudio.com
  2. Download and Install: Follow standard software installation procedures
    • Accept terms
    • Select install location
    • Create desktop icon
    • Launch application post-installation

Initial Launch

  • Welcome Screen: Offers resources and guidance
  • Activity Bar: Main navigation tool with icons which includes Explorer, Search, Git Integration, Debugging, and Extensions
  • Icons Information: Hover to get a description

Setting Up Extensions

Managing Extensions

  • Python Extension: Most popular with 42.2 million downloads; offers linting, debugging, formatting, unit testing, and IntelliSense
  • Install Extensions: Provides functionalities such as code linting, syntax highlighting, etc.

Initial Python Script

  1. Open Folder: Navigate to project folder
  2. Sample Python Script: Demonstrates print statements, function creation, and version information
  3. Running Script: Use Run Python File in Terminal
    • Check Python interpreter
    • Example of switching between Python 2 and Python 3
  4. Integrated Terminal: Can run scripts directly in the VS Code terminal
  5. Creating and Using Virtual Environment: Demonstration using venv module

Customizing VS Code

Settings Adjustments

  1. Color Themes and Icons: Use built-in options or install new ones
  2. User Settings JSON: Modify settings directly using JSON format for better control
  3. Python Path Configuration: Ensure correct interpreter is being used by default for all projects

Extensions Management and Usage

  • Formatter (Black/Auto PEP8): Automatic formatting of Python code using keyboard shortcuts
  • Linting: Pilant installed for checking code for potential errors
  • Code Runner: Allows running code using keyboard shortcuts instead of right-click

Using Git and GitHub Integration

  • Initialize Repository: Set up git tracking for project folder
  • Gitignore File: Ignore specific directories
  • Commit Changes: Stage and commit files
  • Push to GitHub: Set up remote repository and push changes via terminal

Debugging Features

Setting Breakpoints

  • Debug Tab: Set configurations for debugging Python files
  • Interactive Debug Console: Inspect current values and expressions at breakpoints
  • Local Variables and Watch: Monitor changes and variables during debugging

Unit Testing Integration

Discover and Run Tests

  • Test Frameworks: Support for PyTest and Unittest
  • Running Tests: Can run individual tests or the entire suite
  • Testing Tab: Provides visual feedback on test statuses

Additional Tips

Zen Mode

  • Distraction-Free Coding: Minimalist setup to focus on code
  • Custom Shortcuts: Open command palette to explore various shortcuts

Conclusion

  • Summary: Overview of VS Code features customized for Python development
  • Feedback and Support: Encourage viewers to ask questions and support the channel via likes, shares, and Patreon