I'm using anaconda as my python package manager and Visual Studio Code as my python IDE.
While I'm able to see and select the virtual environment from VS code, I'm unable to load matplotlib.pyplot when executing from VS code.
I created a test virtual environment and installed matplotlib with conda install matplotlib.
I have explicitly selected this conda test environment as my Python interpreter in VS Code as shown in this screenshot :

Running this python file from VS code :
import matplotlib.pyplot as plt
print("Hello world")
Fails with the following error :
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.
However, running the same file from the anaconda command prompt works:
(test) C:\Users\louen>python Q:\Code\Tests\hello.py
Hello World
Also, importing only matplotlib does not trigger the error either.
Here is the output from 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
Where does this error comes from ? Is there a difference between the VS code execution environment and the conda environment ?