Abh*_*tia 1 python visual-studio-code
我的python练习项目有以下目录结构:
.
??? data
??? ds-and-algo
??? exercises
| ??? __init__.py
? ??? armstrong_number.py
? ??? extract_digits.py
??? output
Run Code Online (Sandbox Code Playgroud)
本extract_digits.py看起来是这样的:
.
??? data
??? ds-and-algo
??? exercises
| ??? __init__.py
? ??? armstrong_number.py
? ??? extract_digits.py
??? output
Run Code Online (Sandbox Code Playgroud)
在armstrong_number.py我有以下内容:
def extract_digits(n):
pass
Run Code Online (Sandbox Code Playgroud)
如果我运行,则从根项目目录
from .extract_digits import extract_digits
Run Code Online (Sandbox Code Playgroud)
我得到 ModuleNotFoundError: no module named exercises
运行以下带有-m标志的命令可解决错误:
python exercises/armstrong_number.py
Run Code Online (Sandbox Code Playgroud)
但是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}"}
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是这有几个问题:
1)对于不同的文件夹,例如ds-and-algo,我必须手动编辑launch.json文件调试配置中的模块条目
"module" : "ds-and-algo.${fileBaseNameNoExtension}"
Run Code Online (Sandbox Code Playgroud)
如果我有嵌套文件夹配置,例如:
exercises
??? tough
| | __init__.py
| ???ex1.py
| ???ex2.py
??? easy
Run Code Online (Sandbox Code Playgroud)
我再次必须手动编辑launch.json文件中的调试配置:(考虑子文件夹的情况tough)
"module": "exercises.tough.${fileBaseNameNoExtension}"
Run Code Online (Sandbox Code Playgroud)
我需要实现一个一般情况,根据被调试的文件,文件中的"module"条目launch.json应该是:
"module": "folder1.folder2.folder3.....foldern.script"
就像fileBaseNameNoExtension,VSCode还有一些其他预定义的变量:
其中一个变量是relativeFile,它是当前打开的文件相对于 的路径,workspaceFolder
所以对于文件ex1.py,变量relativeFile将是exercises/tough/ex1.py.
我需要操作这个字符串并将其转换为exercises.tough.ex1,如果我可以"module"在launch.json文件中的条目中编写和执行 bash 命令,这是微不足道的。但我无法做到这一点。但是,VSCode 中的链接预定义变量有一个关于Command variables的部分,其中指出:
如果上面的预定义变量不够用,您可以通过${command:commandID}语法使用任何 VS Code 命令作为变量。
此链接包含许多可能有用的其他信息。我不是 python 专家,绝对不知道任何 javascript,如果这是解决这个问题所需要的。
我无法重现您的错误:ModuleNotFoundError: no module named exercises。
使用以下文件
exercises/extract_digits.py
def extract_digits(n):
return 10
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
如果您使用 Python3,请将打印语句更改为:print(armstrong_number(3))并更改使用的 Python 解释器。
如果您的当前目录是项目根目录并且您运行
python exercises/armstrong_number.py
Run Code Online (Sandbox Code Playgroud)
你在控制台中得到数字 10
你只是在运行 python 程序,所以你应该使用Python: Current File配置。
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
Run Code Online (Sandbox Code Playgroud)
Python: Current File配置现在构建并运行一个复杂的命令。它会将 CWD 设置为当前文件的目录并在当前文件上启动调试器。
Multi-Root Workspace在 VSC 中使用 a并launch.json为每个根目录单独配置
${command:commandID}语法,您可以构造一个简单的扩展名,该扩展名使用activeTextEditor并根据文件名构造一个字符串
另一种选择是使用多启动配置。
我已经删除了默认值有效或未使用的属性。
{
"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}",
}
]
}
Run Code Online (Sandbox Code Playgroud)
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module CmdVar",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3498 次 |
| 最近记录: |