Debugging Python in Linux with VS Code

Carlos Bocanegra
5 min readJan 8, 2021

In this article I am going to show you all you need to start coding and debugging Python code in Linux, for this example I will use Kali but you can use any other Debian based distribution.

We will also use VS Code , which is a free and very popular software development environment.

Prerequisites

First thing to do is to get Python and VS Code set up.

Python

Python comes preinstalled in the latest images of Kali and Ubuntu but if you are not sure if you have it installed you can check it by typing the following commands on a terminal:

If you don’t have it installed, you can run these lines to get Python3 set up:

sudo apt update && sudo apt upgrade && sudo apt install python3

VS Code

The next thing we need to do is to get VS Code installed, the tricky part is that VS Code is not included by default in the common apt source lists so we need to manually add it. But it is no big deal, you can follow the next steps to get it set up (credits to Rafael Hart on his post Install Visual Studio Code on Kali Linux):

Download the Microsoft GPG key, and convert it from OpenPGP ASCII armor format to GnuPG format

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg

Move the file into the apt trusted keys directory

sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

Create an apt source list file with the VS Code repository

echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > vscode.list

Move it to the apt source list directory

sudo mv vscode.list /etc/apt/sources.list.d/vscode.list

Update the apt sources and install VS Code

sudo apt update && sudo apt install code

Visual Studio Code is very powerful and easy-to-use but it can take you some time to get familiar with it for the first time, if you want to learn more about the basic features I recommend you to take a look at the guide Getting Started With Visual Studio Code.

Python Extension for VS Code

Open VS Code, then open a new Integrated Terminal (Ctrl + `) and execute the following command to get the Python extension installed:

code --install-extension ms-python.python

You can also install the extension by searching for ms-python in the Extensions panel and clicking on install.

Debug Python Code

If you are new to Python, I recommend you to download some sample code that you can use to explore different commands and code syntax. The following repository is a good option to start:

git clone https://github.com/anthonydb/python-get-started.git

Open the Command Palette (Ctrl + Shift + P) and search for Python: Select Interpreter, then select the python kernel you want to use:

TIP: If the Python options do not show up in the Command Palette search try restarting VS Code.

Create or open a python file and set a breakpoint by clicking on the left side of a line of code or by pressing F9

Debug the file by pressing F5 or by clicking on the green arrow at the top right of the window. Then select the option Python File (or just hit Enter).

Use the tools at the top to navigate through the code and see the output results in the terminal at the bottom. You can also hover over the objects and see their current value.

Execute code in runtime using the Debug Console.

Or explore all the runtime values in the debugging panel on the left section

Python Virtual Environments

The virtual environments in Python are useful to create isolated python instances and install or modify packages locally without affecting any other instances or projects. If you are familiar with npm, you can compare it to the way you can install packages globally or locally.

Debug Python using a Virtual Environment

Install Python3 Virtual Environments and Pip packages (if not installed yet)

sudo apt install python3-venvsudo apt install python3-pip

Create a new environment for the current project and activate it by running these lines below in the integrated terminal (in VS Code). This will create a .venv folder at the root level of your source code repository which will include the files needed to run your new local python environment.

python3 -m venv .venvsource .venv/bin/activate

Select the new virtual environment with Python: Select Interpreter command in the Command Palette (Ctrl + Shift + P)

Create a new python file where you use packages, for example:

from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
plaintext = b'Some sample plaintext'
key = get_random_bytes(32)
iv = get_random_bytes(16)
cipher1 = AES.new(key, AES.MODE_CBC, iv)
encrypted = cipher1.encrypt(pad(plaintext, 16))
cipher2 = AES.new(key, AES.MODE_CBC, iv)
decrypted = unpad(cipher2.decrypt(encrypted), 16)
is_valid = plaintext == decrypted
print ("Passed" if is_valid else "Failed")

Install the required packages, this will be saved locally as we have switched the interpreter to the local virtual environment (for this example we will need to install PyCryptodome package):

python3 -m pip install pycryptodome

Debug the file (F5). The decrypted value should be equals to the original plaintext, which means the Crypto library did its work correctly.

If you change the python interpreter back to the global environment you should get an error if you do not have the needed packages installed globally:

Deactivate the local environment when done (in the integrated terminal)

deactivate

If you want to know more about VS Code debugging check out the documentation here.

I hope you have enjoyed this article and have learnt something new today! Feel free to reach out for any questions or feedback and…

Happy Coding!

--

--

Carlos Bocanegra

Software security professional with 16 years of experience in all the areas of the software lifecycle.