Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"DLL load failed" in Visual Studio Code when trying to open any Python library on Windows 10

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.

like image 646
fixingstuff Avatar asked Jun 16 '19 19:06

fixingstuff


People also ask

Why is it showing DLL error in Python?

The Error Loading Python DLL error typically occurs when the user tries to open the desktop version of Google Drive or several seconds after starting the syncing procedure. In most cases, the error window points towards a Dynamic Link Library (DLL) named python27. dll.

How do you fix Importerror DLL load failed the specified module could not be found?

importerror: dll load failed: The specified module could not be found error occurs because of the incompatibilities of Microsoft Visual C++ (Visual Studio) versions. The best way to fix this error (importerror: dll load failed) is to reinstall/ install the Microsoft Visual C++ distribution.

Can you install Python libraries in Visual Studio Code?

Visual Studio Code and the Python extension# Once VS Code is installed, you can install the Python extension for VS Code from the Visual Studio Marketplace. For additional details about installing extensions, see Extension Marketplace. The Python extension is named Python and is published by Microsoft.

How to fix import error DLL load failed in Python?

Please refer to the following steps: 1 Step 1: Identify the library that causes the error "ImportError: Dll Load Failed: The specified module could not be... 2 Step 2: You can use the following commands to uninstall and install a library (numpy is the name of a Python library). More ...

Why is my DLL file not loading?

Top Reasons for Failed to Load DLL. Why does the unable to load DLL/failed to load DLL error occur? We summarize these causes as follows: The DLL file is not in a directory specified in the path. The referenced DLL file is missing. The DLL file is damaged due to a hard disk error. The DLL file is not executable.

Why can’t I Find my DLL file?

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

What is Python environment in Visual Studio Code?

Using Python environments in VS Code An "environment" in Python is the context in which a Python program runs. An environment consists of an interpreter and any number of installed packages. The Python extension for VS Code provides helpful integration features for working with different environments.


2 Answers

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.

like image 142
Milad Sikaroudi Avatar answered Oct 23 '22 09:10

Milad Sikaroudi


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.

like image 5
bartkej Avatar answered Oct 23 '22 09:10

bartkej