我嘗試了“<”和“>”命令,但 vsc 仍然使用終端。
我的launch.json:
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["<", "${workspaceFolder}input.txt"]
這導致在 vsc 的終端上:
(.venv) PS C:\Users\domip\Desktop\Python> c:; cd 'c:\Users\domip\Desktop\Python'; & 'c:\Users\domip\Desktop\Python\.venv\Scripts\python.exe' 'c:\Users\domip\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher' '63669' '--' 'c:\Users\domip\Desktop\Python\ea2\fizzbuzz.py' '<' 'C:\Users\domip\Desktop\Pythoninput.txt'
但是,在除錯時它仍然使用終端來請求輸入。如果我使用">", "output.txt". 輸出寫在終端上,而不是在 txt 檔案中。我嘗試手動運行它,它的行為方式相同。
我是python的新手。為了測驗,我使用該input()功能。我嘗試使用一個main()函式,也許需要將 args 傳遞給它,但沒有效果。當我在 C 中(在 vsc 中)編碼時,我對此沒有任何問題。我已經嘗試了我在互聯網上找到的一切。沒有任何幫助。謝謝
uj5u.com熱心網友回復:
正如您所發現的<,ms-python 擴展很好地將其放在引號內,因此外殼不會將其視為重定向。
它可以完成,但需要一些配置。
您必須自己設定除錯客戶端和服務器。
除錯服務器被設定為一項任務。客戶端是附加啟動配置。
首先,您需要找到作為debugpyms-python 擴展的一部分的模塊的位置(您可以在您的環境中作為單獨的模塊安裝,但如果您已經擁有它,為什么會這樣)。
我使用以下2個檔案:
redir_input.py
while True:
try:
line = input()
except EOFError:
break
print(line)
input.txt
line 1
line 2
line 3
使用redir_input.py檔案啟動除錯會話(從終端獲取輸入)。終止程式Ctrl Z Enter。
我們想要的是終端上顯示的命令。就像是:
<path>/.venv/Scripts/python <path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/launcher 64141 -- <path>/redir_input.py
我們需要第二個字串:debugpy模塊的位置(不含/launcher部件)
<path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy
<path> 是特定于您的機器的東西。
在.vscode/tasks.json創建一個將啟動程式進行除錯的任務中:
{
"version": "2.0.0",
"tasks": [
{
"label": "Script with redirect input",
"type": "shell",
"command": "${command:python.interpreterPath} <path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy
--listen 5678 --wait-for-client ${workspaceFolder}/redir_input.py < ${workspaceFolder}/input.txt",
"problemMatcher": []
}
]
}
現在設定一個復合啟動配置,一個啟動除錯服務器,一個連接除錯客戶端。除錯服務器啟動一個虛擬的 python 腳本,因為我們實際上需要prelaunchTask.
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach to Process port 5678",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
},
{
"name": "Python: Start Process port 5678",
"type": "python",
"request": "launch",
"code": "print()",
"preLaunchTask": "Script with redirect input"
}
],
"compounds": [
{
"name": "Debug with input redir",
"configurations": [
"Python: Start Process port 5678",
"Python: Attach to Process port 5678"
]
}
]
}
選擇Debug with input redir作為要運行的啟動配置(除錯欄頂部),然后按 Run 按鈕(小綠色箭頭)或F5
除錯器一直等到客戶端附加 ( --wait-for-client),很可能您想在讀取行時設定斷點。
將創建 2 個終端。在程式/任務運行時選擇正確的一個。
您可以更改任務的名稱并啟動配置。請務必更新多個位置的名稱。
uj5u.com熱心網友回復:
謝謝@rioV8 的回復!我已經嘗試了您的解決方案,但遇到了一些問題。我的檔案:launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach to Process port 5678",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
},
{
"name": "Python: Start Process port 5678",
"type": "python",
"request": "launch",
"code": "print()",
"preLaunchTask": "Script with redirect input"
}
],
"compounds": [
{
"name": "Debug with input redir",
"configurations": [
"Python: Start Process port 5678",
"Python: Attach to Process port 5678"
]
}
]
}
任務檔案
{
"version": "2.0.0",
"tasks": [
{
"label": "Script with redirect input",
"type": "shell",
"command": "${command:python.interpreterPath} c:/User/domip/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy --listen 5678 --wait-for-client ${workspaceFolder}/redir_input.py < ${workspaceFolder}/input.txt",
"problemMatcher": []
}
]
}
當我使用Debug with input redir或使用除錯時,Python: Attach to Process port 5678出現以下錯誤:connect ECONNREFUSED 127.0.0.1:5678和The preLaunchTask 'Script with redirect input' terminated with exit code 1.. 當我使用時,Python: Start Process port 5678我只會得到后者。(退出代碼 1)知道如何解決這個問題嗎?謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/393754.html
上一篇:將引數url改寫為普通url但重定向后應該得到原始url引數
下一篇:Java除錯器卡在vscode中
