简体   繁体   English

VS Code 中的 Code Runner 未运行 Conda Python

[英]Code Runner in VS Code Not Running Conda Python

I've installed conda and want Code Runner's output to use the conda installation of Python. When I run this code, this is the output I get:我已经安装了 conda 并希望 Code Runner 的 output 使用 Python 的 conda 安装。当我运行这段代码时,这是我得到的 output:

在此处输入图像描述

It seems like Code Runner is using the non-conda Python, where I don't have the numpy package installed.似乎 Code Runner 使用的是非 conda Python,我没有安装 numpy package。 The interpreter is set to conda.解释器设置为 conda。 How do I get Code Runner to use conda's Python interpreter?如何让 Code Runner 使用 conda 的 Python 解释器? Will adding conda to PATH help?将 conda 添加到 PATH 有帮助吗?

Alternatively, does anyone have a code runner extension that works better with conda?或者,有没有人有一个代码运行器扩展可以更好地与 conda 一起工作? Thank you so much!太感谢了!

I tested it many times and found that Code Runner doesn't seem to follow the interpreter in the lower right corner.我测试了很多次,发现Code Runner似乎没有跟随右下角的解释器。 It picks the interpreter in your current environment, which seems to be the problem.它会在您当前的环境中选择解释器,这似乎是问题所在。

For example, if I use the conda environment now, no matter what interpreter I choose in the lower right corner, when I use Code Runner to run the code, it will use the interpreter in the conda environment, and it will not change with the lower right corner.比如我现在使用conda环境,无论我在右下角选择什么解释器,当我使用Code Runner运行代码时,都会使用conda环境中的解释器,不会随着右下角。 Of course, everything is normal when you select run python file in the upper right corner to run the code (it will change accordingly).当然,当你选择右上角的run python file运行代码时一切正常(它会相应改变)。

So please make sure that the numpy package has been installed in your current environment.所以请确保numpy包已经安装在你当前的环境中。 You can use pip show numpy in the terminal to see if the numpy package is installed and where it is installed.您可以在终端中使用pip show numpy来查看是否安装了numpy包以及安装在哪里。

The following simple example demonstrates:以下简单示例演示:

python code:蟒蛇代码:

import sys
import numpy as np

print('Current Python interpreter path: ' + sys.executable)

print("hello world")

a = np.arange(1,10,1)
print(a)

The numpy package is installed in my conda environment: numpy包安装在我的conda环境中:

在此处输入图像描述

Select the conda interpreter, and the results of Run Python File and Run Code are the same:选择conda解释器, Run Python FileRun Code的结果是一样的:

在此处输入图像描述

Run Python File : Run Python File

在此处输入图像描述

Run Code : Run Code

在此处输入图像描述

Select another python interpreter, then Run Python File switches the interpreter to run and reports an error:选择另一个python解释器,然后Run Python File切换解释器运行,报错:

在此处输入图像描述

Run Code still uses the conda interpreter and runs normally: Run Code仍然使用 conda 解释器并正常运行:

在此处输入图像描述

Uninstall the numpy package, no matter which interpreter reports an error:卸载numpy包,不管哪个解释器报错:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Click Save Workspace As... on the File menu (top-left) to create the *.code-workspace file.单击“文件”菜单(左上角)上的“ Save Workspace As... ”以创建*.code-workspace文件。

Now when you open vscode it will prioritize the settings on this file over the default Json settings.现在,当您打开vscode时,它会将此文件的设置优先于默认的 Json 设置。 Apply those settings (if you are using Code Runner extension) and it will work.应用这些设置(如果您使用的是 Code Runner 扩展),它将起作用。

"settings": {
        "code-runner.executorMap": {
            "python": "C:\\Users\\<Your anaconda environment full path>\\python.exe"
        },
        "python.defaultInterpreterPath": "C:\\Users\\<Your anaconda environment full path>\\python.exe",
    }

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

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