In this post we'll look at how to develop in Python using Visual Studio Code. I'll also show you a few extensions that can make your life much easier while working with Python!
A widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in python, or python3 depending on your Python installation, and then hit Enter. HtmlTest runner is a unittest test runner that save test results in Html files, for human readable presentation of results. This Package was inspired in unittest-xml-reporting and HtmlTestRunner by tungwaiyip.
When you install Python (which I recommend you do via the official installer), remember in which folder it was installed. And on Windows, make sure to tick the 'Add Python to PATH' in the first screen of the official installer.
Download Visual Studio Code and install it like you would any other application.
Open Visual Studio Code and navigate to the Extensions panel on the left hand side. There, look for the Python extension and install it
Once that's installed, you can open any folder using Visual Studio Code and create a file that ends in
.py. The Python extension will use appropriate syntax highlighting and also help you code by offering you suggestions and hints.
You can change which version of Python the extension uses by clicking on the bottom left, where in the screenshot it says 'Python 3.7.4 32-bit'. This will let you select a different version from those you have already installed. Note that if you only have one version, you'll only be able to select that.
Here's a link to the Python extension: https://marketplace.visualstudio.com/items?itemName=ms-python.python
The Code Runner extension
Another extension, aptly called 'Code Runner', allows you to run your code more easily than normal. It gives VSCode a 'Play' icon at the top right, and when you click it, the selected code will run.
This is great because it allows you to either:
- Select a portion of your code and run it, for example if you want to double-check what it does;
- De-select all your code and just open a file, and press the 'Play' icon to run the entire file.
Do note that if you select only a portion of the code and run it, this portion cannot depend on variables or functions defined elsewhere in the code.
The indent-rainbow extension
An extension called 'indent-rainbow' will color every indentation level in Python in a slightly different color, so you can very easily tell which parts of your code are at which indentation level.
This is what the extension will do:
The Rainbow Brackets extension
If you like the indent-rainbow extension, you might also like Rainbow Brackets extension, which highlights nested parentheses, square brackets, and curly braces in different colours, making it easier to keep track of nested brackets.
The vscode-icons extension
While this extension is by no means required to run Python code, I use it and like it very much as it allows me to more easily scan my open files and folders by giving them nicer icons.
You can install this in order to change the icons displayed beside files and folders in your currently open project.
Python Code Tester
That's it! As long as your Python version is in your PATH (which happens automatically on Mac and Linux, and you must tick the box in the installer in Windows), the Python extension will straight work out of the box!
If you have persistent issues, Google is your best friend as tens of thousands of people use this extension, and almost all potential problems have already been discussed and solved in other occasions. The development team is also very helpful in their GitHub page.
The GitHub page also contains extensive guides and information on how to get the extension up and running, it case it's necessary!
Code Runner Python
Hope this has been helpful and your environment is now all set up. Happy coding!