Error while finding module in VS code debugger

huangapple go评论63阅读模式
英文:

Error while finding module in VS code debugger

问题

以下是您要翻译的内容:

我尝试了不同的解决方案,以使我能够调试我的项目中的一个Python文件。
这是我的文件结构:

项目主文件夹

  • ns

  • 示例

以及示例和ns中的多个Python文件。基本上,所有示例文件都在使用ns或其他导入的模块。我还在vs code终端中已经激活了一个conda环境。以下是我的launch.json文件:

{
"version": "0.2.0",
"configurations": [
{
"name": "Python模块",
"type": "python",
"request": "launch",
"module": "project-main.examples",
}
]
}

我从运行菜单中选择“启动调试”,但我一直收到以下错误:

E+00000.063: 无法确定sys.argv的模块路径

回溯(最近一次调用最后):
文件“c:\Users\myuser.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py”,行296,在run_module
spec = find_spec(options.target)
文件“C:\Users\myuser\AppData\Local\Programs\Python\Python310\lib\importlib\util.py”,行94,查找规范
parent = import(parent_name, fromlist=['path'])
模块未找到错误:找不到名为'project-main'的模块。


文件“c:\Users\myuser.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy/..\debugpy\common\log.py”,行215,在swallow_exception
_exception(format_string, *args, **kwargs)
C:\Users\myuser\AppData\Local\Programs\Python\Python310\python.exe: 查找模块规范时出错,'project-main.examples'(模块未找到错误:找不到名为'project-main'的模块)

请注意,我只返回翻译好的部分,不包括代码。

英文:

I have tried different solutions to enable me to debug a python file in my project.
Here is my file structure:

project-main

-ns

-examples

and several Python files in examples and ns. Basically, all example files are using ns or other imported modules. I also have a conda environment that I have already activated in vs code terminal. And here is my launch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Module",
            "type": "python",
            "request": "launch",
            "module": "project-main.examples",
        }
    ]
}

I select Start debugging from the Run menu but I keep getting this error:

E+00000.063: Error determining module path for sys.argv

             Traceback (most recent call last):
               File "c:\Users\myuser\.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 296, in run_module
                 spec = find_spec(options.target)
               File "C:\Users\myuser\AppData\Local\Programs\Python\Python310\lib\importlib\util.py", line 94, in find_spec
                 parent = __import__(parent_name, fromlist=['__path__'])
             ModuleNotFoundError: No module named 'project-main'

.
.
.

       File "c:\Users\myuser\.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy/..\debugpy\common\log.py", line 215, in swallow_exception
         _exception(format_string, *args, **kwargs)
C:\Users\myuser\AppData\Local\Programs\Python\Python310\python.exe: Error while finding module specification for 'project-main.examples' (ModuleNotFoundError: No module named 'project-main')

答案1

得分: 0

根据您提供的信息,我提供一个可能的答案。如果您的模块 project-main 位于工作区下,您可以尝试以下方法。

如果您没有设置 cwd,默认将是文件的根目录。在调试过程中,无法找到模块。您可以在 launch.json 文件中添加以下代码来设置 cwd

"cwd": "${workspaceFolder}";
英文:

Based on the information you provided, I provide a possible answer. If your module project-main is located under the workspace, you can try the following way.

If you didn't set cwd, default would be the root directory of the file. During the debugging process, the module cannot be found. You can set cwd in the launch.json file by add the following codes:

"cwd":"${workspaceFolder}"

答案2

得分: 0

这是正确的配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "examples.run_this",
            "justMyCode": true
        }
    ]
}

基本上,对于"module"字段,我必须输入包含主运行文件(run_this)的子文件夹的名称,然后是运行文件的名称。

英文:

Here is the correct config:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "examples.run_this",
            "justMyCode": true
        }
    ]
}

so basically for the module field, I have to enter the name of the subfolder that includes the main running file (run_this) and then the name of the running file.

huangapple
  • 本文由 发表于 2023年6月1日 06:36:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定