【发布时间】:2019-08-07 10:49:50
【问题描述】:
我的python练习项目有以下目录结构:
.
├── data
├── ds-and-algo
├── exercises
| ├── __init__.py
│ ├── armstrong_number.py
│ ├── extract_digits.py
├── output
extract_digits.py 看起来像这样:
def extract_digits(n):
pass
在armstrong_number.py 我有以下内容:
from .extract_digits import extract_digits
如果我运行,则从根项目目录
python exercises/armstrong_number.py
我收到ModuleNotFoundError: no module named exercises
使用-m 标志运行以下逗号可解决错误:
python -m exercises.armstrong_number
但是使用VSCode 来调试文件,我有以下调试配置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}"}
}
]
}
但是这有一些问题:
1) 对于不同的文件夹,例如ds-and-algo,我得手动编辑launch.json文件调试配置中的模块入口为
"module" : "ds-and-algo.${fileBaseNameNoExtension}"
如果我有嵌套文件夹配置,例如:
exercises
├── tough
| | __init__.py
| ├──ex1.py
| ├──ex2.py
├── easy
我再次必须手动将launch.json 文件中的调试配置编辑为:(考虑子文件夹tough 的情况)
"module": "exercises.tough.${fileBaseNameNoExtension}"
我需要实现一个一般情况,根据被调试的文件,launch.json 文件中的"module" 条目应该是:
"module": "folder1.folder2.folder3.....foldern.script"
就像fileBaseNameNoExtension,VSCode 有some other predefined variables:
其中一个变量是relativeFile,即当前打开的文件相对于workspaceFolder的路径
因此对于文件ex1.py,变量relativeFile 将是exercises/tough/ex1.py。
我需要操作此字符串并将其转换为exercises.tough.ex1,如果我可以在launch.json 文件的"module" 条目中编写和执行bash 命令,这将是微不足道的。但我无法做到这一点。但是,链接predefined variables in VSCode 有一个关于命令变量的部分,其中指出:
如果上面的预定义变量不够用,您可以通过 ${command:commandID} 语法使用任何 VS Code 命令作为变量。
此链接包含许多其他可能有用的信息。我不是 python 专家,绝对不知道任何 javascript,如果这是解决这个问题所需要的。
【问题讨论】: