pythonpipanacondanlopt

ModuleNotFoundError: No module named '_nlopt'


I'm having trouble importing the nlopt library in Python 3.7.4.

I'm using anaconda, but as nlopt isn't available through conda install, I've installed nlopt with pip.

Here is the error message:

>>> import nlopt
Traceback (most recent call last):
  File "C:\Users\nick.rhodes\AppData\Local\Continuum\anaconda3\lib\site-packages\nlopt\nlopt.py", line 22, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\nick.rhodes\AppData\Local\Continuum\anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 670, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 583, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1043, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

  File "<stdin>", line 1, in <module>
  File "C:\Users\nick.rhodes\AppData\Local\Continuum\anaconda3\lib\site-packages\nlopt\__init__.py", line 1, in <module>
    from .nlopt import *
  File "C:\Users\nick.rhodes\AppData\Local\Continuum\anaconda3\lib\site-packages\nlopt\nlopt.py", line 25, in <module>
    _nlopt = swig_import_helper()
  File "C:\Users\nick.rhodes\AppData\Local\Continuum\anaconda3\lib\site-packages\nlopt\nlopt.py", line 24, in swig_import_helper
    return importlib.import_module('_nlopt')
  File "C:\Users\nick.rhodes\AppData\Local\Continuum\anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_nlopt'

Solution

  • Since you are using conda, You should install nlopt using conda

    conda install -c conda-forge nlopt