Quick Tip: VS Code Snippet for Python Developers Wayne Lambert Published: Tue 29-Sep-20 (4 years, 2 months ago) | Updated: Sun 04-Oct-20 (4 years, 2 months ago) Categories: Infrastructure | Python 575 words | 8 min read A common task when debugging is to be able to inspect your variables within the terminal or your development server. This can involve putting in some print statements within your code. Of course, for a more hefty debugging session, you can start up the VS Code debugger, however it's sometimes nice to get a quick view of your variables. The simplest way is to add a print function: print(variable_name) Python 3.8 makes this even better as you can easily give your print function some context by labelling it with the variable name in addition to its value: print(f"{variable_name=}") This can be a little bit to type out when you're in the mode of rapid development, so this next tip will make this nice and easy. Open your command palette within VS Code. Cmd + Shift + P is the default keybinding for Mac, and Ctrl + Shift + P for Windows and Linux. Type in Configure user snippets into the command palette and select it. Select Python from the list. If you have no snippets yet, this will create a python.json file for you and save it in a snippets directory. On my Mac, for example, it saves the file in the following location: ~/Library/Application Support/Code/User/snippets/python.json There will be some guidance to set up the snippet of your choice. For the snippet that I'm recommending, insert the following into the your file. { "Debug f String": { "description": "Inserts boilerplate for a debug f string", "prefix": "df", "body": [ "print(f\"{${1:variable_name}=}\")", ] } } Now, whenever you type df into your VS Code editor whilst in any Python file, it will autocomplete the remainder of the body and just leave you to overtype the name of the variable. This means you can enter a debug f string for a variable within about 5 or 6 key presses. d -> f -> tab -> {1 or 2 letters of variable name} - > tab Best of all, you don't need to remember the syntax for a debug f string. Now, if you're really lazy, you can use the locals() variable within another snippet because then you can get a more complete view of the local variables you have to hand and in even less key strokes. This time, insert this is into your python.json snippets file: { "Debug f String Locals": { "description": "Adds a debug f string with locals variables inserted", "prefix": "dfl", "body": [ "print(f\"{${1:locals()}=}\")", ] } } Now, this time in your Python file, simply type dfl and you will get a completed debug f string along with the contents of the locals() variable. For example, this can be really useful to insert just inside the return statement of a Django view function/method. One friendly word of warning though, if you're doing any Pandas data analysis, you might want to choose a different shortcut to df since df is used as the canonical reference variable for your dataframe and it might be annoying if you get a debug f string when you want to assign a dataframe. 😀 Finding the Largest… Integrate a Bootstr…