Home Computing How to Use VS Code as Your Python IDE

How to Use VS Code as Your Python IDE

When you think of learning Python, you’ll often find tutorials on doing so from the command line. That’s a great option, as Python is one of the more simple languages with which to start.

However, I’ve pointed many new users to the terminal window, only to see them cringe. The idea of using the terminal never fails to elicit responses like, “Isn’t that for advanced users?” If we’re talking Linux administration, I might be so inclined to agree. I certainly wouldn’t toss someone new to Linux at a command-line interface (CLI) and say, “Have at it!” Such behavior would not make for fast friendships.

Anytime I can give someone a leg up on ease of use, I will do so. Ergo, it makes perfect sense to use one of the most popular (and user-friendly) integrated development environments (IDEs) along with one of the most beginner-friendly programming languages.

However, using Visual Studio Code with Python isn’t always an out-of-the-box experience. Fear not, I’m here to help you out with that — so that you too can experience a handy GUI to help you on your journey to becoming a Python developer of renown.

Of course, one thing to keep in mind is that getting this application up and running isn’t exactly the same process on every operating system. Fortunately, that only applies to the installation of the necessary bits. Although I’m going to demonstrate this process on Ubuntu Linux, you shouldn’t have much trouble getting the various pieces installed with your operating system of choice.

Let’s get started.

Installing the Necessary Bits

Remember, I’m demonstrating with Ubuntu Linux. If you’re using macOS, Windows or a Linux distribution with a base other than Ubuntu/Debian, you’ll have to alter the installation instructions.

The first thing to do is install Python. We’re going to install the full package so we get everything required. Log into your desktop and open a terminal window (sorry, it’s required for this installation). From the CLI, issue the command:

When that installation is completed, you’re ready to move on.

Our next installation is VS Code. Open your web browser and point it to the official VS Code download page. Make sure to download the file that matches your operating system. For Ubuntu, download the .deb file.

When the download finishes, open your file manager, navigate to the Downloads folder and double-click the downloaded file. You should be prompted for your user password, at which point VS Code will install.

If the installation doesn’t work, you’ll have to resort to the command line (again, sorry). For that, change to the Downloads directory:

Install VS Code with:

We use the * as a wild card, so we don’t have to type the full release number of the file name.

Type your user password and the installation should complete.

Using VS Code with Python

Before you open VS Code, go back to the terminal window (I won’t say “sorry” this time) and create a directory for our test project with the command:

Change into that project with the command:

We’re now going to open VS Code from within that directory, so it knows that’s the working directory of the project. For that, issue the command:

Yes, the . is required; it instructs VS Code to use the current working directory.

The VS Code app should open, and will immediately ask you if you trust the authors of the file in the folder (Figure 1).

Figure 1: You can also instruct VS Code to trust the authors in the parent directory as well.

Before continuing, you must install the Python extension for VS Code. To do that, click the gear icon at the bottom-left corner of the IDE window. In the resulting search bar, type “Python”. At the top of the results, choose “Install associated with Python” (Figure 2) and allow the installation to complete.

Figure 2: Installing the Python extension for VS Code.

Once the extension is installed, the fun begins. Instead of doing everything manually, I’m going to show you how easy it is to create a Hello app with the GUI.

From the main VS Code page, use the keyboard shortcut Ctrl+Shift+P. In the resulting field, type “Python: Create Environment”. From the drop-down results, select Venv. You will then be prompted to select the interpreter. From that list, choose the newest version of Python that is installed on your system. This may take some time, as the installer may need to update a few packages.

When that process finishes, click the File Explorer icon in the top left of the VS Code window, which should then reveal .venv. Click the New File icon (file icon with +) and, when prompted, type “hello.py” (Figure 3).

Figure 3: Naming our new Python file.

Hit Enter on your keyboard and the file is ready. On the first line of the file, type:

Hit Enter. On the second line, start typing “print.” A drop-down list of options will appear (Figure 4).

Figure 4: With VS Code, you don’t have to type everything manually.

Select print from the list. Once print has been selected, add “(msg)” so the full line reads as follows:

Save the file with Ctrl+S.

Running Your New App

Click the “play” button (right-pointing arrow near the top right of the application window). You should see the output of the script in the bottom pane, which will print out, “Hello, New Stack!” (Figure 5).

Figure 5: Our hello.py program ran successfully.

Congratulations, you just created your first Python app in the user-friendly VS Code IDE. Next time, we’ll use VS Code to do something a bit more challenging. Until then, happy Python-ing.

Group Created with Sketch.

 

Reference

Denial of responsibility! TechCodex is an automatic aggregator of Global media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, and all materials to their authors. For any complaint, please reach us at – [email protected]. We will take necessary action within 24 hours.
DMCA compliant image

Leave a Comment