Running code and the integrated terminal
You are getting the first edition of all these pages. Please let me know if you find an error!
VSCode itself does not know how to run Python code or any other language. VSCode instead uses tools installed on your computer to run programs, e.g., the Python tools you downloaded from https://python.org. So if you want to use VSCode to develop, e.g., Java or Javascript programs, you need to have the necessary tools installed on your system.
VSCode will automatically find language tools on your file system if they are installed in a “standard” location.
Running code
There are three ways to run a program file:
- Select the
Run
menu at the top, thenStart Debugging
- If necessary, select the
Python Debugger
popup, and select default options of subsequent pop-ups until you see the program run in the interactive Terminal at the bottom. - We will discuss the difference between
Start Debugging
andRun Without Debugging
in the future.
- If necessary, select the
- In the editor window,
Right-click
anywhere in the code to open the context menu, then selectRun Python > Run Python File in Terminal
. - Press the
F5
hotkey to start debugging.
By default, VSCode will run the file in the active editor. Alternately, you can right-click a different file in the Explorer and run it.
Exercise
- Create
hello.py
in thepython-test
directory if needed and addprint("Hello World")
- Run
hello.py
using the Run menu - Run it using the editor context window
- Use the
F5
key. If yourF5
key is missing or hard to work with, use Google to research “how to reassign keyboard shortcuts in VSCode”. Reassigning it to the shortcutCmd+R
orCtrl+R
is a solid option.
The Integrated Terminal
When you ran your hello.py
program, you should have seen a flurry of output in the Integrated Terminal window at the bottom. What just happened?
- VSCode opened a Terminal CLI, like you did in the Launching a Terminal lab, except this one is embedded in VSCode.
- VSCode issued the CLI command
python
with your file as an argument. python
runs in the Terminal and prints output.
Your Terminal contents will different, however, you should see Hello World
in there.
Remember, VSCode doesn’t run Python code itself – it uses the tools installed on your computer to do it.
Important note: The Terminal in VSCode is an embedded version of the Terminal we used in Intro to the CLI. You can use the same CLI commands like cd
, ls
, mkdir
, etc.
You may find it convenient to use this integrated Terminal rather than switching to a separate windows. Or you may prefer to keep them separate. Do what works for you.
You can always open the Terminal in VSCode by clicking the Terminal pane (highlighted red in the figure above), or by selecting the Terminal
menu at the top.
Exercise
- List directory contents in the integrated Terminal using the
ls
command. - Type
cd ~
in the integrated Terminal to switch to your home directory. Notice how the Explorer pane does not change. You are only changing the working directory in the Terminal. - Run
hello.py
again using VSCode. What happens in the Terminal? - Use the Terminal to navigate to your
python-test
directory usingcd
commands. - Run the command
touch hello2.py
. Does it appear in the Explorer pane? - Run the command
rm hello2.py
. What happened? What happened in the Explorer pane?
Knowledge check:
- Question: What is the keyboard shortcut for debugging/running your program?
- Question: How do you open an integrated Terminal without running a Python program?
- Question: How can you print the name of the current working directory in the integrated Terminal?
- Question: If you have a runaway process in the integrated Terminal, how do you cancel/kill it? (The answer is the same as for the regular Terminal.)