![](/img/trans.png)
[英]How to configure Visual Studio Code in Windows 10 to disregard shebangs and use a Python interpreter path?
[英]in Windows 10, How to configure Visual Studio Code to find the Python 3 interpreter?
在 Windows 10 中,應如何配置 Visual Studio Code 以查找 Python 3 解釋器?
Visual Studio Code 無法找到 Python 3 解釋器。
我添加了 Python 擴展,Python 擴展位於https://marketplace.visualstudio.com/items?itemName=donjayamanne.python 。
應該如何更改settings.js
以使用 Python?
即使將settings.js
python.pythonPath
更改為導致python.exe
,它位於“C:\\Users<User>\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe”,如果我選擇Python: Select Workspace Interpreter
在命令面板中Python: Select Workspace Interpreter
,我收到消息Please open a workspace to select the Python Interpreter
。 為什么會這樣?
轉到File/Preferences/Settings
並找到Python Configuration
。 這是您手動更改配置的地方。
您還可以從command palette
選擇您的解釋器。 以下說明來自DonJayamanne 的 Github 。
選擇命令
Python: Select Workspace Interpreter
從命令面板中Python: Select Workspace Interpreter
選擇上述命令后,將在快速選擇列表中顯示發現的解釋器列表
從此列表中選擇一個解釋器將自動更新
settings.json
文件。
在打開 VS Code 的當前目錄中,轉到.vscode/settings.json
並修改 json 文件中的這一行:
{
"python.pythonPath": "path_to_your_python_bin",
# e.g., /home/myname/venv/bin/python3.7 or to a .exe file if you are on Windows
}
我也在 Windows 10 上的 Visual Studio Code 中使用此Python 擴展。Python 安裝在 C:\\Python27\\ 和 C:\\Python36\\ 中,並且兩個文件夾都添加到Windows PATH 變量中使用此設置 VS Code 應該能夠找到 Python 解釋器。 至少在我的環境中它運行良好。
但是 VS 使用它可以找到的第一個解釋器,在我的情況下是 3.6 版。 當我需要 2.7 用於項目時,我必須明確地告訴 VS Code 在配置文件settings.json
使用那個在 VS Code 中按Ctrl ,然后在右側窗口中添加:
{
"python.pythonPath": "c:/python27/python.exe"
}
或者你電腦上的 Python 解釋器的路徑是什么。
這也在Wiki 的 Manual Configuration 下進行了描述。 現在,使用此設置使用 Python 2.7。
不起作用的是 Python 版本的自動檢測,因此我也無法使用Select an Interpreter
命令Select an Interpreter
我需要的版本。
Please open a workspace to select the Python Interpreter.
錯誤很簡單。 我相信您在沒有選擇任何文件夾的情況下打開了 VS Code 窗口。 因此,未打開任何工作區,並且無法設置特定於工作區的設置。
如果您希望為所有工作區設置默認的 Python 解釋器,請使用Ctrl+Shift+P
Preferences: Open User Settings
, Preferences: Open User Settings
並搜索Python: Default Interpreter Path
。 否則,如果您只想為當前工作區設置它,請使用Preferences: Open Workspace Settings
。
在您的情況下,您希望將其設置為C:\\Users<User>\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe
。 雖然我建議使用虛擬環境。 如果您直接編輯settings.json
而不是使用 GUI:
{
"python.defaultInterpreterPath": "C:\Users<User>\AppData\Local\Programs\Python\Python36-32\python.exe"
}
詳細說明可以在文檔“手動指定解釋器”中找到,包括使用環境變量作為解釋器的路徑。
此外, "python.pythonPath"
已被棄用。 因此,大多數其他答案已過時:
2021.6.0(2021 年 6 月 16 日)
5. 在 pythonDeprecatePythonPath 實驗中,在工作區級別添加了 python.defaultInterpreterPath 設置。 (#16485)
8.在pythonDeprecatePythonPath實驗中顯示python.pythonPath棄用提示。 (#16485)
2020.7.0(2020 年 7 月 16 日)
9. 在 Deprecate PythonPath 實驗中,提示用戶我們已從他們的工作區設置中刪除了 pythonPath。 (#12533)
2020.5.0(2020 年 5 月 12 日)
6. 如果在 DeprecatePythonPath 實驗中,將 python.pythonPath 設置的現有值一次性傳輸到新的解釋器存儲。 (#11052)
8. 添加了在 Deprecate PythonPath 實驗中要求用戶從他們的工作區設置中刪除 python.pythonPath 鍵的提示。 (#11108)
12.將launch.json中用於引用設置中設置的解釋器路徑的字符串${config:python.pythonPath}重命名為${config:python.interpreterPath}。 (#11446)
2020.4.0(2020 年 4 月 20 日)
13. 新增用戶設置python.defaultInterpreterPath,用於在Deprecate PythonPath實驗中設置默認解釋器路徑。 (#11021)
我也有這個問題。 我做了@jps所說的,它仍然沒有用,但后來我改變了我的環境設置順序,使 Python36 在 Python27 之前,並且它起作用了。
就我而言,我已經看到您正在使用的實際工作區覆蓋了文件/首選項/設置 (Windows) 或代碼/首選項/設置 (Mac)。
解決方案是手動編輯工作區文件(通常在您的主目錄中)或創建一個新的工作區並為您的代碼重新添加您的目錄。
卸載 x64 版本。 並安裝 x86 32 位版本。 從 AV 中排除 python。 這應該可以解決問題。 Python 擴展。 確保每次都重新啟動 VSCode。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.