[英]How to debug Poetry application in Visual Studio Code
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.我有一个应用程序,我被授予使用诗歌写作的所有权,我有一些 Python 经验,但以前从未使用过诗歌。 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.我选择的编辑器是 Visual Studio Code(在 Windows 上),但是我不知道在 launch.json 中要放什么,所以我可以调试应用程序。
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然而,这在应用程序之外似乎不可能,launch.json 想要一个绝对路径,我做不到
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.如果我不提供绝对路径 VS Code 会将当前目录放在 if 前面。 I did try pointing at the generated file in the virtualenv我确实尝试指向 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.这只是导致程序在终端中运行,没有调试。
Run code from poetry environment:从诗歌环境运行代码:
poetry shell
code .
Please check this tutorial: https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2/请查看本教程: https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2/
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".我写这篇文章对其他读者来说有点详尽,但 OP @Mant101 可能只需要执行第 (3.) 和 (4.) 部分并阅读最后一部分“一些最终(重要)注释”。
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)解释该步骤正在做什么,(b)解释该步骤的一些常见问题以及如何解决它们,或(c)提供一些额外的上下文。
A.1. A.1。 You're using Windows您正在使用 Windows
A.2. A2。 VS Code is installed VS 代码已安装
A.3. A.3。 VS Code's default terminal is PowerShell (I'll be using "terminal" and "PowerShell" interchangeably) VS Code 的默认终端是 PowerShell(我将交替使用“终端”和“PowerShell”)
A.4. A.4。 Poetry is installed诗歌已安装
A.5. A.5。 The Python extension by Microsoft from the VS Code extensions market place is installed安装了 Microsoft 来自 VS Code 扩展市场的 Python 扩展
Ctrl + Shift + X
in VS Code to open up the extensions market place and search "python".要检查,请在 VS Code 中使用Ctrl + Shift + X
打开扩展市场并搜索“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.本节将让 Poetry 将一些已经存在的项目文件夹/目录变成实际的 Poetry 项目。 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".如果您在要调试文件的文件夹中已经有一个pyproject.toml
文件和一个poetry.lock
文件,那么您可以跳到“2. 使用 Poetry 虚拟环境”部分。
1.1. 1.1。 Open up VS Code打开 VS 代码
1.2. 1.2. Open up the terminal within VS Code (keyboard shortcut: Ctrl + `
)在 VS Code 中打开终端(键盘快捷键: Ctrl + `
)
1.3. 1.3. Change your directory to be the folder where your project exists with a PowerShell command like cd ~\path\to\my_project_folder
使用 PowerShell 命令(如cd ~\path\to\my_project_folder
)将目录更改为项目所在的文件夹
pyproject.toml
& poetry.lock
files will be created这是创建pyproject.toml
和poetry.lock
文件的地方1.4. 1.4. Type poetry init
into the terminal/PowerShell and press Enter
.在终端/PowerShell 中键入poetry init
并按Enter
。
pyproject.toml
file.完成后,它将创建一个pyproject.toml
文件。 This file tells Poetry which versions of Python you're using, what packages, and any constraints.此文件告诉 Poetry 您正在使用哪些版本的 Python、哪些包以及任何限制。 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.11例如,如果您告诉它您只想要 Python 3.8 到(但不包括)3.11,它将具有行python = ">=3.8,<3.11"
1.5. 1.5。 Type poetry install
into the terminal/PowerShell and press Enter
.在终端/PowerShell 中输入poetry install
,然后按Enter
。
poetry.lock
file and a virutalenv for your Poetry project.这将为您的诗歌项目创建一个poetry.lock
文件和一个virutalenv。 It also installs all the packages specified in the pyproject.toml
file within that virtualenv .它还会在该 virtualenv中安装pyproject.toml
文件中指定的所有包。 Depending on how many packages you have, this could take a while根据您拥有的包裹数量,这可能需要一段时间This section creates a Poetry shell and activates the Poetry virtual environment that was created in step (1.5.).本节创建 Poetry shell并激活在步骤 (1.5.) 中创建的 Poetry 虚拟环境。 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.严格来说,你不需要这样做来让 VS Code 调试器工作,但是当你只想运行Python 文件时,它可能会为你节省一些击键。 Go to "Why you'd want to do (2.)" to see if this is worth bothering with. Go 到“你为什么想做(2.)”,看看这是否值得打扰。 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.如果您赶时间,只需 go 和“为什么要这样做 (2.)”部分中的“选项 1”即可在 Poetry 中运行 Python 文件。
2.1. 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
确保您位于带有pyproject.toml
的项目目录/文件夹中,然后在终端中键入poetry shell
并按Enter
exit
terminal command will simultaneously deactivate the virtual environment and exit the Poetry shell exit
终端命令将同时停用虚拟环境并退出 Poetry shellSay you don't want to use the debugger and just want to run your python file.假设您不想使用调试器,只想运行python 文件。 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).您可能习惯于去终端并使用python your_python_file.py
(或python.\your_python_file.py
使用 PowerShell 的选项卡自动完成时)。
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'
.好吧,假设您在 your_python_file.py 中import pandas
了your_python_file.py
但 pandas 并未全局安装(但您确实使用 Poetry 来安装它,无论是在poetry init
阶段还是使用终端命令poetry add pandas
get...一个ModuleNotFoundError: No module named 'pandas'
。
You might be thinking, "Wait, didn't I install pandas with Poetry?"你可能会想,“等等,我不是用 Poetry 安装了 pandas 吗?” 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这涉及首先执行 (2.1.),然后您可以使用
python.\your_python_file.py
or, funny enough,或者,够有趣的,
python your_python_file.py
when using Tab autocomplete.使用 Tab 自动完成时python your_python_file.py
。
exit
when you want to run a Python file in a different Poetry project that has different packages and dependencies如果您使用 go 选项 2,那么当您想在具有不同包和依赖项的不同 Poetry 项目中运行 Python 文件时,您需要记住使用终端命令exit
ls
and cat your_python_file.py
won't work很明显,您在 Poetry shell 中,因为它实际上不是PowerShell (它是ls
命令提示符和cat your_python_file.py
命令)This section tells VS Code to use Python interpreter within the Poetry virtual environment.本节告诉 VS Code 在 Poetry 虚拟环境中使用 Python 解释器。
3.1. 3.1。 Open the command pallet ( keyboard shortcut: Ctrl + Shift + P
or F1
)打开命令托盘(键盘快捷键: Ctrl + Shift + P
或F1
)
3.2. 3.2. Search for "python: select interpreter" and select it with the mouse or navigate with the arrow keys and press Enter
使用鼠标搜索“python:select 解释器”和 select 或使用箭头键导航并按Enter
3.3. 3.3. Select "Enter interpreter path..." Select "输入解释器路径..."
3.4. 3.4. Go back to the terminal/PowerShell ( keyboard shortcut: Ctrl + `
) Go 返回终端/PowerShell(键盘快捷键: Ctrl + `
)
3.5. 3.5. Type poetry env info --path
and press Enter
.键入poetry env info --path
并按Enter
。
poetry env info
command and see if the "Path:" section has "NA".您可以使用poetry env info
命令检查“路径:”部分是否有“NA”。 If this is the case, you can create a virtualenv by doing (1.5.).如果是这种情况,您可以通过执行 (1.5.) 创建一个 virtualenv。 Then repeat step (3.5.)然后重复步骤 (3.5.) 3.6. 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
将步骤 (3.5.) 的结果复制并粘贴到 VS Code 顶部的提示框中,显示“输入 Python 解释器的路径”,然后按Enter
4.1. 4.1。 Open up the.py file you'd like to debug打开您要调试的 .py 文件
4.2. 4.2. Set any breakpoints you'd like the debugger to stop at设置您希望调试器停止的任何断点
4.2. 4.2. Press F5
to begin the debugger.按F5
开始调试器。
4.3. 4.3. Press F11
to step through the program按F11
单步执行程序
exit
before running the debugger因此,如果您在“为什么要这样做 (2.)”中选择选项 2.,那么您需要在运行调试器之前使用终端命令exit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.