在 VSCode 中调试 python 模块的问题 [英] Problem with debugging python modules in VSCode

查看:68
本文介绍了在 VSCode 中调试 python 模块的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python练习项目的目录结构如下:

<预><代码>.├──数据├── ds-and-algo├── 练习|├── __init__.py│ ├── armstrong_number.py│ ├── extract_digits.py├── 输出

extract_digits.py 看起来像这样:

def extract_digits(n):经过

armstrong_number.py 中,我有以下内容:

from .extract_digits import extract_digits

如果我运行,则从项目根目录

python 练习/armstrong_number.py

我得到ModuleNotFoundError:没有名为练习的模块

使用 -m 标志运行以下命令可解决错误:

python -m Exercises.armstrong_number

但是使用 VSCode 来调试文件,我有以下调试配置 launch.json:

<代码>{版本":0.2.0",配置":[{"name": "Python 模块",类型":蟒蛇",请求":启动","program": "${file}","console": "integratedTerminal","pythonPath": "${config:python.pythonPath}","module": "exercises.${fileBasenameNoExtension}","cwd": "${workspaceRoot}","env": {"PYTHONPATH":"${workspaceRoot}"}}]}

但是这有几个问题:

1) 对于不同的文件夹,例如ds-and-algo,我要手动编辑launch.json文件中的模块入口调试配置为

"module" : "ds-and-algo.${fileBaseNameNoExtension}"

如果我有嵌套文件夹配置,例如:

练习├── 艰难||__init__.py|├──ex1.py|├──ex2.py├── 简单

我再次必须手动编辑 launch.json 文件中的调试配置:(考虑子文件夹 tough 的情况)

"module": "exercises.tough.${fileBaseNameNoExtension}"

我需要实现一个一般情况,根据被调试的文件,launch.json 文件中的 "module" 条目应该是:

"module": "folder1.folder2.folder3.....foldern.script"

就像 fileBaseNameNoExtension 一样,VSCode一些其他预定义变量:

其中一个变量是relativeFile,它是当前打开的文件相对于workspaceFolder的路径因此对于文件 ex1.py,变量 relativeFile 将是 exercises/tough/ex1.py.

我需要操作这个字符串并将其转换为exercises.tough.ex1,如果我可以在"module"条目中编写和执行bash命令,这很简单在 launch.json 文件中.但我无法做到这一点.但是,链接VSCode 中的预定义变量 有一个关于命令的部分变量,其中说明:

如果上面的预定义变量不够用,您可以通过 ${command:commandID} 语法使用任何 VS Code 命令作为变量.

此链接包含许多可能有用的其他信息.我不是 python 专家,绝对不知道任何 javascript,如果这是解决这个问题所必需的.

解决方案

我无法重现您的错误:ModuleNotFoundError: no module named practice.

使用以下文件

练习/extract_digits.py

def extract_digits(n):返回 10

练习/armstrong_number.py

from extract_digits import extract_digitsdef armstrong_number(n):返回提取数字(n)如果 __name__ == "__main__":打印 armstrong_number(3)

如果您使用 Python3,请将打印语句更改为:print(armstrong_number(3)) 并更改使用的 Python 解释器.

如果你的当前目录是项目根目录并且你运行

python 练习/armstrong_number.py

您在控制台中得到数字 10


在 Visual Studio Code 中,您使用了错误的启动配置.

您只是在运行 Python 程序,因此您应该使用 Python: Current File 配置.launch.json

<代码>{版本":0.2.0",配置":[{"name": "Python:当前文件",类型":蟒蛇",请求":启动","program": "${file}","console": "integratedTerminal",}]}

  1. 在任何目录中打开要运行/调试的示例 py 文件.
  2. 或者将其设为当前文件
  3. 选择调试侧栏
  4. 选择Python: Current File配置
  5. 点击绿色"三角形.

现在构建并运行一个复杂的命令.它会将 CWD 设置为当前文件的目录并在当前文件上启动调试器.


如果您确实需要启动模块,您可以在 VSC 中使用 Multi-Root Workspace 并为每个模块单独配置一个 launch.json根目录


如果您想使用 ${command:commandID} 语法,您可以构造一个使用 activeTextEditor 的简单扩展,并根据文件名构造一个字符串
编辑

另一种选择是使用多启动配置.

我删除了默认值有效或未使用的属性.

<代码>{版本":0.2.0",配置":[{"name": "练习",类型":蟒蛇",请求":启动","console": "integratedTerminal","module": "exercises.${fileBasenameNoExtension}",},{"name": "Exercise.Easy",类型":蟒蛇",请求":启动","console": "integratedTerminal","module": "exercises.easy.${fileBasenameNoExtension}",},{"name": "DS",类型":蟒蛇",请求":启动","console": "integratedTerminal","module": "ds.${fileBasenameNoExtension}",}]}


您可以使用扩展名 Command Variable 来获取带有点分隔符的相对目录.

<代码>{版本":0.2.0",配置":[{"name": "Python: 模块 CmdVar",类型":蟒蛇",请求":启动","console": "integratedTerminal","module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",}]}

I have the following directory structure for my python practice project:

.
├── data
├── ds-and-algo
├── exercises
|   ├── __init__.py
│   ├── armstrong_number.py
│   ├── extract_digits.py
├── output

The extract_digits.py looks something like this:

def extract_digits(n):
    pass

In the armstrong_number.py I have the following:

from .extract_digits import extract_digits

From root project directory if I run

python exercises/armstrong_number.py

I get ModuleNotFoundError: no module named exercises

Running the following commad with -m flag resolves the error:

python -m exercises.armstrong_number

However using VSCode in order to debug the file, I have the following debug config launch.json:

{
"version": "0.2.0",
    "configurations": [
        {
            "name": "Python Module",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "pythonPath": "${config:python.pythonPath}",
            "module": "exercises.${fileBasenameNoExtension}",
            "cwd": "${workspaceRoot}",
            "env": {"PYTHONPATH":"${workspaceRoot}"}
        }
    ]
}

However this has a few problems:

1) For a different folder, for e.g. ds-and-algo, I have to manually edit the module entry in the launch.json file debug configuration to

"module" : "ds-and-algo.${fileBaseNameNoExtension}"

In case I have nested folder configuration like:

exercises
├── tough
|   | __init__.py
|   ├──ex1.py
|   ├──ex2.py
├── easy

I again have to manually edit the debug config in launch.json file to: (considering the case for the sub-folder tough)

"module": "exercises.tough.${fileBaseNameNoExtension}"

I need to implement a general case where depending on the file being debugged, the "module" entry in the launch.json file should be:

"module": "folder1.folder2.folder3.....foldern.script"

Just like fileBaseNameNoExtension, VSCode has some other predefined variables:

One of the variables is relativeFile, which is the path of the current opened file relative to workspaceFolder So for the file ex1.py, the variable relativeFile will be exercises/tough/ex1.py.

I need to manipulate this string and convert this to exercises.tough.ex1, which is trivial if I can write and execute bash command inside the "module" entry in launch.json file. But I am unable to do that. However, the link predefined variables in VSCode has a section on Command variables, which states:

if the predefined variables from above are not sufficient, you can use any VS Code command as a variable through the ${command:commandID} syntax.

This link has a bunch of other information that may be helpful. I am no expert in python, and definitely don't know any javascript, if that is what is required to solve this problem.

解决方案

I can't reproduce your error: ModuleNotFoundError: no module named exercises.

Using the following files

exercises/extract_digits.py

def extract_digits(n):
  return 10

exercises/armstrong_number.py

from extract_digits import extract_digits

def armstrong_number(n):
  return extract_digits(n)

if __name__ == "__main__": 
  print armstrong_number(3)

If you use Python3 change the print statement to: print(armstrong_number(3)) and change the used Python interpreter.

If your current directory is the project root directory and you run

python exercises/armstrong_number.py

You get the number 10 in the console


In Visual Studio Code you use the wrong Launch configuration.

You are just running python programs so you should use the Python: Current File configuration. launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
    }
  ]
}

  1. Open the example py file you want to run/debug, in whatever directory it is.
  2. Or make it the current file
  3. Select the Debug Side Bar
  4. Select the Python: Current File configuration
  5. Click the "green" triangle.

Now a complicated command is constructed and run. It will set the CWD to the directory of the current file and start the debugger on the current file.


If you really need the module launch you can use a Multi-Root Workspace in VSC and have a separate configured launch.json for each of the root directories


If you want to use the ${command:commandID} syntax you can construct a simple extension that uses the activeTextEditor and construct a string based on the file name
Edit

Another option is to use Multiple Launch configurations.

I have removed the properties where the default value works or that are not used.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Exercise",
      "type": "python",
      "request": "launch",
      "console": "integratedTerminal",
      "module": "exercises.${fileBasenameNoExtension}",
    },
    {
      "name": "Exercise.Easy",
      "type": "python",
      "request": "launch",
      "console": "integratedTerminal",
      "module": "exercises.easy.${fileBasenameNoExtension}",
    },
    {
      "name": "DS",
      "type": "python",
      "request": "launch",
      "console": "integratedTerminal",
      "module": "ds.${fileBasenameNoExtension}",
    }
  ]
}


You can use the extension Command Variable to get this relative directory with dot separator.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Module CmdVar",
      "type": "python",
      "request": "launch",
      "console": "integratedTerminal",
      "module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
    }
  ]
}

这篇关于在 VSCode 中调试 python 模块的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆