This is what this looks like on Windows: Our program is run in VSCode (with a deliberate error) Debug Python in VSCodeĭebugging is one of those features that makes an IDE more powerful than a simple editor. Since we made a deliberate mistake, you should get an error similar to: Traceback (most recent call last): What happens next is VSCode opening an integrated terminal window in which the file is run. To run this file, you can either use the menu Run -> Run Without Debugging or press Control + F5 (Windows and Linux) or cmd + F5 (MacOS). I deliberately made a mistake that we will try to debug later on. If so, it assigns that to the name variable. This code will see if the script received an argument. Go ahead and pick one.Ĭopy and paste the following program into your newly created file: import sys If you haven’t already done so, VSCode might ask you to pick a Python interpreter at this point. Call it anything you like I called mine vscode_playground.py. Create a new file by clicking the ‘new file’ button in the explorer at the left or using the File menu. Let’s create a simple program for testing purposes. With the interpreter configured, we can now run a Python program. In Windows, you can click ‘New folder,’ for example: Step 3: Create and run a Python file in VSCode I created one beforehand, but you can use the ‘Open Folder’ dialog to create one in place too. Let’s open a folder in which we can start experimenting first. When you open VSCode for the first time, you’ll start with an empty workspace. In Windows, this looks like this: Selecting the Python interpreter in Windows Step 2: Create a new Python project in VSCode Pick the Python version that is appropriate for your project You shouldn’t have to VSCode should be able to detect a correctly installed Python interpreter. If you don’t have any choice, make sure you have Python installed on your system and opt to enter the path to your interpreter manually. If you don’t know which one to pick, choose Python 3 with the latest version. Preferably, you choose the interpreter from your virtual environment if you use one. With the command palette open, start typing ‘Python: select interpreter.’ You’ll quickly see that the auto-complete helps you out you don’t have to type the entire text: Use the command pallet to find what you are looking for quickly If there’s one shortcut you need to learn, it’s this one! Alternatively, you can use the menu: “View -> Command Pallet…“ The shortcut ‘Control + shift + P’ (Windows/Linux) or cmd + shift + P (MacOS) allows you to quickly open the command pallet. It’s awesome and a real timesaver, so I suggest you get used to it early on. It allows you to do almost everything with just your keyboard. The command palette gives you quick access to all functionality VSCode has to offer. We’ll use a feature called the command palette to set the interpreter. In addition, it also supports enhanced virtual environment managers such as Poetry and Pipenv. VSCode even detects a virtualenv in your project folder, and this virtualenv also contains a Python interpreter, which VSCode can use. VSCode usually does its best to detect the available Python interpreters automatically. Using the right interpreter for your project is important since VSCode uses it to run and debug your code and provide things like auto-completion. Step 1: Select python interpreterĪ system can have multiple Python interpreters. The following step-by-step guide helps you to set up VSCode correctly for running Python code. vscode directory to the ignore list since your coworkers probably prefer other settings and preferences or even use a completely different IDE. If you use a version control system, you may want to add the. vscode/settings.json, containing these project-specific settings. If you define project-specific settings in VSCode, the IDE creates a new file. If you prefer, you can also open the folder from the menu: File -> Open Folder. The code command is a handy shortcut to open a VSCode window. If you are on a terminal, and it doesn’t matter if this is on Linux, MacOS, or Windows, you can create a new project and open it with VSCode as follows: $ mkdir myproject When doing so, one window with one workspace will suffice.Ĭreating a project is simple it’s a directory that you open with VSCode. However, you’ll typically work on one project at a time. You can have multiple workspaces open, each in its own window. A workspace can, in turn, show multiple folders (or: projects) if you want it to.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |