Working With Python

Once we open our first Python file in VS Code, we’ll see some configuration pop-ups. For the time being, don’t dismiss them.

Python Pop Ups

Configuring VS Code for Python

Open the file

If you haven’t created a file in the pyworkshop directory, now is the time to do so. You can make a new file (Ctrl+N or ⌘P) and then save it (Ctrl+S or ⌘S).

Now that you’ve learned the necessary keyboard shortcuts, use Quick Open with Ctrl+P or ⌘P to open the file.

If you haven’t installed the Python extension at this point, you’ll get a pop-up recommending you install it for this type of file. Please install it now, and reload VS Code when prompted to load the extension.

Configuring The Interpreter

Many operating systems include Python, but unfortunately it’s usually a few versions behind.

We never want to use Python2 for new Python projects, and we want to make sure we select the latest version of Python3 that we installed in the pre-requisites.

Luckily, VS Code is smart. If you launch it from a directory with an activated virtual environment, it’ll automatically pick up the correct interpreter.

In case you need to configure the interpreter manually, follow these (optional) instructions.

You can always see what your interpreter is set to on the left side of the status bar at the bottom of the window.

Selected Interpreter

Setting Up a Linter

Per the VS Code documentation, linting highlights syntactical and stylistic problems in your Python source code. A linter will give you code hints about a variety of different types of problems. For example, when you have subtle errors in your code, like trying to use a variable you haven’t defined yet. A linter will also show you when you’re not following Python style convention called PEP8. PEP8 is a set of defined rules for how Python code should look. We’ll cover it in more depth later, but what you need to know right now is that PEP8 warnings are not syntax errors. If your code doesn’t adhere to the PEP8 standard, it will still run.

By default the linter will run every time you save a file, so it’s good practice to save often with (Ctrl+S or ⌘S).

Let’s go ahead and click install on the Linter popup.

Install pylint

Note: If you accidentally dismissed the popup, open the command palette and search for Python: Select Linter, then select pylint. The popup will now reappear, and you can hit install. Expand this section for more details.