Getting Started with Python
Let's get started by creating the simplest "Hello World" Python application.
To successfully complete this tutorial, you must do the following:
Install the Python extension.
Install whichever version of Python you want to use. Options include:
- The built-in Python installation on Linux.
- An installation through Homebrew on macOS using
brew install python3(the system install of Python on macOS is not supported).
- A download from python.org.
- A download from Anaconda (for data science purposes).
From within VS Code, select a version of Python using the Python: Select Interpreter command on the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), or by using the Select Python Environment option on the Status Bar if available:
The command presents a list of available interpreters that VS Code can find automatically. If you don't see the desired interpreter, see Configuring Python environments.
Create a folder and source code file
Create an empty folder called "hello", navigate into it, and open VS Code (
code) in that folder (
mkdir hello cd hello code .
From the File Explorer toolbar, press the New File button:
Name the file
hello.py, and it automatically opens in the editor:
By using the
.py file extension, VS Code interprets this file as Python and evaluates the contents with the Python extension.
Next, start entering the following source code:
msg = "Hello World" print(msg)
When you start typing
IntelliSense and auto-completions work for standard Python modules as well as other packages you've installed into your Python environment. It also provides completions for methods available on object types. For example, because the
msg variable contains a string, IntelliSense provides string methods then you type
Feel free to experiment with IntelliSense some more, but then revert changes so you have only the
msg variable and the
Running Hello World
It's simple to run
hello.py with Python. From an external terminal, type
python3 hello.py (macOS/Linux) or
python hello.py (Windows, assuming Python is in your PATH). Either way you should see "Hello World" as output.
You can also use the VS Code integrated terminal, opened using View > Integrated Terminal (⌃` (Windows, Linux Ctrl+`) with the backtick character), to stay within the context of VS Code. Then you can just run
python hello.py directly:
Debugging Hello World
Let's now try debugging our simple Hello World application.
First, set a breakpoint in
hello.py by placing the cursor on the
Next, select the Debug View in the sidebar:
Now you need to configure
launch.json for Python by clicking the settings icon on the debug toolbar:
This command automatically creates a
launch.json with a number of Python configurations, which appear in the configurations drop-down:
These different configurations are fully explained in Debugging; for now, just select "Python," which runs the current file using the current Python environment, and automatically stops the debugger when the program starts. (See the
stopOnEntry setting in the configuration).
Run the debugger by clicking the green arrow in the Debug toolbar or pressing F5. Because
stopOnEntry is set to true in the configuration, the debugger stops on the first line of the file. If you examine the Local variables window at this point, you see that only automatic dunder variables are defined:
Notice also that a debug toolbar appears with commands to run, step, restart, and stop the program, and that the Status Bar changes to an orange color to indicate debug mode. The Debug Console also appears automatically.
Click the green arrow to continue running the program (F5), and the debugger stops on the breakpoint. The now-defined
msg variable appears in the Local pane and you can work with the variable in the debug console:
Click the green arrow again to run the program to completion. "Hello World" appears in the debug console and VS Code exits debugging mode once the program is complete.
Tip: Although the debug console works well for output, it presently cannot take input from a Python program through the
raw_inputfunctions. In those cases, it's necessary to run the debugger using an external terminal. This is easily done by selecting the External Terminal debug configuration:
For full details, see Debugging.
Let's now run an example that's a little more interesting, using matplotlib and NumPy.
Return to Explorer, create a new file called
standardplot.py, and paste in the following source code:
#%% import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np x = np.linspace(0, 20, 100) plt.plot(x, np.sin(x)) plt.show()
Try running the file in the debugger as described in the last section. If you run the program to completion, it may fail if matplotlib and numpy are not installed in the current environment.
This is easy to remedy. Go to the Terminal and enter
pip3 install matplotlib, and VS Code installs that package into your project along with its dependencies (including NumPy). Note that if you don't want to install matplotlib and its dependencies globally then use a virtual environment.
Rerun the program now and a plot window appears with the output:
You can configure VS Code to use any Python environment you have installed, including virtual environments. You can also use a separate environment for debugging. For full details, see Environments.
There is much more to explore with Python in Visual Studio Code:
- Python environments - Control which Python interpreter is used for editing and debugging.
- Editing code - Learn about autocomplete, IntelliSense, formatting, and refactoring for Python.
- Linting - Enable, configure, and apply a variety of Python linters.
- Debugging - Learn to debug Python both locally and remotely.
- Unit testing - Configure unit test environments and discover, run, and debug tests.
- Settings reference - Explore the full range of Python-related settings in VS Code.