如何正确设置特定模块以在 VS 代码中进行调试?

How to correctly set specific module to debug in VS code?

我一直在关注 instruction by VS code's website,但似乎我尝试的任何方法都不起作用。

我根据需要创建了一个新配置,但是每当我放置路径时,它拒绝在 VS 代码中工作,尽管 VS 代码在集成终端 window 中抱怨的路径在我手动调用它时工作正常。

调试器抛出的错误如下:

(automl-meta-learning) brandomiranda~/automl-meta-learning/automl/experiments ❯ env PTVSD_LAUNCHER_PORT=59729 /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher -m /Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization.py 
E+00000.025: Error determining module path for sys.argv

             Traceback (most recent call last):
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 220, in run_module
                 spec = find_spec(options.target)
               File "/Users/brandomiranda/miniconda3/envs/automl-meta-learning/lib/python3.7/importlib/util.py", line 94, in find_spec
                 parent = __import__(parent_name, fromlist=['__path__'])
             ModuleNotFoundError: No module named '/Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization'

             Stack where logged:
               File "/Users/brandomiranda/miniconda3/envs/automl-meta-learning/lib/python3.7/runpy.py", line 193, in _run_module_as_main
                 "__main__", mod_spec)
               File "/Users/brandomiranda/miniconda3/envs/automl-meta-learning/lib/python3.7/runpy.py", line 85, in _run_code
                 exec(code, run_globals)
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/__main__.py", line 45, in <module>
                 cli.main()
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 361, in main
                 run()
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 226, in run_module
                 log.exception("Error determining module path for sys.argv")


/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python: Error while finding module specification for '/Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization.py' (ModuleNotFoundError: No module named '/Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization')

然后我尝试 运行手动处理它抱怨的文件,它 运行 很好...

(automl-meta-learning) brandomiranda~/automl-meta-learning/automl/experiments ❯ python /Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization.py
--> main in differentiable SGD
-------> Inside Experiment Code <--------

---> hostname:

device = cpu
Files already downloaded and verified
Files already downloaded and verified
Files already downloaded and verified

即使我将鼠标悬停在路径名称上并使用 command + click 单击它,它也会将我带到 VS 代码中的路径。这看起来很奇怪。所以不知何故只有当我 运行 它处于调试器模式时它才不起作用。为什么?


Launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: Experiments Protype1",
            "type": "python",
            "request": "launch",
            "module": "${workspaceFolder}/automl/experiments/experiments_model_optimization.py" // ~/automl-meta-learning/automl/experiments/experiments_model_optimization.py
        },
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name-here",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
        },
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}

交叉发布:

您在 launch.json 中使用 module 而不是 program。使用模块时,您必须仅传递 module\sub-module 名称,而不是整个路径。 Visual Studio 然后将加载指定的模块并执行它的 __main__.py 文件。

这将是正确的输入,假设 automl 是一个模块而 experiments 是一个子模块:

"module": "automl.experiments"

如果您想直接指向您的脚本,您可以使用您之前使用的路径,只需将 module 更改为 program:

"program": "${workspaceFolder}/automl/experiments/experiments_model_optimization.py"

这就是我所做的。通过转到左侧的调试器选项卡打开 launch.json 文件后:

然后我点击了Add configuration然后打开了launch.json文件。然后在右下角有一个带有 Add Configuration:

的蓝色按钮

然后我填写了选择Python file后出现的stex。出现的文字是:

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }

我把它改成:

        {
            "name": "Python: My Trainable",
            "type": "python",
            "request": "launch",
            "program": "/Users/brandomiranda/automl-meta-learning/prototyping_tests_playground/trainable_optimizers/my_trainable.py",
            "console": "integratedTerminal"
        },

然后我确定我在左侧调试菜单中选择了它,这样每次我 运行 使用 F5 快捷方式将它 运行 正确的文件无论我在哪里在 VS 代码中。


当前文件:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: My Trainable Step Size Higher",
            "type": "python",
            "request": "launch",
            "program": "/Users/brandomiranda/automl-meta-learning/prototyping_tests_playground/trainable_optimizers/trainable_step_size.py",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Experiments Protype1",
            "type": "python",
            "request": "launch",
            "program": "${env:HOME}/automl-meta-learning/automl/experiments/experiments_model_optimization.py",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name-here",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
        },
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}

似乎 ${end:HOME} 对于回家的路很重要。