简体   繁体   English

如何在 Visual Studio Code 中调试诗歌应用程序

[英]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)提供一些额外的上下文。

Assumptions假设

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 扩展

  • To check, use 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.如果已安装,它将没有安装按钮。

1. Setup Poetry project 1.设置诗歌项目

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 )将目录更改为项目所在的文件夹

  • This is where the pyproject.toml & poetry.lock files will be created这是创建pyproject.tomlpoetry.lock文件的地方

1.4. 1.4. Type poetry init into the terminal/PowerShell and press Enter .在终端/PowerShell 中键入poetry init并按Enter

  • Poetry will walk through the setup interactively and ask what packages you want for this project. Poetry 将以交互方式遍历设置,并询问您想要该项目的哪些包。 After it's finished, it will create a 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

  • This creates a 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根据您拥有的包裹数量,这可能需要一段时间

2. Using the Poetry virtual environment 2.使用诗歌虚拟环境

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

  • The exit terminal command will simultaneously deactivate the virtual environment and exit the Poetry shell exit终端命令将同时停用虚拟环境并退出 Poetry shell
  • It's worth noting that the Poetry docs seem to recommend setting up a new shell instead of just activating the virtualenv值得注意的是, 诗歌文档似乎建议设置一个新的 shell 而不是仅仅激活 virtualenv
Why you'd want to do (2.): Saving keystrokes when running (not debugging) Python files为什么要这样做(2.):运行(非调试)Python 文件时保存击键

Say 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 pandasyour_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:所以你有几个选择:

Option 1: Run the Python file with Poetry (more keystrokes)选项 1:使用 Poetry 运行 Python 文件(更多击键)

So instead of所以而不是

python.\your_python_file.py

you can use您可以使用

poetry run python.\your_python_file.py

  • This option probably isn't such a big hassle since you can use the up arrow key to get previously entered commands这个选项可能不是那么麻烦,因为您可以使用向上箭头键来获取以前输入的命令
  • This is probably a better option if you expect to be debugging a lot since the debugger doesn't work within the Poetry shell (read the section "Some final (important) notes" at the end)如果您希望进行大量调试,这可能是一个更好的选择,因为调试器在 Poetry shell 中不起作用(阅读最后的“一些最终(重要)注释”部分)
Option 2: Create a Poetry shell and run the Python file选项 2:创建诗歌 shell 并运行 Python 文件

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

  • If you go with option 2, then you'll need to remember to use the terminal command 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
  • You need to exit the shell if you want to use the VS Code debugger如果要使用 VS Code 调试器,需要退出 shell
  • It'll be pretty noticeable that you're in the Poetry shell since it's actually not a PowerShell (it's a Windows Command Prompt aka CMD), so commands like ls and cat your_python_file.py won't work很明显,您在 Poetry shell 中,因为它实际上不是PowerShell (它是ls命令提示符和cat your_python_file.py命令)

3. Change VS Code Python interpreter for Poetry 3.更改诗歌的VS Code Python解释器

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 + PF1

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

  • Note: If nothing shows up, then you probably don't have a virtualenv yet.注意:如果没有显示,那么您可能还没有 virtualenv。 You can check by using the 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. Running & using the VS Code debugger 4. 运行和使用 VS Code 调试器

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设置您希望调试器停止的任何断点

  • This can be done by clicking to the left of a line number.这可以通过单击行号的左侧来完成。 A red dot should appear应该出现一个红点

4.2. 4.2. Press F5 to begin the debugger.F5开始调试器。

  • VS Code will probably pop up some options under "Select a debug configuration". VS Code 可能会在“选择调试配置”下弹出一些选项。 If so, just select "Python file Debug the currently active Python file"如果是这样,只需 select “Python 文件调试当前活动的 Python 文件”

4.3. 4.3. Press F11 to step through the programF11单步执行程序

Some final (important) notes一些最终(重要)说明

  • Any time you try to run the VS Code debugger, it'll use the Python interpreter you set up in section "3. Change VS Code Python interpreter for Poetry".每当您尝试运行 VS Code 调试器时,它都会使用您在“3. Change VS Code Python interpreter for Poetry”部分中设置的 Python 解释器。 Simply repeat those steps if you want to debug a different Poetry project如果您想调试不同的 Poetry 项目,只需重复这些步骤
  • The VS Code debugger will only work when you're not in the Poetry shell . VS Code 调试器仅在您不在 Poetry shell 中时才能工作 So if you went with option 2. in "Why you'd want to do (2.)", then you need to use the terminal command exit before running the debugger因此,如果您在“为什么要这样做 (2.)”中选择选项 2.,那么您需要在运行调试器之前使用终端命令exit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM