2

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 : 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 ?

2
  • 1
    Looks the same as this issue which is reported to be fixed without giving any details. Various versions of fixes to this error are shown here, not clear which one would apply for you but you may test some. Commented Aug 14, 2019 at 1:27
  • I worked around it by using a different python IDE (pyCharm in that case) Commented Aug 15, 2019 at 21:43

2 Answers 2

2

If you notice in your output when the debugger ran, the terminal didn't get its conda activation completed in time, and so Python couldn't find your conda-installed packages. This is part of a larger set of conda issues we are trying to fix in the extension, but it's surprisingly hard due to how conda and VS Code operate.

Sign up to request clarification or add additional context in comments.

Comments

0

I got this work by setting the default shell executable to "C:/Windows/System32/cmd.exe" in vscode

Steps:

  1. Open vscode Settings
  2. Type "powershell" in filter
  3. Find "Edit in settings.json" and click it
  4. Set "terminal.integrated.shell.windows": "C:/Windows/System32/cmd.exe"
  5. Save settings.json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.