Getting Started with Python in VS Code
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. 🎬 Watch The Story of VS Code! VS Code is now on Instagram, follow @vscode.ig for updates, tips, and more. Rewatch GitHub Copilot Day. Getting Started with Python in VS Code View as Markdown Ask in VS Code Ask in VS Code Insiders In this tutorial, you will learn how to use Python 3 in Visual Studio Code to create, run, and debug a Python "Roll a dice!" application, work with virtual environments, use packages, and more!
By using the Python extension , you turn VS Code into a great, lightweight Python editor. If you are new to programming, check out the Visual Studio Code for Education - Introduction to Python course. This course offers a comprehensive introduction to Python, featuring structured modules in a ready-to-code browser-based development environment. To gain a deeper understanding of the Python language, you can explore any of the programming tutorials listed on python.org within the context of VS Code.
For a Data Science focused tutorial with Python, check out our Data Science section . Prerequisites To successfully complete this tutorial, you need to first set up your Python development environment. Specifically, this tutorial requires: Python 3 VS Code VS Code Python extension (For additional details on installing extensions, see Extension Marketplace ) Understand the Python setup If you're new to programming tools, it helps to know that three separate pieces work together to run Python in VS Code: VS Code : the editor where you write and organize your code.
Python extension : adds Python language support to VS Code, such as IntelliSense, running, and debugging. The extension doesn't include Python itself. Python interpreter : the program that actually runs your Python code. You install it separately from VS Code. You install all three in this tutorial. VS Code is the workspace, the Python extension connects VS Code to Python, and the interpreter does the work of running your script.
Install a Python interpreter Along with the Python extension, you need to install a Python interpreter. Which interpreter you use is dependent on your specific needs, but some guidance is provided below. Windows Install Python from python.org . Use the Download Python button that appears first on the page to download the latest version. Note : If you don't have admin access, an additional option for installing Python on Windows is to use the Microsoft Store.
The Microsoft Store provides installs of supported Python versions . For additional information about using Python on Windows, see Using Python on Windows at Python.org macOS The system install of Python on macOS is not supported. Instead, a package management system like Homebrew is recommended. To install Python using Homebrew on macOS use brew install python3 at the Terminal prompt. Note : On macOS, make sure the location of your VS Code installation is included in your PATH environment variable.
See these setup instructions for more information. Linux The built-in Python 3 installation on Linux works well, but to install other Python packages you must install pip with get-pip.py . Other options Data Science : If your primary purpose for using Python is Data Science, then you might consider a download from Anaconda . Anaconda provides not just a Python interpreter, but many useful libraries and tools for data science.
Windows Subsystem for Linux : If you are working on Windows and want a Linux environment for working with Python, the Windows Subsystem for Linux (WSL) is an option for you. If you choose this option, you'll also want to install the WSL extension . For more information about using WSL with VS Code, see VS Code Remote Development or try the Working in WSL tutorial , which will walk you through setting up WSL, installing Python, and creating a Hello World application running in WSL.
Note : To verify that you've installed Python successfully on your machine, run one of the following commands (depending on your operating system): Linux/macOS: open a Terminal Window and type the following command: python3 --version Windows: open a command prompt and run the following command: py -3 --version If the installation was successful, the output window should show the version of Python that you installed. Alternatively, you can use the py -0 command in the VS Code integrated terminal to view the versions of python installed on your machine.
The default interpreter is identified by an asterisk (*). Note You run these commands in a terminal, also called a shell, which is the panel that lets you type and run commands. If you install Python while VS Code or a terminal is already open, close and reopen the terminal (or restart VS Code) so it detects the newly installed interpreter. A terminal reads your system configuration when it starts, so a fresh terminal is needed to find Python on your PATH .
Start VS Code in a workspace folder By starting VS Code in a folder, that folder becomes your "workspace". Using a command prompt or terminal, create an empty folder called "hello", navigate into it, and open VS Code ( code ) in that folder ( . ) by entering the following commands: mkdir hello cd hello code . Note : If you're using an Anaconda distribution, be sure to use an Anaconda command prompt. Alternately, you can create a folder through the operating system UI, then use VS Code's File > Open Folder to open the project folder.
Create a virtual environment A best practice among Python developers is to use a project-specific virtual environment . Once you activate that environment, any packages you then install are isolated from other environments, including the global interpreter environment, reducing many complications that can arise from conflicting package versions. You can create non-global environments in VS Code using Venv or Anaconda with Python: Create Environment .
Open the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ), start typing the Python: Create Environment command to search, and then select the command. The command presents a list of environment types, Venv or Conda. For this example, select Venv . The command then presents a list of interpreters that can be used for your project. Select the interpreter you installed at the beginning of the tutorial. After selecting the interpreter, a notification will show the progress of the environment creation and the environment folder ( /.venv ) will appear in your workspace.
Ensure your new environment is selected by using the Python: Select Interpreter command from the Command Palette . Note : For additional information about virtual environments, or if you run into an error in the environment creation process, see Environments . Create a Python source code file From the File Explorer toolbar, select the New File button on the hello folder: Name the file hello.py , and VS Code will automatically open it in the editor: By using the .py file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension and the selected interpreter.
Note : The File Explorer toolbar also allows you to create folders within your workspace to better organize your code. You can use the New folder button to quickly create a folder. Now that you have a code file in your Workspace, enter the following source code in hello.py : msg = "Roll a dice!" print (msg) When you start typing print , notice how IntelliSense presents auto-completion options. IntelliSense and auto-completions work for standard Python modules as well as other packages you've installed into the environment of the selected Python interpreter.
It also provides completions for methods available on object types. For example, because the msg variable contains a string, IntelliSense provides string methods when you type msg. : Finally, save the file ( ⌘S (Windows, Linux
The rest of this page is on the live site.