13

I'm new to Python, just installed cvxopt module for my Python3.3 system (64 bit). The installation was successful, but when I typed "import cvxopt" in Python command line, it returned an error:

File "C:\Program Files (x86)\Python\lib\site-packages\cvxopt__init__.py", line 33, in import cvxopt.base ImportError: DLL load failed: The specified module could not be found.

Could anyone help me on this problem? Thanks a lot!

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
user2055265
  • 131
  • 1
  • 1
  • 4

10 Answers10

10

You need to import numpy first before importing cvxopt.

import numpy
import cvxopt
Tsung-Han Yang
  • 141
  • 1
  • 4
  • 5
    I do not see how adding an import could possibly change anything. Please explain this further. (Also, this solution does not work for me.) – Samufi Nov 09 '17 at 02:09
  • This is not the problem, see my comment below. – Daniel Nov 30 '17 at 09:22
  • Welp, doesn't make sense to me either, but this worked for me. Thanks. +1. –  Jul 08 '18 at 14:06
  • 1
    Uninstall numpy, then install numpy+mkl as indicated below by Baokai Zhang (https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy). Also make sure you have python 64bit version installed. Agree that it does not make much sense, but for me: import numpy then import cvxopt worked that way. – Cy Bu Sep 26 '18 at 15:25
9

The package CVXOPT requires numpy+mkl, you can try uninstall the numpy package and reinstall numpy+mkl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy, then importing the CVXOPT, it will work.

Baokai Zhang
  • 107
  • 1
  • 1
  • Current version of Anaconda (e.g. Anaconda 5.x) have MKL support for Numpy. This cannot be the problem. See my answer below. – Daniel Nov 30 '17 at 09:23
  • This worked for me. Make sure to download the .whl associated to your current python version, and dont forget to uninstall numpy as mentionned: `pip uninstall numpy`. Thanks @Baokai Zhang! – JejeBelfort May 20 '20 at 03:16
  • This works for me, even though as others mentioned newer version of Anaconda's numpy should also support mkl. I tried just adding the \Library\bin path to PATH environment variables, it didnt work either. – lizardfireman May 28 '20 at 03:20
9

You need to add YourPythonPath\Library\bin to your PATH environment variable. In my case it is C:\Python36-64\Library\bin

  • Thanks, this worked for me for Python 3.5.4 also. [Not sure why I didn't need this last time](https://stackoverflow.com/questions/14778178/import-cvxopt-base-the-specified-module-could-not-be-found#comment89444602_24214008), but I may have had a different version of Python back then. –  Apr 07 '19 at 20:58
  • If you work in PyCharm, you may have to close and re-open PyCharm after changing the PATH, so that it notices the change. – Erel Segal-Halevi Oct 23 '19 at 13:37
  • This also worked for me AFTER I restarted Python (in my case, restarted VSCode). I use Python 3.7. Note: I additionnally uninstalled `cvxopt` after adding the PATH variable then reinstalled it (with pip), maybe that is also why it worked) – charelf Jun 01 '20 at 11:59
2

The reason for the dll load problem is most likely a very different one. The most up to date Anaconda version (e.g. Anaconda 5.0.1) has Numpy with MKL support. This is NOT the issue. I verified this by looking at the installed packages using conda list. The issue is the fact that cvxopt requires some dlls, which are in the directory

C:\Anaconda3\envs\foo\Library\mingw-w64\bin  

If you install Anaconda and do NOT register Anaconda python on the system path (which is their suggested setup anyway) and then use the Anaconda prommpt to set up a new environment (note for current verison of cvxopt you need Python 3.5)

conda create -n foo python=3.5
activate foo

conda install cvxopt

and importing cvxopt

(foo) C:\tmp>python
Python 3.5.4 |Anaconda, Inc.| (default, Nov  8 2017, 14:34:30) [MSC v.1900 
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cvxopt
>>> 

all should work fine. If you check the Anaconda related path settings you will find

C:\Anaconda3\envs\foo;
C:\Anaconda3\envs\foo\Library\mingw-w64\bin;
C:\Anaconda3\envs\foo\Library\usr\bin;
C:\Anaconda3\envs\foo\Library\bin;
C:\Anaconda3\envs\foo\Scripts;

If you now remove C:\Anaconda3\envs\foo\Library\mingw-w64\bin from the path you are back to the problem

C:\tmp>python
Python 3.5.4 |Anaconda, Inc.| (default, Nov  8 2017, 14:34:30) [MSC v.1900 
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cvxopt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\envs\tf14\lib\site-packages\cvxopt\__init__.py", line 32, in <module>
    import cvxopt.base
ImportError: DLL load failed: The specified module could not be found.
>>>

However there is another issue, related to PyCharm: how to actually use Anaconda envs properly with PyCharm? PyCharm cannot handle additional paths as required by an environment. At leat not out of the box, or I did not find a way to properly do it. Any suggestions are welcome.

Daniel
  • 1,522
  • 1
  • 12
  • 25
1

I fixed it. Just add path C:\Python36\Library\bin to PATH environment variable the same as Artashes Khachatryan said. When I imported cvxopt library, it run base.cp36-win_amd64 file and this file requires dll in bin folder.

0

cvxopt.base is built as a pyd file (essentially a Windows DLL). It should be located in "C:\Program Files (x86)\Python\lib\site-packages\cvxopt". Check that this directory is in your PYTHONPATH. If it isn't, you can append the directory to your path from the command line as follows:

import sys
sys.path.append("C:\Program Files (x86)\Python\lib\site-packages\cvxopt")

Hope that helps!

0

I had the same issue of ImportError while importing cvxopt module. Since cvxopt supports python version 2.7-3.5, I created a conda virtual environment first with python 3.5 using the steps below:

  1. open Anaconda Prompt
  2. conda create -n python=3.5
  3. conda activate

In the activated conda environment install cvxopt package using command: conda install cvxopt

This will install the cvxopt package and all the dependencies.

After installation open spyder by typing spyder in the Anaconda prompt and this will open Spyder with the virtual environment that you have created.

After this cvxopt package will work without any errors.

Note: I have been trying to open the virtual environment in Pycharm but that didn't work and in the end switched to spyder.

0

I had the same issue and what fixed it was to move to python 3.5 (by creating a virtual environment). Note that cvxopt does not work unfortunately with python 3.6.

baduker
  • 19,152
  • 9
  • 33
  • 56
0

I encountered this issue when working in PyCharm 2018.1 in combination with a Conda environment. When I run the following from the Anaconda terminal, everything works fine:

conda activate cvxopt_test
python -c "import cvxopt"

However, when running import cvxopt in PyCharm the error you describe is raised. As Daniel mentioned in his anwser, this has to do with the windows PATH variable (os.environ['PATH']). Somehow PyCharm does not add the cvxopt folders when running the code. For me the minimal working example to avoid the error was:

import os
# add the folder containing the DLL to the PATH variable
os.environ['PATH'] += r';C:\Anaconda3\envs\foo\Library\mingw-w64\bin'

Although it might be wise to add all the folders Daniel mentions to the PATH variable in the same way.

RvdV
  • 406
  • 3
  • 10
0
  1. Open the System Properties window and click on the Advanced tab.
  2. Click the Environment Variables button at the bottom.
  3. In the User variables section, select Path and click Edit.
  4. Add the directory that contains mkl_rt.dll to the path.