我将anaconda用作python包管理器,将Visual Studio Code用作python IDE。
虽然我可以从VS代码中查看和选择虚拟环境,但是从VS代码执行时却无法加载matplotlib.pyplot
。
我创建了一个test
虚拟环境,并使用conda install matplotlib
安装了matplotlib。
我已明确选择此conda test
环境作为VS Code中的Python解释器,如以下屏幕快照所示:
从VS代码运行此python文件:
import matplotlib.pyplot as plt
print("Hello world")
失败,并出现以下错误:
PS Q:\Code\Tests> & 'C:\Users\louen\.conda\envs\test\python.exe' 'c:\Users\louen\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '50055' 'q:\Code\Tests\hello.py'
Traceback (most recent call last):
File "c:\Users\louen\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\louen\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\louen\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\louen\.conda\envs\test\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\louen\.conda\envs\test\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\louen\.conda\envs\test\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "q:\Code\Tests\hello.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\louen\.conda\envs\test\lib\site-packages\matplotlib\pyplot.py", line 32, in <module>
import matplotlib.colorbar
File "C:\Users\louen\.conda\envs\test\lib\site-packages\matplotlib\colorbar.py", line 31, in <module>
import matplotlib.contour as contour
File "C:\Users\louen\.conda\envs\test\lib\site-packages\matplotlib\contour.py", line 16, in <module>
import matplotlib.font_manager as font_manager
File "C:\Users\louen\.conda\envs\test\lib\site-packages\matplotlib\font_manager.py", line 40, in <module>
from matplotlib import afm, cbook, ft2font, rcParams
ImportError: DLL load failed: The specified module could not be found.
但是,从anaconda命令提示符运行相同的文件是可行的:
(test) C:\Users\louen>python Q:\Code\Tests\hello.py
Hello World
此外,仅导入matplotlib
也不会触发错误。
这是conda info
(test) C:\Users\louen>conda info
active environment : test
active env location : C:\Users\louen\.conda\envs\test
shell level : 3
user config file : C:\Users\louen\.condarc
populated config files :
conda version : 4.6.14
conda-build version : 3.10.5
python version : 3.6.5.final.0
base environment : C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64 (read only)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/win-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\pkgs
C:\Users\louen\.conda\pkgs
C:\Users\louen\AppData\Local\conda\conda\pkgs
envs directories : C:\Users\louen\.conda\envs
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\envs
C:\Users\louen\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/4.6.14 requests/2.18.4 CPython/3.6.5 Windows/10 Windows/10.0.17134
administrator : False
netrc file : None
offline mode : False
此错误来自何处? VS代码执行环境和conda环境之间有区别吗?
答案 0 :(得分:1)
如果您在调试器运行时在输出中注意到,终端没有及时完成conda激活,因此Python找不到您的conda安装包。这是我们试图在扩展程序中修复的conda issues的更大集合的一部分,但是由于conda和VS Code的操作方式,这令人惊讶地困难。
答案 1 :(得分:0)
我通过在 vscode 中将默认 shell 可执行文件设置为“C:/Windows/System32/cmd.exe”来完成这项工作
步骤: