Visual Studio Code (VS Code) is a free, lightweight, cross-platform (meaning it works on Windows, Mac, and Linux) integrated development environment (IDE) created and open-sourced by Microsoft. It provides must-have features such as intellisense and syntax highlighting for writing code and tooling for debugging it, as well as plenty of productivity tools and nice-to-haves (code templates/snippets, color themes, etc.). It can support pretty much any language, and it does this primarily through extensions available on the marketplace (don't worry, despite the name, the vast majority of them are free and open-source as well).
Many of these extensions also provide visual interfaces for working on specific types of projects/workloads. For example, the Microsoft Python extension for Visual Studio Code adds support for writing and running Python code in scripts and in Jupyter Notebooks within VS Code with ease. This quick article will focus on getting you up and running with VS Code and Python to make you as productive as possible.
In order to run Python code, you need what is called the Python interpreter. This is essentially software that executes the instructions written in the language directly, without having them first be compiled - so Python is an interpreted language, as opposed to a compiled language (such as C#).
Download Python 3.9 (latest at the time of writing): https://www.python.org/downloads/release/python-390/
pythonand enter and it should tell you the version you just installed
Now that we have Python installed and we want to make VS Code our editor of choice for it, we need to take some steps to make it an optimal environment. Namely, installing a few VS Code extensions as well as updating some settings to make our lives easier and our coding more productive.
Now that you have Python installed, you can start writing code with it. However, what makes computer programming powerful is the fact that you can start using amazing code that has been worked on by others for many years so that you're not constantly recreating the wheel (which often times you/your team don't have the time or even the skills to recreate yourself!). Of course we are talking about Python packages, or libraries, which you can install using a package manager. The default one included with Python is called
Installing packages is simple, you just run
pip install PACKAGE_NAME_HERE, e.g. for the famous
pandas, you'd execute
pip install pandas in a terminal. However, when you do this, it will install the latest version of the package in a global location on your system. And it will get re-used whenever you're writing scripts/notebooks. The trouble is, packages have their own dependencies (packages that they reference) and over time you may run into strange issues as they start to interact with each other in unexpected ways. The solution to that, is creating virtual environments for each major project you do. This doesn't mean you need a virtual environment for every project, but for those that you want to ship or for those that are non-trivial, it is a best practice.
The easiest and recommended way to create and manage your virtual environments is through an additional tool called
pipenv. Check out this article for a great introduction to pipenv and more details as to why it's important and how to get started with it. Again, for basic scripts there is no harm in not using it, but at a minimum it's something to be aware of.
Great news is, VS Code makes it easy to work with Python virtual environments, giving you helpful pop-ups and suggestions when it notices that pipenv was used in a project.
Within VS Code, click on the blocks icon in the left side-panel, or
CTRL+SHIFT+X, to open up the Extensions pane. Search for and install the extensions in the list below, ensuring that the name matches exactly. Take a moment to look at the summary for each, which include more detailed descriptions and often-times short videos demonstrating some of their functionalities.
"""it will bring up a template to help you write your documentation strings faster and in the right format, e.g. for functions that you're writing, which of course lead to better intellisense for you and others reading/using your code.
These aren't crucial by any means, but they certainly make you feel more at home.
There are a couple of settings we need to tweak to greatly improve our lives and have our code get auto-formatted correctly when we save a file, so we're spending our time writing our code and not making sure every little thing conforms to a desired style. This sounds trivial but actually it not only saves you time, but also ensures consistency across your code that you will come to appreciate, as will others (and for a team it's quite imperative).
pip install blackfor it to work.
VS Code really took the dev world by storm - even the likes of Facebook have officially made it their development platform of choice. It was a bit of a surprise coming from Microsoft, which historically had been anti-open-source, but that has completely changed in more recent years, particularly under the leadership of Satya Nadella.
It is my personal favorite, and in particular offers a great debugging experience (you can see all your variables in the script at any given time when you set breakpoints to step through your code) and handles Jupyter Notebooks as well, so you don't need to use a separate interface for that.
However, another great alternative to check out is PyCharm (which has a 100% free communitiy version). At the end of the day, it's all about productivity and getting familiar with our toolset, and between these 2, you really can't go wrong.
Please let me know if you run into any issues or have any questions. Thanks for reading.