Ever wanted to test Python code piece by piece like in Jupyter, but still keep everything in a normal .py file? This tutorial shows you exactly how to do that in VS Code.
Just two things:
1. Jupyter Extension for VS Code
Press Ctrl+Shift+X to open Extensions, type "Jupyter", and install it (the Microsoft one).
2. Install ipykernel
Run this in your terminal:
pip install ipykernelDone! That's all the setup.
Create any Python file, let's say my_script.py. Add # %% wherever you want to split your code into chunks:
# %% Import section
from datetime import datetime
# %% Get current info
current_time = datetime.now()
day = current_time.strftime("%A")
print(f"Today is {day}")
# %% Main program execution
if __name__ == "__main__":
print(f"Full timestamp: {current_time}")Option 1: Run Chunks Individually
Put your cursor in any chunk and press:
Ctrl+Enter→ Runs that chunkShift+Enter→ Runs that chunk and jumps to the next
Results show up in the Interactive window on the side.
Option 2: Run Everything at Once
Just run it like normal Python:
python my_script.pyCopy this into a new file and try running the chunks:
# %% Configuration
user_name = "Alex"
favorite_color = "blue"
# %% Create greeting
greeting = f"Hey {user_name}! I heard you like {favorite_color}."
print(greeting)
# %% Generate fun facts
colors = ["red", "blue", "green", "yellow"]
color_count = len(colors)
has_favorite = favorite_color in colors
print(f"Total colors: {color_count}")
print(f"Your favorite is in the list: {has_favorite}")
# %% Execute full program
if __name__ == "__main__":
print("=== Color Preference Program ===")
print(greeting)
print(f"Available colors: {', '.join(colors)}")- Test small parts of your code without running everything
- Keep regular
.pyfiles (better for Git than.ipynb) - Switch between interactive testing and script execution
- Works great for data analysis and experiments
"Run Cell" button not showing?
- Save the file with
.pyextension first - Make sure Jupyter extension is active
Chunks not running?
- Check that
ipykernelis installed - Select your Python interpreter (bottom-left corner of VS Code)
That's it! Now you can have the best of both worlds. 🚀



