亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

VS 代碼 | Python程序 | 模塊導入過程未發生

VS 代碼 | Python程序 | 模塊導入過程未發生

墨色風雨 2022-11-09 16:55:02
環境作為多根工作區的一部分,我在 VS Code 中有一個 Python 快速入門。我手動添加了 3 個文件;tasks.json、settings.json和launch.json。意外行為當我調試項目時,主腳本由于缺少模塊而失敗。這是因為在啟動時未安裝requirements.txt中列出的模塊。任務.json{    "version": "2.0.0",    "tasks": [      {        "label": "pipInstall",        "type": "shell",        "osx": {          "command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"        },        "windows": {          "command": "${config:python.pythonPath}\\Scripts\\python -m pip install -r requirements.txt"        },        "linux": {          "command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"        },        "problemMatcher": [],        "options": {          "cwd": "${workspaceFolder}"        }      }    ]  }啟動.json    "version": "0.2.0",    "configurations": [        {            "name": "Python: Current File",            "type": "python",            "request": "launch",            "program": "${workspaceFolder}\\batch-python-quickstart\\src\\python_quickstart_client.py",            "console": "integratedTerminal"        }    ]}設置.json{    "python.pythonPath": "C:\\Python Virtual Environments\\myenvid\\.venv\\Scripts\\python.exe"}
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

為什么沒有安裝模塊

沒有安裝 requirements.txt 文件中指定的模塊,因為 launch.json 中沒有對tasks.json的引用。要在執行 python 應用程序之前執行任務,需要"preLaunchTask": "pipInstall"引用pipInstalltasks.json 中命名的任務。


修改后的代碼

注意:我還修復了一些不正確的路徑并將我的虛擬環境移動到我的項目之外的新目錄。


設置.json

{

    "python.pythonPath": "C:\\thepath\\.venv\\Scripts\\python.exe"

}

啟動.json

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "Python: Current File",

            "type": "python",

            "request": "launch",

            "program": "${workspaceFolder}\\batch-python-quickstart\\src\\python_quickstart_client.py",

            "console": "integratedTerminal",

            "preLaunchTask": "pipInstall"

        }

    ]

}

任務.json

{

    "version": "2.0.0",

    "tasks": [

      {

        "label": "pipInstall",

        "type": "shell",

        "osx": {

          "command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"

        },

        "windows": {

          "command": "${config:python.pythonPath} -m pip install -r requirements.txt"

        },

        "linux": {

          "command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"

        },

        "problemMatcher": [],

        "options": {

          "cwd": "${workspaceFolder}\\batch-python-quickstart\\src"

        }

      }

    ]

  }

此外

可以通過在任務中添加相同的依賴屬性來級聯任務;"dependsOn": "othertasklabel". 一組構建步驟的理想選擇。


查看完整回答
反對 回復 2022-11-09
  • 1 回答
  • 0 關注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號