A Visual Studio Code extension that makes it easier to run Python projects with multiple entry points.
Python projects often contain multiple files with:
if __name__ == "__main__":
However, VS Code's default Run Python File button only runs the currently opened file.
Python Main Runner scans your workspace for Python entry points and allows you to quickly select and run any of them without opening the file first.
Features
- 🔍 Automatically detects Python entry points
- ▶ Run any Python file containing
if __name__ == "__main__" - 📂 Supports nested folders
- 💾 Remembers your selected entry point
- 🔄 Automatically rescans when Python files are created, changed, or deleted
- 🖥 Runs selected files in a dedicated VS Code terminal
- 🚫 Ignores common virtual/environment folders:
.gitnode_modules__pycache__venv.venv
Example
Given this project:
my-project/
│
├── src/
│ ├── main.py
│ └── server.py
│
├── tools/
│ └── migrate.py
│
└── README.md
With:
src/main.py
if __name__ == "__main__":
print("Application started")
src/server.py
if __name__ == "__main__":
print("Server started")
tools/migrate.py
if __name__ == "__main__":
print("Migration started")
Python Main Runner detects:
src/main.py
src/server.py
tools/migrate.py
You can then select any of them and run them directly from VS Code.
Usage
- Open a Python project folder in VS Code.
- Python Main Runner automatically searches for entry points.
- Use the Python Main Runner toolbar button.
- Select the Python entry point you want to use.
- Press the run button to execute it.
The selected entry point is saved and restored when reopening the project.
Requirements
- Visual Studio Code
1.125.0or newer - Python installed and available through your system PATH
Commands
| Command | Description |
|---|---|
Run Python Main |
Runs the currently selected Python entry point |
Select Python Main |
Opens a list of detected Python entry points |
How It Works
Python Main Runner:
- Searches the workspace for
.pyfiles. - Reads each Python file.
- Detects the following pattern:
if __name__ == "__main__":
- Adds matching files to the available entry point list.
- Runs the selected file in a VS Code terminal.
Development
Clone the repository:
git clone <repository-url>
cd python-main-runner
Install dependencies:
npm install
Compile the extension:
npm run compile
Start development mode:
npm run watch
Then press:
F5
inside VS Code to launch the Extension Development Host.
Packaging
Install the VS Code Extension Manager:
npm install -g @vscode/vsce
Create a .vsix package:
vsce package
Install locally:
code --install-extension python-main-runner-x.x.x.vsix
Project Structure
python-main-runner/
│
├── src/
│ └── extension.ts
│
├── package.json
├── tsconfig.json
├── README.md
└── node_modules/
Future Improvements
Possible improvements:
- Use the VS Code selected Python interpreter instead of system
python - Support debugging selected entry points
- Allow command-line arguments
- Add run profiles
- Improve Python parsing using the Python AST
- Add workspace-specific configurations