A Beginners Guide to Using Visual Studio Code for Python

Visual Studio Code (VSCode) is a powerful, free source-code editor that makes it easy to write and run Python code. This guide will walk you through setting up VSCode for Python development, step by step.
Prerequisites
Before we begin, make sure you have:
- Python installed on your computer
- An internet connection
- Basic familiarity with your computer’s operating system
Step 1: Download and Install Visual Studio Code
Windows, macOS, and Linux
- Go to the official VSCode website: https://code.visualstudio.com/
- Click the “Download” button for your operating system
- Run the installer and follow the installation instructions
- Launch VSCode after installation
Step 2: Install Python Extension
- Open VSCode
- Click on the Extensions view icon on the left sidebar (or press Ctrl+Shift+X)
- Search for “Python” in the extension marketplace
- Look for the official Microsoft Python extension
- Click “Install”
Step 3: Configure Python Interpreter
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P on macOS)
- Type “Python: Select Interpreter”
- Choose the Python version you want to use
- If you don’t see your Python installation, you may need to add it to your system PATH
Step 4: Create Your First Python Project
- Create a new folder for your project
- Open the folder in VSCode (File > Open Folder)
- Create a new file with a .py extension (e.g., hello.py)
- Write your first Python code:
Step 5: Running Python Code
Method 1: Using the Run Button
- Open your Python file
- Click the “Run” triangle button in the top-right corner of the editor
- See the output in the integrated terminal
Method 2: Using the Integrated Terminal
- Open the integrated terminal (View > Terminal or Ctrl+`)
- Run your script by typing:
Step 6: Debugging Python Code
- Set a breakpoint by clicking the red dot in the gutter (left of the line numbers)
- Go to the Run view (Ctrl+Shift+D)
- Click “Run and Debug”
- Select “Python File” configuration
- The debugger will stop at your breakpoint, allowing you to inspect variables
Recommended VSCode Settings for Python
Code Formatting
- Install the “Black Formatter” extension
- Open VSCode settings (File > Preferences > Settings)
- Search for “format on save”
- Check the “Format on Save” option
Linting
- The Python extension includes PyLint by default
- Configure linting rules in settings to improve code quality
Useful Extensions for Python Development
- Pylance: Enhanced language support
- Python Docstring Generator
- IntelliCode: AI-assisted development
- GitLens: Git integration
Troubleshooting Common Issues
Python Not Recognized
- Ensure Python is installed correctly
- Add Python to your system’s PATH
- Restart VSCode after installation
Extension Not Working
- Reload VSCode
- Reinstall the Python extension
- Check your Python interpreter settings
Tips for Productive Python Development
- Use virtual environments
- Learn keyboard shortcuts
- Explore IntelliSense for code completion
- Use Git for version control
Conclusion
In this tutorial, we explored how to set up VSCode for Python development from scratch. We began by installing VSCode and the Python extension, selecting the appropriate Python interpreter, and creating our first Python script. We learned how to run and debug Python code using both the Run button and the integrated terminal, and saw how breakpoints and the debugger help identify issues in our code. The guide also covered recommended settings such as code formatting with Black, linting with PyLint, and introduced helpful extensions like Pylance and GitLens. Lastly, we addressed common setup issues and shared productivity tips such as using virtual environments and mastering keyboard shortcuts. By the end of this guide, you should be comfortable using VSCode as a powerful and flexible environment for Python programming.
Nikhil is an intern consultant at Marktechpost. He is pursuing an integrated dual degree in Materials at the Indian Institute of Technology, Kharagpur. Nikhil is an AI/ML enthusiast who is always researching applications in fields like biomaterials and biomedical science. With a strong background in Material Science, he is exploring new advancements and creating opportunities to contribute.