How to configure VS Code for AI, ML and MLOps development in Python 🛠️️

MĂŠdĂŠric Hurier (Fmind)
12 min readNov 23, 2023

--

Visual Studio Code is a remarkable application. In the past, developers faced a choice between simple, lightweight text editors like Vim, Atom, and Emacs or powerful yet complex IDEs such as PyCharm and Eclipse. I can recall a time when running my IDE (Netbeans), my browser (Firefox), and my application (Java) simultaneously on a laptop with just 512MB of RAM was a challenge. Today, with VS Code, programmers have access to a versatile, modern, powerful, open-source tool well-supported by the community.

But possessing a good tool is only the beginning. Just as a skilled craftsman must master their tools, it’s essential for programmers to become proficient with their editor for enhanced productivity. In this article, I will outline the steps for configuring VS Code for data scientists and machine learning engineers. I’ll start by listing extensions that augmente your programming environment. Then, I will share some settings and keybindings to enhance your development experience. Finally, I’ll provide tips and tricks to boost your coding efficiency with VS Code.

Photo by Andrew Ruiz on Unsplash

Extensions 🔌

This section lists the extensions you can install from VS Code Marketplace.

A Tier: The Must

B Tier: The Great

C Tier: The Good

S Tier: The Super (Optional)

Three features have remarkably enhanced my programming productivity throughout my career: VIM, Terminal, and GitHub Copilot. Each of these features contributes to a minimum of a 30% increase in my daily output. Yet, they may be one of the most difficult VS Code features to master.

Vim 📝

If we were living in a simulation, I’m convinced that Vim (and LISP) would be the tools used to program it. Programmers spend a significant portion of their day working with text, and Vim allows you to manipulate text at an astonishing pace. Instead of moving the cursor with a mouse, you can perform high-level operations with just a few keystrokes. For example, “Move the cursor to the next word” is ‘w,’ “delete the current line” is ‘dd,’ or “copy the following line seven times” is ‘yy7p.’ These operations can be combined to create complex commands, such as “Replace all the content between the current parentheses” using ‘ci(‘. The possibilities are virtually limitless, to the extent that there are even games designed to teach Vim usage!

Vim Cheatsheet: http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Allow me to emphasise the value of Vim with an anecdote. During my master’s degree, I encountered a configuration issue with my Apache HTTP Server and sought help from my teacher, a sysadmin at Peugeot (now Stellantis). He used Vim, and his actions were so rapid that I couldn’t keep up. In a matter of minutes, he had troubleshooted my problem. That’s when I made the decision to master Vim and the Linux ecosystem.

The best part is that you can use Vim within VS Code as your primary or secondary keybinding. All it takes is installing the VSCodeVim extension and configuring some settings. Keep in mind that mastering Vim requires time, effort, and practice, but if you plan to edit lots of code files in the near future, it’s a valuable investment!

Bonus: You can use Vim in your Browser with the Vimum extension.

Terminal 👨‍💻

Before switching to VS Code, my tools of choice were NeoVim (or Spacemacs), and the terminal served as my IDE. A terminal provides access to an extensive array of tools and commands (cp, tee, awk, grep, find, and more) that can be combined in countless ways. Need to sort and deduplicate all your files? Just use find . -type f | sort | uniq. Want to resize a batch of images? You can do it with find . -type f -name "*.jpg" -exec mogrify -resize 100x100 {} \;.

Although the terminal isn’t an extension, it’s a vital feature of VS Code, and its power should not be underestimated. How many times have I needed to troubleshoot issues or perform advanced tasks that would have required writing complex programs. The terminal’s flexibility is a true asset!

Command Line Fu: https://xkcd.com/196/

If you’re interested in configuring your terminal, numerous individuals share their “dotfiles” repositories on GitHub. These repositories contain the tools and configurations they use to enhance their command-line interface. You can find mine at this address: https://github.com/fmind/dotfiles. Please note that to install them, you’ll need both Python and Ansible.

GitHub Copilot ✈️

GitHub Copilot is a game changer for programmers. Once you figure out the program in your head, you have to type it and the time spent between thinking to typing maybe 1:10. What if you could just “Tab” you way out? Instead of writing the text yourself, express your intent with clear names or comments, and let GitHub Copilot write the rest for you by pressing “Tab”. This is an autocompletion engine cranked up to 9999, and it is even better now with its new GitHub Copilot Chat feature!

Example of GitHub Copilot in action: https://github.com/features/copilot

GitHub Copilot proves invaluable in various scenarios: it assists in generating unit tests from code definitions, completes unfamiliar API calls, and creates relevant examples based on your context. It’s intelligent enough to examine your project’s files and adapt to your coding style. This experience is both gratifying and highly productive, and I strongly recommend trying Copilot as soon as possible!

Settings and Keybindings 🤖

Within VS Code, there are two sets of configurations: Settings that control the application’s behavior and Keybindings that modify your shortcuts. Both can be edited from the “Gear” icon located at the bottom left of VS Code.

Settings 🔧

Below, you’ll find the link to the settings I utilize with VS Code. Each setting is annotated to help you understand what is does. Keep in mind that you can hover your cursor over a setting key in VS Code to access more information about that setting. You can also switch between the textual and visual interface using the “Go to document” icon at the top left of the editor settings.

Remember that it’s essential to tailor the settings to your preferences since every user is unique. Additionally, strive to understand what each setting does to optimize your satisfaction and productivity.

https://gist.github.com/fmind/67c1ce34d0f21005b4aa7aa79b9162aa#file-settings-json (163 lines)

Keybindings 🎹

Keybindings are primarily a matter of personal preference. Some individuals may opt for minor modifications, while others may choose to completely rebind commands to their liking.

In my case, I prefer to assign quick shortcuts to the most frequently used commands and utilize the command selection (CTRL+SHIFT+P) for less common commands. To execute these quick shortcuts, I employ a technique I call the “Alt-key trick.” You’re welcome to adopt this approach if you find it beneficial or explore alternatives like VSpaceCode (Spacemacs keybindings for VS Code).

The Alt-key Trick 🗡

By default, most desktop applications use the Alt key to navigate through the application menu, allowing actions like “Alt+i i” to access the “Insert” menu and insert an image into a Word document. In VS Code, you can disable this behavior by setting "window.enableMenuBarMnemonics": false and create your own custom keybindings. Another advantage is that this behavior works across all major operating systems: Linux, Mac, and Windows.

The code snippet provided below replaces Alt-key shortcuts with custom keybindings. For example, pressing “Alt+j” will move to the next editor, and “Alt+,” will open a new terminal. The code snippet is divided into several sections:

  1. ACTIONS: Primary shortcuts associated with each key on your keyboard.
  2. DISABLED: Resolving conflicts found in the extensions I’m using.
  3. JUPYTER: Additional shortcuts for Jupyter.
  4. POPUPS: Navigating popup menus similar to Vim.
  5. TERMINALS: Shortcuts related to terminal manipulation.
https://gist.github.com/fmind/67c1ce34d0f21005b4aa7aa79b9162aa#file-keybindings-json (300 lines)

Tips and Tricks 👉

1. Synchronization ↔️

Leverage VS Code’s ‘Settings Sync’ feature for a seamless development experience across multiple machines. Activate this by clicking the ‘Gear’ icon at the bottom-left of the application to synchronize your configurations, facilitating effortless transitions between different work profiles like Java/Python or personal/professional.

https://code.visualstudio.com/docs/editor/settings-sync

2. Command Palette 🎨

Quickly access VS Code’s shortcuts through the Command Palette by using ‘CTRL+SHIFT+P’. Ideal for infrequent commands or when the exact name eludes you. This feature is an essential tool in the VS Code arsenal!

https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette

3. Put the mouse away 🐭

Optimize your coding workflow by mastering keyboard shortcuts; they surpass the mouse in boosting typing efficiency. For instance, Vim enthusiasts can adopt the recommended hand positioning below to enhance performance.

Consider remapping the Caps Lock key to Ctrl (Windows/Linux) or Cmd (Mac) to prevent finger strain.

https://stackoverflow.com/questions/14021283/position-of-fingers-for-better-productivity-in-vim

4. Code Workspace File 🗄️

Customize your VS Code environment globally, by folder, or using a workspace. Workspaces bundle folders for project management, ensuring consistent settings among developers.

Create one via ‘File’ > ‘Save Workspace As…’ to generate a ‘code-workspace’ file, typically placed at your project’s root.”

VS Code Workspace for the MLOps Python Package project on GitHub

5. Python Interactive Window 🕹️

Transform your Python script into an interactive notebook within VS Code using the Python Interactive Window. Insert ‘# %%’ comments to delineate cells. Edit on the left, execute with ‘Ctrl+Enter’, and view results on the right. You can greatly streamline your coding with this simple integration!

Python Interactive Window on a simple script

6. Automation with Make or Invoke 🤖

Automate repetitive commands with a task automation system, directly through VS Code or your shell, to enhance productivity. Consider these three systems:

  • Make: A time-tested tool compatible across all platforms.
  • Invoke: Python specific. Great if you want to mix Python code and shell commands. You can improve VS Code integration with this extension.
  • VS Code Tasks: while native to VS Code, I favor Make or Invoke for better collaboration with other developers.
Example of Invoke tasks in https://github.com/fmind/mlops-python-package

7. Xonsh, Fish, Zsh, PtPython, and others 🐚

Move beyond basic bash with these more advanced shells:

  • Xonsh: merges the familiarity of Python with shell capabilities — a harmonious blend for those preferring Python’s syntax over bash’s complexity.
  • Fish: affers a superb out-of-the-box experience, though it may not always align with bash standards — a choice for the bash-averse :)
  • Zsh: outshines bash in power but requires fine-tuning. Supercharge it with https://ohmyz.sh/
  • IPython/PtPython: not for system shell use, but excellent for an enriched Python interpreter interface.
Xonsh shell used for executing Python code and Make tasks https://xon.sh/

There are also tons of resources only to further extend your configuration:

8. Text file can be used for so many things 📄

With proficiency in VS Code, you’ll discover that text-based operations often outperform GUIs in productivity. Activities suited for a text-centric approach include:

Integration of Mermaid with VS Code https://marketplace.visualstudio.com/items?itemName=bierner.markdown-mermaid

9. Look at other configurations on GitHub 😺

Developers love to publish their settings and keybindings on GitHub, and VS Code is no exception. You can check this topic to tracks VS Code configurations on GitHub.

I also recommend you subscribe to these sources of tips and tricks:

https://vscode.email/about

10. Use GhostText to use VS Code on the web 👻

When VS Code’s reach seems limited, GhostText emerges as an ally. This Browser/VS Code extension enables you to edit browser text areas directly in VS Code. It offers bi-directional sync, allowing edits anywhere, anytime. Disengage sync when your editing is complete.

https://ghosttext.fregante.com/

Conclusions 🌅

Congratulations on reaching the end of this guide! Your dedication to refining your development environment is commendable. Remember, the crux of an optimal setup is personalization. Embrace and experiment with your tools’ potential to suit your unique preferences.

Farewell, skilled developer, may your code be efficient and your professional path successful!

Photo by Taylor Brandon on Unsplash

--

--

MĂŠdĂŠric Hurier (Fmind)

Freelancer: AI/FM/MLOps Engineer | Data Scientist | MLOps Community Organizer | MLOps Coding Course Author | MLflow Ambassador | Hacker | PhD