We briefly learned some of the code editors that are available to create Python programs in the previous section “Python Code Editors, Integrated Development Environments“. I am going to stick to Visual Studio Code out of all these available code editors. And I will show you how to configure it which will help improve our coding experience with Python.
To begin first I’ll go to https://code.visualstudio.com/ and start downloading the installer. The download page auto detects the platform you are in and let you download accordingly. Since I am on Windows, I get the Download for Windows as depicted below.
After downloading run the installer and accept the defaults and complete the installation. Then launch Visual Studio Code and in the start page click new file. Type the following code in the blank file.
print (“This is my first code in Python”)
Note that when you start typing above code you will not see any syntax highlighting as the file is still not saved and Visual Studio Code doesn’t recognize the programming language you are typing. So as a best practice before we start typing our code we save the file name with .py extension which is the script file format used by Python.
As you can see above, when I saved the file I can see the syntax highlighting because VS Code knows the language grammar for Python. Now let’s execute our file, but within our code editor. The question is, how?
Simply open the command window (Ctrl+Shift+P) and look for the command to run a Python file in the terminal. But as you will see, right now there are no Python commands yet because we need to install the extensions first, which is exactly the popup in right bottom in the below figure shows us.
You can dive right in and install, but let’s take a few moments and click on Show Recommendations, which opens the side bar on the extensions view. If you have already installed other code editors such as Notepad++ or Atom, you will get recommendations for the Atom and Notepad++ keymap extensions.
These extensions are used for assigning to VS Code the same keybindings that are used in those code editors. This will make a transition to Visual Studio Code easier, as you would know already all the shortcuts that you use in your daily development.
But for the time being we are going to skip those and move ahead to install the recommended Python extension. It’ll take a while depending on your internet connection as the extension needs to be downloaded first.
After successful installation when If I right-click now, I can perform actions like Run Python file in terminal, among others. When I click Run Python File in Terminal, I will get the output like this.
Alternatively, you can see there is a green run button located on the top right area. It becomes visible only after installing the python extension. You can also click the green run button to run the Python file in terminal.
As you can see, I had a code editor, I installed an extension, and now my code editor suddenly started to work more like an integrated development environment.
We start to get features that go beyond just modifying text in a nice editor, to being able to invoke an interpreter and execute a program, like being able to select a few lines and execute them in the terminal as depicted below.
Here the Python REPL starts in a terminal window. And now this function is available to us, and we can keep using the terminal.
You can also go to the Terminal menu in VS Code and click New Terminal. When the new terminal window is open just type python and enter. It will start the Python REPL in the current terminal window.
And let’s not forget that now we have the terminal, and we can use as we please. We can import a library and use any of its functions. We can also use pip.
And aside from the terminal, we have other useful windows that includes the debug console, output, and the problems window which will be useful when you start writing and executing your Python code.