After installing Python on your PC, we recommend that you install either an integrated development environment (IDE) or a strong text editor to write and manage your Python code. We recommend installing and using the highly popular and completely free Visual Studio Code developed by Microsoft. An alternative is PyCharm, one of the most popular Python IDEs that will assist you in writing your Python code, install additional modules, and much more.
NOTE: This article is a copy of an article from my, Mathias Balsløw, personal blog website. Link to the original article: https://www.mbalslow.com/blog/article/how-to-install-and-setup-visual-studio-code-python/
After installing Python and running our very first simple script in the previous article, it is now time to download, install, and set up a well-suited application for writing, editing, and executing Python code. As also mentioned in the previous article, you can essentially write Python code in any text editor application, for example, Notepad, however, I would highly recommend finding and using more efficient software for the job like Visual Studio Code. With an application like Visual Studio Code, you gain the benefits of working in an application that can help you write your code faster, cleaner, and better - it also makes it easier to test, debug, and execute your code. Furthermore, Visual Studio Code allows you to install a wide variety of extensions made by Microsoft and other contributors in the community that offers different toolsets like linting (checking the validity of your code).
I introduce Visual Studio Code in this article since this is one of the most popular code editors not only for Python but for programming in general. It is completely free to use, maintained by Microsoft, and it offers some amazing extensions to make your coding nice and easy. But, I would like to mention that I might write another article at some point explaining how to get started and using other code editors and integrated development environments (IDEs) as I also use other tools frequently, for example, the PyCharm IDE. Essentially, it is a matter of taste, mood, and personal preference which tool you decide to use when developing Python code.
Before we started the guide, I would like to mention that you can find many great guides on how to set up Visual Studio Code for Python, for example, Microsoft offers their own documentation on the subject here and here. With that said, let's get started!
Download Visual Studio Code
The first step is to download Visual Studio Code. Go to the official Microsoft Visual Studio Code website and select the appropriate installer for your system by hitting the down arrow. I am going to download the Windows version:
After download, you should now have an installer ready to be installed!
Install Visual Studio Code
The first step, obviously, is to run the executable installer file. Before the installer is started, you might be prompted with the below message. Simply hit the OK on that, at least I recommend that, as you rarely want to install Visual Studio Code for all users as different users might want to use different code editors. Of course, if that is not the case and you want to install Visual Studio Code for all users on the machine, make sure to follow the instructions in the message.
Now, hit next...
Then, accept the license agreement. Here you can see that the software is available under the MIT license, meaning that it is open-source and free to use.
On the next page, you have to select the path for the installation of Visual Studio Code. In the previous article on how to install Python, I recommended that you installed Python directly on the C: drive or somewhere similar, however, that is not (as) relevant with here since we will not regularly use or need the path to the application. Therefore, I will simply use the default path, but you can, of course, change it if you would like.
Next, I will simply keep it with the default settings to let the installer create a shortcut for the application in the start menu of my Windows:
On the next page, I will check some additional options as I like to use Visual Studio Code for various different file types, not only Python (.py) files. You do not need to do this, the only important one is the last option where you need to make sure to have the option to add to PATH enabled.
Lastly, simply hit "Install" in order to run the installation.
When the installation is done, the installer offers to launch Visual Studio Code when you click "Finish". Here, I recommend deselecting the option as we need to restart the machine after the installation (as stated by the option to add to the PATH). Therefore, deselect the option and restart your machine at this point!
Have you restarted? Awesome, welcome back, now let's proceed!
Setting Up Visual Studio Code for Python
It's now time for the exciting part! Go ahead and open the Visual Studio Code application. The first time you open the application (and after installing updates), you are greeted with information on new additions in the latest version.
You can go ahead and close the two (or if you only have one) tabs like this:
Great. With a blank Visual Studio Code application, it is now time to install the Python extension by navigating to the Extension section:
Here, you can simply search for the extension that you are looking for. The one we want now is the primary Python extension, therefore, we can write "Python" and press enter to search. Now, select the one made by Microsoft - notice that this extension has been installed a lot of times! Simply hit the install button to get the extension:
When the installation is completed, it will say "Installed":
Awesome, we now have Python installed for our Visual Studio Code application and we can start writing Python code!
NOTE: If you are new to Python, or even if you are an experienced Python programmer, I recommend checking out this extension as well that helps you by evaluating your code in real-time.
So, let us perform a quick test to make sure that everything works properly. Start by closing the extension tab and navigate back to the file section.
What I recommend doing now is to create a folder somewhere appropriate on your machine to store this first Python test project and all your future projects. I like to do it in the Python folder that we created during the installation in the previous article that is placed on the C: drive, so the path to the projects folder would be "C:\Python\Projects". In here, let us make another folder for pure testing here in the beginning, like this ("C:\Python\Projects\Testing"):
Now, in Visual Studio Code, we can open this folder and create our first Python (.py) file. To open the folder, go to "File" in the menu and select "Open Folder...":
Here, we can select the folder that we just created:
Upon reload, Visual Studio Code might show you the welcome screen again, you can just close that down. You will now notice that we have "access" to the folder on the left side of the application:
We can now create a new file in the "Testing" folder where we will write our first Python code. Click on the icon (it's also possible via the menu) to create a new file.
Give the file any name you would like. The only important thing here is that you remember to append the file extension ".py". Notice how as soon as you append the ".py" file extension, Visual Studio Code will set the icon to the left of the file to be the Python icon.
As soon as you press enter or otherwise tab out of the file name field, the file is created and opened. Since we have not installed and selected any Python linter yet, Visual Studio Code will automatically suggest installing one of the most popular tools for the job, Pylint. In this article, I will show you how you can install Pylint, however, I actually recommend using a different tool called Pyright that you can read about in this article.
For now, let us hit the "Install" button to install Pylint. This will help you to make sure that your code is properly written.
Visual Studio Code will automatically start a "pip install", you will learn more about this in the next article of this series. Please ignore the warnings and such for now - you will know that the installation is complete when you see the message "Successfully installed...":
Awesome, we now have linting installed and enabled. You can read more about linting in the official documentation, for now, you just need to know that this will check the validity of the code and give you guidance if you have any invalid Python code.
We are now done setting up Visual Studio Code and are ready to actually write and execute some code, so let's do that!
Writing & Running Python Code
The installation of the Python linter automatically opened the terminal subwindow, I would recommend closing it down again for now to have more room to view the actual code:
Now, let's test whether your Visual Studio Code is actually worth something by writing some code and see if we can run it! Again, at this stage, you might still be completely blank on how to actually write Python code, don't worry, that will be something we will take a closer look at in the next articles. For now, the most important thing is to make sure that the Visual Studio Code application is actually working properly. So, to test this, let's simply take the same snippet of code we used in the previous article:
print('starting our script!')
with open(r'c:\testfile.txt', 'w') as file:
file.write('This is Python writing to a file, how awesome is that?!')
Copy-paste this into the file in Visual Studio Code:
Cool, this does look a bit better than writing in Notepad, right? Notice that it says "UNSAVED" to the right of the file. Make sure to continuously save the file(s) that you are working on by hitting the Ctrl+S shortcut on your keyboard or via the menu under "File":
Now, before we run the code, let us quickly take a look at how the linting actually works. In order to do that, let us force an error - purposefully make the code invalid - to see how Visual Studio Code will guide us. On line number 6, let us change it to:
print script completed!
Then make sure to save the file as the linting will evaluate the code upon saving the document. After saving the document, the word "print" will be underlined and if we hover over with the mouse, Visual Studio Code will provide an explanation to the error.
In this case, Visual Studio Code will tell us that we are missing parentheses and suggests that we change the code to:
If we do that and save again, the error message will update as the problem is no longer the missing parentheses but the fact that we have an invalid argument in our print function call since there's no object or variable called "script completed!". The issue here is that we have not surrounded our string with quotation marks.
So let's fix that and save the file again, and you will now see that there are no errors left!
Now, let's run the damn thing! First, show the terminal again by going to "View" and "Terminal" in the menu.
Now, the terminal will still show you all the information from when the Python linter was installed earlier in the process. In order to clear the terminal window, simply click in the terminal section (to make sure that is where you write), write "cls" and press enter. That should clear the terminal window:
This part will work exactly the same way as when we executed the Python script in the previous article, the only difference is that we are simply doing it through the Visual Studio Code window instead of opening a new Command Prompt window, which makes it easier and more convenient - but if you like to work in the Command Prompt window instead of Visual Studio Code, you can definitely do that. The other nice thing is that we can now simply right-click in the file section of the window and select the option to run the Python file instead of having to write it all out in the Command Prompt.
After clicking to run the Python file in the terminal, Visual Studio Code will automatically write the path of the Python application and the Python file, again just like we did manually in the previous article. Now, everything is just happening inside the same application for our convenience!
And that's it! We have now set up Visual Studio Code to be able to write and run Python code with linting enabled. Obviously, as already mentioned, there are a ton of other things that can be set up in Visual Studio Code, but I will end the article here as our Visual Studio Code is now functional and ready for next steps, which is to get started writing some exciting code and learn how to the pip install functionality.
One of the amazing things with Python is the endless number of packages of code that other people in the Python community have made, enabling you to perform powerful and complex commands with very few lines of code. To learn how to install additional packages using "pip", see this article:
Use the Python action in Foxtrot
If you are looking for some general information and help to use the Python action in Foxtrot in general, you can read our guide:
I also want to mention some noteworthy references on Visual Studio Code that you can dive into yourself:
- Would you like to know more generally about working with Python in Visual Studio Code? Read more here.
- Would you like to know how to set the used Python interpreter/version? Read more here.
- Would you like to be able to change the color theme of the Visual Studio Code application? Read more here.
Please sign in to leave a comment.