簡體   English   中英

如何使用 Visual Studio Code 從不同的驅動器運行 python

[英]How to run python from different drive using Visual Studio Code

我的操作系統是 Windows 10。

我在 E 盤中有 test.py 文件。

我在 C 驅動器中安裝了 Python 3.7.2 和 Visual Studio Code。

我為 Python 3.7.2 設置了系統路徑,因此我可以在兩個驅動器上的 CMD 上運行 python。

在此處輸入圖像描述

我還在我的工作文件夾下的.vscode中的setting.json文件中設置了python.pythonPath。

所以 python.pythonPath 與系統路徑相同。

但我無法在 VScode 上運行該 test.py 文件。

它顯示 python 3.7.2 文件的路徑並顯示“沒有這樣的文件或目錄”。

有沒有辦法使用 VScode 在不同的驅動器上運行 python 文件?

如果您已經將 Python 設置為環境變量,您可以簡單地路由到文件所在的目錄(在本例中為 E:驅動器)並運行命令:

python test.py

您可以使用調試以可重用的方式運行特定文件。

https://code.visualstudio.com/docs/python/debugging

它將創建一個launch.json 文件,其內容與此類似。 您可以添加額外的
特性。 VSCode 擅長幫助您創建和編輯 launch.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": "RunPython",
            "type": "python",
            "request": "launch",
            // run a file by specifying a full path
            "program": "E:/FullPathTo/FileA.py",
            // OR run the file currently open in VSCode
            // // "program": "${file}",
            // OR run a file relative to the current folder you have open
            // // "program": "${workspaceFolder}/folder/FileB.py"
            "console": "integratedTerminal",
            "args": ['InputArg1','InputArg2']
        }
    ]
}

我安裝了 Code Runner Extension,它自動遵循了我的系統路徑 python 3.7.2。 現在我可以簡單地使用這個快捷方式“Crtl+Alt+N”運行我的代碼,而不是每次在終端上輸入“python test.py”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM