简体   繁体   中英

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:

在此处输入图像描述

It seems like Code Runner is using the non-conda Python, where I don't have the numpy package installed. The interpreter is set to conda. How do I get Code Runner to use conda's Python interpreter? Will adding conda to PATH help?

Alternatively, does anyone have a code runner extension that works better with 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. 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. Of course, everything is normal when you select run python file in the upper right corner to run the code (it will change accordingly).

So please make sure that the numpy package has been installed in your current environment. You can use pip show numpy in the terminal to see if the numpy package is installed and where it is installed.

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:

在此处输入图像描述

Select the conda interpreter, and the results of Run Python File and Run Code are the same:

在此处输入图像描述

Run Python File :

在此处输入图像描述

Run Code :

在此处输入图像描述

Select another python interpreter, then Run Python File switches the interpreter to run and reports an error:

在此处输入图像描述

Run Code still uses the conda interpreter and runs normally:

在此处输入图像描述

Uninstall the numpy package, no matter which interpreter reports an error:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Click Save Workspace As... on the File menu (top-left) to create the *.code-workspace file.

Now when you open vscode it will prioritize the settings on this file over the default Json settings. Apply those settings (if you are using Code Runner extension) and it will work.

"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",
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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