Keywords: Python | VSCode | Environment Variables | PATH Configuration | Code Runner
Abstract: This article provides a comprehensive analysis of the 'Python was not found' error when running Python code in Visual Studio Code. Based on high-scoring Stack Overflow answers, it explores the root causes including Windows PATH environment variable configuration and the interaction between VSCode Python extension and Code Runner extension. The article presents systematic diagnostic steps, multiple verification methods, and practical solutions with code examples to help developers resolve Python environment configuration issues and ensure smooth Python program execution in VSCode.
Problem Background and Error Analysis
When running Python code in Visual Studio Code, users frequently encounter the Python was not found but can be installed from the Microsoft Store error message. The core issue lies in Windows operating system's command resolution mechanism and the complexity of Python environment configuration. When users execute the python command in terminal or VSCode, the system searches for executable files in directories specified by the PATH environment variable. If the corresponding Python interpreter cannot be found, this error message is displayed.
Root Cause Diagnosis
According to high-scoring Stack Overflow answers, the primary cause of this error is that the Python interpreter is not properly added to the system's PATH environment variable. When users download installation packages from the official Python website, if they do not check the "Add Python to PATH" option during installation, Python's executable file path will not be automatically added to the system environment variables. In this case, the system cannot find the Python interpreter through the python command, triggering the error message.
VSCode Extension Interaction Mechanism
The Python extension and Code Runner extension in Visual Studio Code employ different mechanisms when handling Python code execution. The Python extension interacts directly with the Python interpreter through integrated terminal and language server protocol, while the Code Runner extension relies on the system PATH environment variable to locate Python executable files. When both extensions are enabled simultaneously, execution path conflicts may occur, preventing Code Runner from correctly identifying the configured Python interpreter.
Solution Implementation
First, users need to verify whether the Python interpreter is properly installed. This can be tested using the following commands in Command Prompt:
python --version
py --version
If neither command is recognized, it indicates that Python is not properly added to PATH. In this case, users need to rerun the Python installer, ensuring the "Add Python to PATH" option is checked, or manually add the Python installation directory to the system environment variables.
VSCode Configuration Optimization
In VSCode, it's recommended to prioritize the execution functionality provided by the Python extension. Users can configure this through the following steps:
- Select Python interpreter in VSCode's bottom status bar
- Disable Code Runner extension or adjust its configuration
- Use the Python extension's green run button to execute code
This approach ensures that VSCode uses the correct Python interpreter path, avoiding PATH environment variable configuration issues.
Alternative Solutions
Beyond modifying PATH environment variables, consider the following alternative approaches:
- Install Python through Windows Store, which automatically handles environment variable configuration
- Use Python virtual environments to isolate project dependencies
- Configure VSCode workspace settings to specify exact Python interpreter paths
Verification and Testing
After completing configuration, verify Python environment functionality by creating a simple test script:
# test_environment.py
import sys
print(f"Python version: {sys.version}")
print(f"Execution path: {sys.executable}")
print("Environment test successful!")
Run this script in VSCode. If it outputs Python version and execution path information normally, the environment configuration is correctly completed.
Best Practice Recommendations
To avoid similar issues, developers are advised to follow these best practices:
- Always check the
"Add Python to PATH"option during Python installation - Regularly inspect system environment variable configurations
- Use Python extension as the primary code execution tool in VSCode
- Create independent virtual environments for different projects
- Maintain timely updates of Python and related extensions