简体   繁体   English

Select Python 解释器在 VScode 中不起作用

[英]Select Python interpreter does not work in VScode

I have installed the first Python interpreter in my Windows PC and the path of python.exe is我已经在我的 Windows PC 上安装了第一个 Python 解释器,python.exe 的路径是

C:\Users\myname\AppData\Local\Programs\Python\Python38-32\python.exe

It worked well originally (running, debugging, etc...).它最初运行良好(运行、调试等...)。
Recently, I tried to install miniconda in my computer to build different Python environment and the path of python.exe is最近尝试在自己的电脑上安装miniconda来搭建不同的Python环境,其中python.exe的路径为

D:\miniconda\python.exe

I followed the tutorial on VScode office to select the conda environment I created.我按照VScode office上的教程到select我创建的conda环境。 And the Status Bar seems to be correct:状态栏似乎是正确的:

pic图片

However, if I run the following python code:但是,如果我运行以下 python 代码:

import sys
sys.executable

The output is: output 是:

C:\Users\myname\AppData\Local\Programs\Python\Python38-32\python.exe

which doesn't seem to be correct.这似乎不正确。

I have added both the two path of Python into the Path environment variable in my Windows settings.我已将 Python 的两个路径添加到我的 Windows 设置中的Path环境变量中。 How to fix this problem?如何解决这个问题?

I just reloaded the python extension which you will see when you go to the vscode and the "python extension" and in that the below "reload required" option will be there just click and then check the "python interpreter" in the "view" again it will resolve the current issue which you are facing.我刚刚重新加载了 python 扩展,当你将 go 重新加载到 vscode 和“python 扩展”时,你会看到它,并且下面的“需要重新加载”选项将在那里,只需单击然后检查“视图”中的“python 解释器”再次它将解决您面临的当前问题。

You can configure your VSCode workspace settings.您可以配置 VSCode 工作区设置。 Do you need to create a folder named .vscode/ at the source project with the settings.json file inside him.您是否需要在源项目中创建一个名为.vscode/的文件夹,其中包含settings.json文件。 The file content is how below:文件内容如下:

{
    "python.pythonPath": "path-to-your-venv/bin/python",
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "python.linting.lintOnSave": true,
    "python.linting.flake8Enabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintArgs": ["--load-plugins", "pylint_django"],
    "python.linting.enabled": true,
    "editor.rulers": [80],
    "editor.tabSize": 4,
    "prettier.singleQuote": true,
    "editor.defaultFormatter": "ms-python.python",
    "python.formatting.provider": "autopep8"
}
  

With your venv activated, do you need to install the libs autopep8 and flake8 with pip :激活venv后,是否需要使用pip安装 libs autopep8flake8

pip install autopep8
pip install flake8

Then, restart the VSCode.然后,重新启动 VSCode。

I hope help you.我希望能帮助你。

When VSCode did not let me select my Python interpreter, I added a defaultInterpreterPath to settings.json which I could select then. When VSCode did not let me select my Python interpreter, I added a defaultInterpreterPath to settings.json which I could select then.

  1. Open the settings.json of your workspace as explained in this SO post .本文所述,打开工作区的settings.json
  2. Then add "python.defaultInterpreterPath": "/path/to/your/interpreter/python.exe" to settings.json (as mentioned in the VSCode docs ).然后将"python.defaultInterpreterPath": "/path/to/your/interpreter/python.exe"添加到settings.json (如VSCode 文档中所述)。
  3. Then you can select this default interpreter .然后你可以select 这个默认解释器

Example settings.json :示例settings.json

{
    ... some settings here ... ,
    "python.defaultInterpreterPath": "c:/python39/python.exe"
}

Encountered the same problem.遇到了同样的问题。 Turns out I can't select interpreter at workspace level so I select it for the work folder and it works.结果我不能在工作区级别使用 select 解释器,所以我将 select 用于工作文件夹并且它可以工作。

  1. Press F1 to open the menu.按 F1 打开菜单。 Menu菜单
  2. Select Python:Select Interpreter. Select Python:Select 解释器。 If you don't see it, try to type it.如果您没有看到它,请尝试输入它。
  3. Select the first option, which is your work folder. Select 第一个选项,也就是你的工作文件夹。 List of selection选拔一览
  4. You will find a list of virtual environment and installed python.您将找到虚拟环境列表并安装 python。 Select the one you want. Select 你想要的那个。 List of python environment python环境列表

I had the same and it was because site-packages/sitecustomize.py (a script that runs before any other python code) was outputting something, which it isn't supposed to (my fault entirely).我也有同样的情况,这是因为site-packages/sitecustomize.py (在任何其他 python 代码之前运行的脚本)正在输出一些不应该的东西(完全是我的错)。 Simply deleting the file resolved the issue.只需删除文件即可解决问题。

to investigate similar problems I suggest looking at the vscode output, tab "Python", maybe that output gives you a hint.要调查类似的问题,我建议查看 vscode output,选项卡“Python”,也许 output 会给你一个提示。 For me it was something like对我来说,这就像

Failed to get interpreter information for "..." returned bad JSON

I had the same problem.我有同样的问题。 After selecting the interpreter the selected environment did not show in the status bar.选择解释器后,所选环境未显示在状态栏中。 It was still saying as 'Select Interpreter'.它仍然在说“选择口译员”。 A simple restart of VScode worked.简单重启 VScode 即可。

I had a similar issue because I edited settings.json before while trying to play with live server ( a vs code extension ) However the solution for me was to uninstall python the re-install it from the extensions section in vs code我有一个类似的问题,因为我在尝试使用实时服务器(一个 vs 代码扩展)之前编辑了settings.json但是我的解决方案卸载 python 然后从 vs 代码的扩展部分重新安装它

The reason for this, at least on Macs, is that the python/python3/python3.9 inside virtual environments is a symlink to the system interpreter in eg /opt/homebrew/bin/python3 and VSCode follows the symlink.至少在 Mac 上,这样做的原因是虚拟环境中的 python/python3/python3.9 是指向系统解释器的符号链接,例如/opt/homebrew/bin/python3并且 VSCode 遵循符号链接。

So the path for relative imports and all the packages in your virtual environment is now /opt/homebrew/bin instead of ./venv/bin , and so VSCode can't resolve any of the imports from your venv unless they happen to also be installed in /opt/homebrew/bin .因此,相对导入和虚拟环境中所有包的路径现在是/opt/homebrew/bin而不是./venv/bin ,因此 VSCode 无法解析来自你的 venv 的任何导入,除非它们恰好也是安装在/opt/homebrew/bin中。 This means you lose the "jump to definition" and similar functionality, "run this code" doesn't work, linters can't provide any kind of import-related feedback, etc.这意味着你失去了“跳转到定义”和类似的功能,“运行这段代码”不起作用,linters 不能提供任何类型的与导入相关的反馈,等等。

A solution that works is to copy the python binary into the venv, rather than use symlinks.一个可行的解决方案是将 python 二进制文件复制到 venv 中,而不是使用符号链接。 You can do this after you create the venv.您可以在创建 venv 后执行此操作。

python -m venv venv
rm venv/bin/python venv/bin/python3 venv/bin/python3.9
cp /opt/homebrew/bin/python3.9 venv/bin/
ln -s venv/bin/python3.9 venv/bin/python
ln -s venv/bin/python3.9 venv/bin/python3
source venv/bin/activate
pip install -r your_requirements_file.txt

Then set the python interpreter in VSCode to venv/bin/python3.9 and everything will work.然后将VSCode中的python解释器设置为venv/bin/python3.9就可以了。

This was reported but a fix didn't garner enough votes to be implemented.这已被报告,但修复没有获得足够的选票来实施。

https://github.com/microsoft/vscode-python/issues/13603 https://github.com/microsoft/vscode-python/issues/13603

Of course, change the paths and python versions in the code above as needed.当然,根据需要更改上面代码中的路径和python版本。

This happened on my windows PC.这发生在我的 windows PC 上。 Eventhough I have installed conda as the Virtual environment manager and created a bunch of virtual envs, VSCode didn't recognize any python interpreters.尽管我已经将 conda 安装为虚拟环境管理器并创建了一堆虚拟环境,但 VSCode 无法识别任何 python 解释器。

Short Answer: Install Extension: Python extension for Visual Studio Code简短回答:安装扩展: Python extension for Visual Studio Code

  • Go to Extensions on VSCode Go 到 VSCode 上的扩展
  • Install python extension for visual studio code.为 Visual Studio 代码安装 python 扩展。
  • Press CTRL + P select interpreter按 CTRL + P select 解释器

Now it should list out all the configured environments.现在它应该列出所有配置的环境。

The value of python in the terminal is entirely disconnected from what you select in VS Code as the terminal controls what is on PATH .终端中python的值与 VS 代码中的 select 完全断开,因为终端控制PATH上的内容。 You have two options:你有两个选择:

  1. Use a virtual environment so the Python extension can activate your terminal to make python point at what you want使用虚拟环境,因此 Python 扩展可以激活您的终端,使python指向您想要的
  2. Use the green Play button to run your code使用绿色的 Play 按钮运行您的代码
  3. Use the run/debug functionality of VS Code使用 VS Code 的运行/调试功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM