I have an application I have been given to take ownership of written using Poetry, I have some Python experience but have never used Poetry before. My editor of choice is Visual Studio Code (on Windows), however I can't figure out what to put in launch.json so I can debug the application.
If I am inside the application folder I can run it fine
app dostuff
or
poetry run app dostuff
However this does not seem possible outside the app, launch.json wants an absolute path and I cannot do
C:\users\me\app\app dostuff
I get an error saying the command is not found. If I don't provide an absolute path VS Code puts the current directory in front of if. I did try pointing at the generated file in the virtualenv
C:\Users\me\AppData\Local\pypoetry\Cache\virtualenvs\app-py3.9\Scripts\app dostuff
That just results in the program running in the terminal, no debugging.
I wrote this to be somewhat exhaustive for other readers, but the OP @Mant101 probably only needs to do sections (3.) and (4.) and read the last section "Some final (important) notes".
For other readers, you can just read the intro paragraphs for the numbered sections and "Some final (important) notes" and follow the numbered steps to (hopefully) arrive at a workable solution. The bullet points beneath the numbered steps either (a) explain what that step is doing, (b) explain some common issues for that step and how to troubleshoot them, or (c) give some additional context.
A.1. You're using Windows
A.2. VS Code is installed
A.3. VS Code's default terminal is PowerShell (I'll be using "terminal" and "PowerShell" interchangeably)
A.4. Poetry is installed
A.5. The Python extension by Microsoft from the VS Code extensions market place is installed
Ctrl + Shift + X
in VS Code to open up the extensions market place and search "python". If it's installed, it won't have a button there to install it.This section will get Poetry to make some project folder/directory that already exists into an actual Poetry project. If you already have a pyproject.toml
file & a poetry.lock
file in the folder within which you want to debug files, then you can skip to the section "2. Using the Poetry virtual environment".
1.1. Open up VS Code
1.2. Open up the terminal within VS Code (keyboard shortcut: Ctrl + `
)
1.3. Change your directory to be the folder where your project exists with a PowerShell command like cd ~\path\to\my_project_folder
pyproject.toml
& poetry.lock
files will be created1.4. Type poetry init
into the terminal/PowerShell and press Enter
.
pyproject.toml
file. This file tells Poetry which versions of Python you're using, what packages, and any constraints. For example, it'll have the line python = ">=3.8,<3.11"
if you tell it you only want Python 3.8 up to (but excluding) 3.111.5. Type poetry install
into the terminal/PowerShell and press Enter
.
poetry.lock
file and a virutalenv for your Poetry project. It also installs all the packages specified in the pyproject.toml
file within that virtualenv. Depending on how many packages you have, this could take a whileThis section creates a Poetry shell and activates the Poetry virtual environment that was created in step (1.5.). Strictly speaking, you don't need to do this to get the VS Code debugger working, but it might save you some keystrokes when you just want to run your Python file. Go to "Why you'd want to do (2.)" to see if this is worth bothering with. If you're in a hurry, just go with "Option 1" in the "Why you'd want to do (2.)" section to run Python files in Poetry.
2.1. Make sure you're in the project directory/folder with the pyproject.toml
and type poetry shell
into the terminal and press Enter
exit
terminal command will simultaneously deactivate the virtual environment and exit the Poetry shellSay you don't want to use the debugger and just want to run your python file. You might be in the habit of going to the terminal and using python your_python_file.py
(or python .\your_python_file.py
when using PowerShell's Tab autocomplete).
So what's the issue? Well, say you have import pandas
in your_python_file.py
but pandas isn't installed globally (but you did use Poetry to install it, either during poetry init
stage or with the terminal command poetry add pandas
)... you'll probably get a ModuleNotFoundError: No module named 'pandas'
.
You might be thinking, "Wait, didn't I install pandas with Poetry?" and the answer is yes, but only Poetry knows that. So you have a couple options:
So instead of
python .\your_python_file.py
you can use
poetry run python .\your_python_file.py
This involves doing (2.1.) first, and then you can use
python .\your_python_file.py
or, funny enough,
python your_python_file.py
when using Tab autocomplete.
exit
when you want to run a Python file in a different Poetry project that has different packages and dependenciesls
and cat your_python_file.py
won't workThis section tells VS Code to use Python interpreter within the Poetry virtual environment.
3.1. Open the command pallet ( keyboard shortcut: Ctrl + Shift + P
or F1
)
3.2. Search for "python: select interpreter" and select it with the mouse or navigate with the arrow keys and press Enter
3.3. Select "Enter interpreter path..."
3.4. Go back to the terminal/PowerShell ( keyboard shortcut: Ctrl + `
)
3.5. Type poetry env info --path
and press Enter
.
poetry env info
command and see if the "Path:" section has "NA". If this is the case, you can create a virtualenv by doing (1.5.). Then repeat step (3.5.)3.6. Copy and paste the result of step (3.5.) into the prompt box at the top of VS Code that says "Enter path to a Python interpreter" and press Enter
4.1. Open up the .py file you'd like to debug
4.2. Set any breakpoints you'd like the debugger to stop at
4.2. Press F5
to begin the debugger.
4.3. Press F11
to step through the program
exit
before running the debugger