VS Code 使用 Anaconda Python 编译出错的问题
问题描述
- 在VS Code中,使用 Anaconda Python 编译.py程序时报错,报错内容如下:
PS C:\Users\VikingsWu\Desktop\code space> & C:/Users/VikingsWu/anaconda3/python.exe "c:/Users/VikingsWu/Desktop/code space/standardplot.py"
C:\Users\VikingsWu\anaconda3\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
from . import multiarray
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
from . import overrides
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/VikingsWu/Desktop/code space/standardplot.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\matplotlib\__init__.py", line 107, in <module>
from . import cbook, rcsetup
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 28, in <module>
import numpy as np
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import core
File "C:\Users\VikingsWu\anaconda3\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "C:\Users\VikingsWu\anaconda3\python.exe"
* The NumPy version is: "1.19.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
- 查找很多资料,包括重新安装Numpy,均不能解决问题;注意,VS Code已经先执行了
conda activate base
;
问题解决
-
用Anaconda Prompt (anaconda3) 执行
import numpy
不报错,说明numpy没有问题;
-
在Power Shell中,执行
conda activate base
失败;提示需要配置shell;
-
执行
conda init powershell
命令,配置Power Shell;
-
重新打开Power Shell,提示如下信息:
. : 无法加载文件 C:\Users\VikingsWu\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信息,
请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\VikingsWu\Documents\WindowsPowerShell\profile.ps1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
-
参考博客:https://blog.csdn.net/l_x_cser/article/details/104956657 解决这一问题;
-
重新打开Power Shell,Power Shell自动进入base,运行python,执行
import numpy
不报错,说明问题已经解决,只需要关闭自动activate base; -
执行命令
conda config --set auto_activate_base false
,关闭自动activate_base; -
重新打开Power Shell,执行
conda activate base
成功, 运行python,执行import numpy
不报错,问题解决;
扫描二维码关注公众号,回复: 12834634 查看本文章