Author: April Speight
Whether you’re brand new to learning Python or are in need of a home to write your Python code, choosing a code editor to get started with can be a tough decision. And while there’s many options available, the one that I love most is Visual Studio Code .
Visual Studio Code is a free open-source and cross-platform code editor developed by Microsoft. Ranked as the most popular development environment in the 2019 Stack Overflow Developer Survey, Visual Studio Code is a feature-rich, highly customizable code editor that’s not only great for editing source code, but also has built-in support for collaboration and cloud-hosted environments.
The editor has a lot of great features, and it’d be almost virtually impossible to list every single feature in one blog post. So rather than blasting you with an exhaustive list, I’m going to share with you my top 5 tips for using Python with Visual Studio Code.
1. Download the Python extension
Remember how I said Visual Studio Code can support additional languages through extensions? Well, the Python extension is one such extension. Could you write Python code without the extension? Sure. But you’d miss out on all the Python-specific features available for programming in the language!
The Python extension created by Microsoft provides support for the Python language and includes features such as syntax coloring, code completion, linting, debugging, code navigation and code formatting, along with Python-specific features like Jupyter Notebook support. And like most other features within Visual Studio Code, you can customize the settings for the extension as well. You can find the Python extension within the Extension Marketplace .
2. Select the linter that’s right for you
Mistakes happen, right? Sometimes it’s easy to get so focused on writing code that tiny (and sometimes significant) errors occur while typing. Rather than relying on yourself to catch all your mistakes, you can use a linter to help you out. Linters analyze how the code runs and detects potential errors as you type. There are various linters available for Python, and the Python extension supports the following:
- Pylint(default): Checks for errors and tries to enforce a coding standard
- Flake8: Checks code against style conventions in PEP 8, programming errors and cyclomatic complexity
- mypy: Checks for optionally-enforced static types
- pydocstyle: Checks compliance with Python docstring conventions
- pycodestyle (pep8): Checks Python code against some of the style conventions in PEP 8
- prospector: Analyze Python code and output information about errors, potential problems, convention violations and complexity
- pylama: A wrapper for multiple Python tools (pycodestyle, pydocstyle, PyFlakes, Mccabe, Pylint, Radon, gjslint, eradicate, mypy)
- Bandit: Finds common security issues in Python code
As noted, Pylint is the default linter. Visual Studio Code enables Pylint when a new Python file is created. However, if the editor doesn’t detect that a linter is enabled, a prompt displays in the lower right corner to install Pylint. Of course, if Pylint isn’t your jam, you could always enable one of the other supported linters with the Command Palette command
Python: Select Linter.
3. Make use of IntelliSense
There’s a lot to remember when it comes to coding in Python. If you’ve managed to memorize every bit of syntax or can locate all objects and/or variable declarations in your code, then congratulations—you’re a unicorn! As for the rest of us, we could all use a little help to get by. If you’re anything like me, you’ll find IntelliSense is a saving grace. IntelliSense is an umbrella term that Microsoft uses to identify a variety of useful tools to assist with programming, such as code completion, object definition and the location of object or variable definitions.
So how does it work? Typing a trigger character (such as the dot character in Python) triggers IntelliSense. For example, consider string methods. Let’s say I have the following line of code:
phrase = "visual studio code is so cool."
If I wanted to print the phrase variable but also convert the string within the phrase variable to uppercase, I would use the upper() method. So naturally, I would enter the following:
However, as I type the “.” after the phrase variable, the editor would provide a list of suggested methods. Pretty cool, right? This may be a simple example. However, as you’re working with more complex or new libraries, this feature can really help save you time. And another tip: You can also trigger IntelliSense by typing Ctrl+Space.
4. Leverage the Python extension for unit testing
Visual Studio Code has a Test Explorer which is where you can do all your testing. You can do your testing with either the unittest, pytest or nose framework. You’ll need to do some of the heavy lifting with setting up your tests properly; the Python extension can take over when it’s time to discover and enable your tests. Should you choose to use pytest or nose, you’ll need to install those frameworks before they can be used for unit testing.
5. Work with Jupyter Notebooks in Visual Studio Code
My final tip is likely a great benefit for those of you who work with Jupyter Notebooks. Visual Studio Code provides Jupyter Notebook support. You can open, create and modify .ipynb files directly in the editor. One of the great things about working with notebooks in Visual Studio Code is that you can take advantage of all the editor’s editing and debugging features that aren’t typically available for notebooks in a browser. Whether you’re creating a new .ipynb file or opening an existing one, the editor opens the notebook in the Jupyter Notebook Editor Interface. Feel free to run a notebook locally or by connecting to an external server.
There’s so many other great features available within the editor that can help make your development workflow quite productive and efficient. I cover a great deal of those features within my book Visual Studio Code for Python Programmers . If you’re itchin' to get started today, head over to code.visualstudio.com to download the editor and check-out the documentation.