Skip to content

Saif3985/python-interactive-vscode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Interactive Python Development in VS Code

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.

What You Need

Just two things:

1. Jupyter Extension for VS Code

Press Ctrl+Shift+X to open Extensions, type "Jupyter", and install it (the Microsoft one).

Installing Jupyter

2. Install ipykernel

Run this in your terminal:

pip install ipykernel

Done! That's all the setup.

How It Works

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}")

Code with cell markers

Running Your Code

Option 1: Run Chunks Individually

Put your cursor in any chunk and press:

  • Ctrl+Enter → Runs that chunk
  • Shift+Enter → Runs that chunk and jumps to the next

Results show up in the Interactive window on the side.

Interactive results

Option 2: Run Everything at Once

Just run it like normal Python:

python my_script.py

Running in terminal

Quick Example to Try

Copy 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)}")

Why This Is Useful

  • Test small parts of your code without running everything
  • Keep regular .py files (better for Git than .ipynb)
  • Switch between interactive testing and script execution
  • Works great for data analysis and experiments

Common Issues

"Run Cell" button not showing?

  • Save the file with .py extension first
  • Make sure Jupyter extension is active

Chunks not running?

  • Check that ipykernel is installed
  • Select your Python interpreter (bottom-left corner of VS Code)

That's it! Now you can have the best of both worlds. 🚀

About

Learn how to run Python files interactively like Jupyter notebooks in VS Code using cell markers

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages