'ImportError: DLL load failed: The specified module could not be found - PyCharm

So I've been trying to switch to PyCharm this week and using some of my old code has not been working out. I am using the base anaconda environment as the interpreter, and the following code

import openpyxl
import numpy as np

And then some basic code on opening a sheet and creating arrays with the rows and columns. AFAIK this is an import problem related to a missing DLL, but nothing more than that. Here's the error:

Traceback (most recent call last):

File "C:\Users\nikol\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-a51a1395fac2>", line 1, in <module>
    runfile('C:/Users/nikol/Desktop/APO A1P2/Code/make_excel.py', wdir='C:/Users/nikol/Desktop/APO A1P2/Code')

File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script

File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)

  File "C:/Users/nikol/Desktop/APO A1P2/Code/make_excel.py", line 2, in <module>
    import numpy as np

  File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)

  File "C:\Users\nikol\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init

  File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)

  File "C:\Users\nikol\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from . import _mklinit

  File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)

ImportError: DLL load failed: The specified module could not be found.

Greatly appreciate any and all feedback! Thanks!



Solution 1:[1]

SOLVED!

Frustrating working with PyCharm, but here it is, step by step so a newbie like me can solve it later on. Note I am using Windows 10

Find your anaconda directory, for me it was:

C:\Users\<myusername>\Anaconda3\

THEN:

Right click my PC > Properties > Advanced system settings > Environment variables

Under System variables find the one named "Path" And in it, you have to ADD the following paths (note your anaconda directory may differ!!!):

C:\Users\<myusername>\Anaconda3\Scripts\
C:\Users\<myusername>\Anaconda3\Library\
C:\Users\<myusername>\Anaconda3\Library\bin\
C:\Users\<myusername>\Anaconda3\Library\mingw-w64\bin\

Click OK and restart PyCharm. Worked for me!

Solution 2:[2]

There is a bug in pycharm on windows that the program does not run as administrator. So if you create a project and pycharm is not runner as administrator, your project will not have access to your system files forever and this is an issue. Try to recreate your project and environment when you run pycharm as admin.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy
Solution 2 Hossein Babashah