---
title: Interactive Programming in VS Code - Verfiy Setup
tags: PythonSetup
---
# Interactive Programming in VS Code - How to verify your setup is working?
Being able to leverage Python's interactive programming capabilities can greatly improve development experience. This document summarizes a few ways how to run code interactively and describes a setup to verify that your system is properly configured.
Links:
- [Python in VS Code](https://code.visualstudio.com/docs/languages/python)
- [Read-eval-print-loop](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)
## 0. Requirements
- Python environment satisying the requirements below (the example here uses `conda` but that is not mandatory)
- VS Code including the extensions
- Python
- Jupyter
## 1. Prepare Folder and Files Needed
Create a folder called `testsetup` and with the following files:
`environment.yml`:
```
name: testenv
channels:
- conda-forge
- defaults
dependencies:
- ipython
- numpy
- jupyter
```
`script.py`:
```
# %%
print("Execute this cell by clicking run or Shift+Enter")
# %%
print("This is another cell")
# %%
import numpy as np
print(np.array([1, 2, 3]))
```
The content of the directory looks like this:
```
testsetup/
├── environment.yml
└── script.py
```
You can do this using whatever means available to you.
If you already use VS Code, it will recognise the Python file and
maybe (depending on your setup) prompt you to install Python.
Ignore that for now.
## 2. Create `conda`-Environment
Install `conda` using the [Miniconda](https://docs.conda.io/en/latest/miniconda.html) installer or install the [Anaconda](https://www.anaconda.com/products/individual) distribution which includes `conda`.
Then create the environment as follows:
```console
$ conda env create -f environment.yml
```
If you use Windows, please take a look at this [guide](https://hackmd.io/@caichinger/Hk5Bs2dQt/edit).
## 3. Launch VS Code and Activate Python Environment
In VS Code, open the folder created above.
Open the Python file created above.
Type `Ctrl+Shift+P` to tnstruct VS Code to use our Python environement by selecting it as Python interpreter (if not already set).
Note that settings are saved in a `.vscode/` directory, it is created
automatically once you select a non-default interpreter.
There are two main option to run code:
1. In the terminal
2. In the interactive window
Which one you use is a matter of taste and personal style. Try both if you are unsure.
On first invocation, the terminal/window is initialized.
### Run entire file in terminal
Execute/run the entire script by hitting the `Run Python File in Terminal` :arrow_forward: button in
the upper right corner of the editor (or right-click and chosing the respective option).
The code is executed in the terminal.
### Run selection in terminal
Execute/run a piece of code by first selecting a few lines and then `Shift+Enter` (or right-click and chosing the respective option).
The code is executed in the terminal.
### Run code block/sell
Execute/run a code block/cell (marked with percent (`# %%`) markers) by clicking `Run cell` or `Shift+Enter` while the cursor resides in a cell.
The code is executed in the interactive window.
It may be necessary to restart VS Code after it tried to load the interactive kernel for the first time.
## Notes
- The behavior of VS Code can be tweaked to your taste.
- Default behavior changes if you do not use percent syntax `# %%` to indicate code cells. Use of code cells is optional.
## Optional: Use IPython in Terminal
If you prefer to use IPython instead of Python in the terminal:
Type `Ctrl+Shift+P` and open JSON settings, then copy-paste below configuration and restart.
```json
"python.terminal.launchArgs": [
"-m",
"IPython",
"--no-autoindent",
],
```