Is it the best code editor for Python and Data Science?
Are you struggling to find an optimal code editor for Python programming and data science? You’re not alone. There’s a ton of options to choose from — both free and paid — and today I’ll show you my favorite free one.
It’s Visual Studio Code — a completely free code editor from Microsoft. It’s by far the most flexible and feature-rich code editor I found. It even has more features than PyCharm Community, which is supposed to be a professional IDE. And don’t even get me started on Jupyter — it’s probably the best way to write notebooks, but notebooks alone aren’t enough for data professionals.
Today you’ll see my go-to approach for setting up VSCode for Python programming and data science. We’ll start with the installation and go through the plugins from there.
Don’t feel like reading? Watch my video instead:
Download and install Visual Studio Code
Head over to code.visualstudio.com to download VSCode. The webpage should detect your OS automatically, so you only need to hit the big blue download button:
On Mac, it will download a ZIP file which you can extract and drag the app to the Applications folder. On Windows, I assume you need to open the .EXE file and click on Next a couple of times, and for Linux, it’s probably either a Terminal command or a DEB file.
Here’s what you should see once you launch it:
You can create a Python file by opening any folder on your machine, right-clicking the left sidebar and selecting New file. I’ve created a folder on Desktop and a
main.py file in it:
By default, you won’t have the best debugging options, nor you’ll have IntelliSense. You also won’t be able to select a virtual environment. Fixing this is quick and easy. You only need to install a plugin. But let’s address something more urgent first.
Download a theme (optional)
The first thing I like to do in VSCode is to change the theme. It has nothing to do with Python and data science, so you can either skip this section or consider it as a bonus point.
The default theme is just too Microsofty for me. To change it, you can click on the Extension tab and search for themes. I particularly like the One Dark Pro theme, which is free, even though it says Pro:
Click on the theme and hit the Install button. The default skin is a bit too light for my liking. On Mac, you can press CMD+K CMD+T to open the theme dropdown. My favorite is One Dark Pro Darker:
Much nicer, isn’t it? Let’s address the extensions next.
Official Python extension
This one is a must-have if you want to work with Python. Go to the Extensions tab once again and search for Python. You should install the official extension from Microsoft:
You’ll now have a much easier time writing Python files. You can also choose a virtual environment now, which is something you’ll do daily. Click on the bottom left text that says Python 3.9.7 64 bit (at least on my machine) and select any environment you want:
Do you know what the best part is? You can immediately start working with Jupyter Notebooks! Create a
.ipynb file to verify — it might prompt you to install some additional dependencies, so just agree to everything.
Once installed, you can enter Python code to a cell to verify everything works:
And that’s it — you now have the basics installed, so you can work with Python either through scripts or through notebooks. Let’s add some additional functionality next.
Python docstring generator
One essential tip for writing good Python code is documentation. VSCode can help you with that. All you need to do is to install the Python Docstring Generator extension.
Let’s see how it works. You’ll write a dummy Python function that sums up two integers:
def add(a: int, b: int) -> int: return a + b
Write the function inside
You can now add a docstring by writing three double quotes below the function declaration and selecting the Generate Docstring option:
It will immediately write the boilerplate for you:
All you’ll have to now is to edit the description and the role each parameter has:
That’s much easier than writing everything from scratch. Maybe you don’t see the benefit because we have only one function, but imagine you had multiple Python modules, each having dozens of functions — then this extension is a huge time saver.
And finally, I want to discuss linting. You can enable linting in VSCode to automatically tell you if you’re not following Python conventions. It will tell you if you have unused imports, variables, or if there’s anything to improve in your code.
To start, open up the Command Palette (Settings — Command Palette… or press SHIFT + CMD + P) and type in Linter. Choose the Select Linter option:
PyLint is the most popular one, so just click on it:
It will ask you to install PyLint if it’s not installed already. You’ll have to repeat the process for every virtual environment, so keep that in mind:
Let’s now delete the
add() function and explore what PyLint has to offer. You’ll import
random modules and print a random integer between 1 and 100:
import json import random print(random.randint(1, 100))
Here’s how it should look like:
You’ll see warning messages as soon as you save the file. The
The top import statement is underlined because we don’t have a file-level docstring on the top, so let’s write one quickly:
The warning won’t go away if you save the file. It now complains that you’ve imported
json but aren’t using it in the file:
The message will go away once you delete the unused import.
To summarize, linters can help you write better Python code and make sure you’re following all the conventions. Your code will still run if the linter gives you warning messages, but it’s annoying to look at them, so you’ll likely address them as they come.
And that’s it for today. My Python and data science setup in VSCode is somewhat simplistic, but it gets the job done.
What are your favorite VSCode extensions for Python and data science? Please let me know in the comment section below.
This article was republished from towardsdatascience.com