Debugging Poetry with Visual Studio Code (2024)

17 Apr 2021 #Python

Debugging Poetry with Visual Studio Code (1)

A guide on how to set up debugging of Poetry in Visual Studio Code, using Pipx and Pyenv.

Prerequisites

My developer environment is Ubuntu 20.04 via WSL2 running on Windows 10, so that’s what this guide is written for. In order to get set for debugging, we need to install/setup a couple of tools:

  • Pyenv - Python version management
  • Pipx - install and run Python applications in isolated environments
  • Poetry - you should know what this is, if you are reading this ;)

Pyenv

I like to pick the Python interpreter version for a system-wide installations of tools, but also for individual projects using Pyenv. Installation instructions can be found here and pyenv’s prerequisites can be found in their wiki.

Let’s install Python 3.9.2 for system-wide installed tools and Python 3.8.8 for development/debugging of Poetry. The versions selected are just used to illustrate that different interpreters can be used for Poetry and the projects themselves.

$ pyenv install 3.9.2$ pyenv install 3.8.8

Try it out, once installed, with e.g. Python 3.9.2:

$ pyenv global 3.9.2 # set the 'python' command to use this new version$ python --versionPython 3.9.2$ pyenv global system # reset back to system default

Pipx

Once pyenv is installed and a Python interpreter of choice is available, I like to have an installation of poetry from the same branch or pull request I am about to develop/debug. This can easily be maintained using Pipx.

Let’s install pipx into the Python interpreter version of choice:

Make sure to follow the pipx installation instructions and add ~/.local/bin to $PATH, for example.

Poetry

As a last prerequisite, I’ll install a “generic” Poetry version via pipx. This is so that we can bootstrap Poetry’s own development installation.

When debugging in Poetry’s master branch, I’ll install Poetry from the latest commit in master. I also like to install Poetry using a pipx-suffix. In this case I’ll use the suffix @master and make the Poetry executable available as poetry@master.

$ pipx install --suffix=@master --force git+https://github.com/python-poetry/poetry.git'

Sometimes I might want to install Poetry from a GitHub pull request (in this example pull request #3967) and make this version of Poetry available via the poetry@3967 command:

$ pipx install --suffix=@3967 --force 'poetry @ git+https://github.com/python-poetry/poetry.git@refs/pull/3967/head'

Note that the --force command makes it possible to run those commands again, to “update” to the current code in either master or in the pull request.

Visual Studio Code debug setup

This consists of a few steps:

  • Download the Poetry source code
  • Set up the virtual environment
  • Visual Studio Code setup

Download the Poetry source code

In order to develop and debug poetry, we first need to clone down the git repo’s source code:

$ git clone https://github.com/python-poetry/poetry.git

Set up the virtual environment

In the project folder of poetry, we can create a .python-version file, read by pyenv and which will set the Python interpreter version invoked by the python command:

$ cd poetry$ pyenv local 3.8.8 # creates .python-version$ python --versionPython 3.8.8

Always make sure you’re up to date with pip:

pip install --upgrade pip

Let’s now create a virtual environment and install the Poetry development environment. This can be done in several ways:

$ python -m venv .venv$ source .venv/bin/activate$ poetry@master install
$ pip install virtualenv$ virtualenv .venv$ source .venv/bin/activate$ poetry@master install

However, the below will not work, as Poetry (at least not currently) does not support reading the .python-version file, created from the pyenv local 3.8.8 command:

# WARNING: this will NOT work!$ poetry config virtualenvs.in-project true$ poetry@master install # creates the ".venv" automatically$ source .venv/bin/activate$ python --versionPython 3.9.2 # here we expected Python 3.8.8!

So I would go either with venv or virtualenv. Moving on…

Even if poetry@master uses Python 3.9.2, it will still be able to complete an installation in the Python 3.8.8 virtual environment!

Now you have the poetry command at your disposal, as well as python -m poetry, provided by the development installation. The latter is what we are going to use when debugging!

Visual Studio Code setup

Launch Visual Studio Code and open the poetry project folder, containing all source code. Make sure you have the Python extension and all other necessities for sane Python development. ;)

Also select your virtual environment (.venv) as the active Python interpreter for the project.

Set up tasks.json

Debugging in Visual Studio Code is set up in the project folder’s .vscode/launch.json file, so let’s create it:

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "poetry install", "type": "python", "request": "launch", "module": "poetry", "args": [ "-vvv", // change or comment out for different verbosity level "install" ], }, { "name": "poetry update", "type": "python", "request": "launch", "module": "poetry", "args": [ "-vvv", // change or comment out for different verbosity level "update" ], } ]}

You can see in the above file that I have added two basic examples of executing Poetry; poetry install and poetry update. The way this works is that Visual Studio Code will wrap e.g. the python -m poetry install -vvv command in its debugger.

Note that we no longer use e.g. poetry@master. This was only meant to bootstrap the development environment and make the poetry command available in the virtual environment.

Set breakpoints and run

Add breakpoints by clicking just left to the line number of the code you wish to debug.

Now, in the debug menu, from the “Run and debug” section (upper left corner), you can pick between the different configurations (from the .vscode/launch.json) and execute them (click the green “play” button). Visual Studio Code’s debugger wrapper will now execute the command and stop the execution on your breakpoints.

Debugging Poetry with Visual Studio Code (2)

Inspect objects and navigate the call stack to the left and use the navigation in the top center to continue, step over/into/out of, restart or stop.You can also view the terminal or use the debug console at the bottom. Keep track of your breakpoints in the lower left corner section “Breakpoints”.

More on debugging in Visual Studio Code here.

Happy developing/debugging!

Debugging Poetry with Visual Studio Code (2024)

FAQs

Is VS code good for debugging? ›

One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile, and debug loop.

How do I debug multiple files in Visual Studio Code? ›

  1. When you press Ctrl + Shift + B , VS Code will ask you which one you want to build.
  2. To debug or start without debug, you press Ctrl + Shift + D then on the left top selector, you select which project you want to run.
May 16, 2015

How do I fix the python path in your debug configuration is invalid? ›

To Solve The Python path in your debug configuration is invalid in vscode just Open Command Palette (Ctrl + Shift + P). Type Python: Select Interpreter, open it Select the python interpreter or enter the full path to the python interpreter.

Can you debug python in VS code? ›

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

Which is better VS Code or Visual Studio? ›

Visual Studio Code, first released in 2015, is an open-source rich text editor that runs on Windows, macOS, and Linux. Unlike Visual Studio, VSCode is a much lighter installation that needs to take advantage of plugins for a better coding experience.

Why is VS Code so popular? ›

Its popularity is due to the growth of the web development field in these years and the need of the developers of having a lightweight well-done editor, with few features but less complex than the others available on the market.

What is a breakpoint in debugging? ›

Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint.

Can I open two projects in Visual Studio Code? ›

You can work with multiple project folders in Visual Studio Code with multi-root workspaces. This can be helpful when you are working on several related projects at one time. For example, you might have a repository with a product's documentation that you like to keep current when you update the product source code.

How do I trace code in Visual Studio? ›

Begin code stepping by selecting F10 or F11. Doing so allows you to quickly find the entry point of your app. You can then continue to press step commands to navigate through the code. Run to a specific location or function, for example, by setting a breakpoint and starting your app.

What is better than VS Code? ›

Atom, Visual Studio, Eclipse, IntelliJ IDEA, and WebStorm are the most popular alternatives and competitors to Visual Studio Code.

What language is Visual Studio Code written in? ›

Visual Studio Code

Is VS Code good for C++? ›

Visual Studio Code by Microsoft

Although it's not created solely for C++ development (the way CLion is,) VS Code still provides some excellent performance features. Primarily thanks to the use of extensions and the marketplace built by Microsoft around the IDE.

What languages is VS Code best for? ›

Python is the most popular programming language used in VS Code, if gauged by downloads of the top language extensions, which provide language "smartness" in the popular open source, cross-platform code editor.

Is Visual Studio Code easy? ›

Visual Studio Code is a code editor in layman's terms. Visual Studio Code is “a free-editor that helps the programmer write code, helps in debugging and corrects the code using the intelli-sense method ”. In normal terms, it facilitates users to write the code in an easy manner.

Do companies use Visual Studio Code? ›

We have data on 115,134 companies that use Microsoft Visual Studio. The companies using Microsoft Visual Studio are most often found in United States and in the Computer Software industry. Microsoft Visual Studio is most often used by companies with 10-50 employees and 1M-10M dollars in revenue.

How do you Debug a test complete? ›

To enable test debugging, select Debug > Enable Debugging from the TestComplete main menu. While your test is running, you can switch to TestComplete just as you would switch to any Window's application, and set a breakpoint. The new breakpoint will be set, and your script will pause when it reaches the breakpoint.

Why breakpoint is not hitting with VS code? ›

If a source file has changed and the source no longer matches the code you're debugging, the debugger won't set breakpoints in the code by default. Normally, this problem happens when a source file is changed, but the source code wasn't rebuilt. To fix this issue, rebuild the project.

How do you Debug a test case? ›

Debug mode​

Open a test case and switch to the Script view. Double-click on the leftmost side of the script editor to mark a breakpoint. A breakpoint is where Katalon Studio pauses the test execution for you to start debugging. Choose a browser for Debug from the main toolbar.

How do I debug multiple projects in Visual Studio? ›

Visual Studio allows you to specify how more than one project is run when you press F5 (Start with Debugging), or Ctrl+F5 (Start without debugging), or use the toolbar button to launch your application.

How do I make multiple projects in one solution in Visual Studio? ›

To add existing projects to the solution: Right click the Solution node in Solution Explorer select Add -> Multiple Projects.

How do I run multiple codes in VS Code? ›

How to run multiple VS Code commands with the multi ... - YouTube

How do I debug in Visual Studio? ›

  1. Applies to: Visual Studio Visual Studio for Mac Visual Studio Code.
  2. To debug, you need to start your app with the debugger attached to the app process. ...
  3. Press F5 (Debug > Start Debugging) or the Start Debugging button. ...
  4. To start your app with the debugger attached, press F11 (Debug > Step Into).
Apr 29, 2022

How do I use VS Code debugger in Chrome? ›

To debug any project in either Chrome or Microsoft Edge, all you need to do is to start a session by pressing F5 or activating the debug icon in the menu bar and selecting “Run and debug”. Alternatively, you can also use the Visual Studio Code command palette and run the “Debug: Open Link” command.

How do I enable debugging in Visual Studio? ›

Debug ASP.NET Core apps
  1. Select the ASP.NET Core project in Visual Studio Solution Explorer and click the Properties icon, or press Alt+Enter, or right-click and choose Properties.
  2. Select the Debug tab and click the link to open the Open debug launch profiles UI. ...
  3. Select the profile to configure for debugging.
Jun 7, 2022

How do I debug in VS Code react native? ›

You can launch your app either in Android or iOS with the standard commands you use. Once it is running, click the play button for that option to debug your app. This works when you are having trouble launching an emulator or simulator with a debug configuration, too.

How do I debug a script in Visual Studio? ›

Debug server-side script
  1. With your project open in Visual Studio, open a server-side JavaScript file (such as server. ...
  2. To run your app, press F5 (Debug > Start Debugging). ...
  3. Press F5 to continue the app.
  4. If you want to use the Chrome Developer Tools, press F12 in the Chrome browser.
Jun 28, 2022

How do I change debugger in VS Code? ›

VS Code - Debugging - YouTube

How do you debug HTML code? ›

To open DevTools, press Command + Option + I (Mac), Control + Shift + I (Windows, Linux), or F12. This shortcut opens the Console panel. We will look at the debugger in more detail in the Java Script chapter. There we will talk about breakpoints, for example.

Does Visual Studio have a debugger? ›

The Visual Studio debugger can help you navigate through code to inspect the state of an app and show its execution flow. You can use keyboard shortcuts, debug commands, breakpoints, and other features to quickly get to the code you want to examine.

Which options are available in Debug mode? ›

Some of the major functions available with on-chip debug include: Interrupt or break into debug mode on program and/or data memory address. Interrupt or break into debug mode on a peripheral access. Enter debug mode using a DSP microprocessor instruction.

Can I code React Native in VSCode? ›

This VS Code extension provides a development environment for React Native projects. Using this extension, you can debug your code and quickly run react-native commands from the command palette.

How do you shake an emulator? ›

I am writing console. log statements as I develop. I want to see these logging statements in Chrome while I'm running the Android app in the default Android emulator. According to Facebook's docs I just need to "shake the device".
...
  1. click on the (...) (its extended controls)
  2. close extended controls.
  3. ctrl+m.

Which software is used for React Native? ›

Visual Studio Code is by far the most popular React Native development tool out there. It is the most used IDE for React Native development, and it works across all platforms (like Windows, Mac, and Linux).

Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6545

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.