11

When running a basic python program such as a single line of code:

import matplotlib.pyplot as plt

I get the response

"DLL load failed: The specified module could not be found"

I use Visual Studio Code on Windows 10 and am a beginner with Python. I run Python 3.7 and installed everything using Anaconda.

I have added various system environment variables as proposed in this answer. I have verified that I can run the code in the Anaconda Powershell Prompt so there seems to be some problem between Visual Studio Code and Anaconda upon install.

I have been reading this discussion which seems to relate to the problem but do not see that they offer a solution, merely that it is fixed.

----------EDIT---------

I was able to load the libraries in Spyder (see comments) so the issue is perhaps somehow related to Visual Studio Code.

fixingstuff
  • 559
  • 2
  • 7
  • 18
  • Can you run it in basic Windows terminal (cmd)? – cho_uc Jun 16 '19 at 20:17
  • Typing "python" in the command line gives the following response: "Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation" I have run the command: conda activate C:\Users\MyUSerName\Anaconda3 in the conda powershell to no avail – fixingstuff Jun 16 '19 at 20:45
  • @cho_uc my last comment was directed at you. I forgot to mention you in the comment – fixingstuff Jun 16 '19 at 21:08
  • @cho_uc and by the way, my folder named 'envs' in the Anaconda3 folder is empty. – fixingstuff Jun 16 '19 at 21:20
  • Your Windows cannot find the dll file. Usually adding various paths can solve this issue. You may need to recheck your path. But if it's done correctly, then I am also at a loss. – cho_uc Jun 16 '19 at 21:23
  • @cho_uc Just to make sure that I have done it correctly, I have added a system variable. Its Variable is set to PYTHON_HOME and its Value is set to C:\Users\MyUSerName\Anaconda3\ ...Correct? Then I ran the command: conda activate C:\Users\MyUSerName\Anaconda3 Also, my own code is outside of this folder, I assumed that was okey.. – fixingstuff Jun 16 '19 at 21:27
  • In the SO link that you mentioned, you have to specify the path up to the inner directory of Anaconda... '/Script', '/bin', /Library'. Have you done this? – cho_uc Jun 16 '19 at 21:36
  • @cho_uc I have added these different paths to my system variables. I have done nothing more than add them to my system variables. I cannot run conda activate on these paths becuase it returns a red text error: Invoke-Expression : Can not bind argument to parameter 'Command' because it is an empty string. – fixingstuff Jun 16 '19 at 21:44
  • What do you get when you run 'conda list'? – cho_uc Jun 16 '19 at 21:59
  • @cho_uc A list of all libraries (a lot) including ones I could not import in Visual Studio (but could import with Spyder). I think I am missing something essential in Visual Studio Code. Some setting or something – fixingstuff Jun 16 '19 at 22:32
  • Are you doing this from the base environment or a new conda environment? And is the activation command executing when you open the terminal? There are some [known issues](https://github.com/microsoft/vscode-python/issues/5344) with conda in VS Code that are being actively worked on. – Brett Cannon Jun 18 '19 at 00:18

2 Answers2

27

This issue happens when you run VS code standalone and not in the anaconda prompt. The more complicated solution is to add anaconda path to your system path and make VSCode understand the conda virtual environment. But the simpler solution is to open anaconda prompt and then type:

    (base) C:\Users\{your_user}>conda activate {your_env}
    ({your_env}) C:\Users\{your_user}>code

to open VSCode through anaconda. Hope it works.

Milad Sikaroudi
  • 677
  • 6
  • 16
6

In your case it seems there is a problem with activation itself. You can still run VSC as standalone, but you just need to add those two crucial lines in teminal's 'settings.json' file:

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe", 
"python.terminal.activateEnvironment": true

This will force env activation and worked for me pretty well. I will try to analyze this issue in the future. I hope this post could still be helpful as I did not find any fix nor satisfying explanation.

bartkej
  • 61
  • 1
  • 1