python - 如何正确设置特定模块以在 VS 代码中进行调试?
问题描述
我按照VS code 网站的说明进行操作,但似乎我尝试过的任何方法都不起作用。
我根据需要创建了一个新配置,但是每当我放置路径时它拒绝在 VS 代码中工作,尽管路径 VS 代码在集成终端窗口中抱怨当我手动调用它时工作正常。
调试器抛出的错误如下:
(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 代码中的路径。这似乎很奇怪。所以不知何故,只有当我在调试器模式下运行它时它才不起作用。为什么?
启动.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"
}
]
}
交叉发布:
解决方案
您正在使用module
而不是program
in launch.json
。使用模块时,您必须只传递模块\子模块名称,而不是整个路径。Visual Studio 然后将加载指定的模块并执行它的__main__.py
文件。
这将是正确的输入,假设 automl 是一个模块并且 Experiments 是一个子模块:
"module": "automl.experiments"
如果您想直接指向您的脚本,您可以使用之前使用的路径,只需更改module
为program
:
"program": "${workspaceFolder}/automl/experiments/experiments_model_optimization.py"
推荐阅读
- php - 如何在 $variable 中获取 url 的最后一部分
- excel - Excel VBA graphQL http 请求“运行时错误 -2147012744 服务器返回了无效或无法识别的响应”
- docker - 将映像从 Azure 容器注册表部署到 Azure Linux 虚拟机
- ruby-on-rails - Rails 控制器过滤器在 Ruby 中实现了哪些语言功能?
- c# - 覆盖另一个 MVC 项目的 API 控制器
- r - 循环遍历 R 中的数据帧行以根据条件创建向量不起作用
- https - 使用 https 的 Service Fabric 反向代理
- javascript - ReactJS:我们可以在单个文本字段中有两个 onChange 吗?
- spring-boot - Hibernate - 更改 DTYPE 的最佳方法是什么?
- c++ - TTF_SizeText() 未在 SDL2、C++ 中返回“i”、“j”和“1”(迄今为止发现)的正确值