1

Importing the cvxopt package isn't working after installing it with pip:

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.15.7
BuildVersion:   19H15
% python3 --version
Python 3.7.2
% pip3 install cvxopt 
Collecting cvxopt
  Using cached cvxopt-1.2.6-cp38-cp38-macosx_10_9_x86_64.whl (3.1 MB)
Installing collected packages: cvxopt
Successfully installed cvxopt-1.2.6
% cat testcvxopt.py 
import cvxopt

print("hi")
% python3 testcvxopt.py 
Traceback (most recent call last):
  File "testcvxopt.py", line 1, in <module>
    import cvxopt
ModuleNotFoundError: No module named 'cvxopt'

I have read and tried most things I could find about installing cvxopt but haven't found anything that works. Any suggestions? Thanks

1 Answers1

1
% python3 --version
Python 3.7.2
% pip3 install cvxopt 
Collecting cvxopt
  Using cached cvxopt-1.2.6-cp38-cp38-macosx_10_9_x86_64.whl (3.1 MB)

Your default python3 is 3.7 but pip3 runs under Python 3.8. Choose which Python you gonna use and use the one.

Either run

python3.7 -m pip install cvxopt

and then use python3.7 to run scripts.

Or use python3.8 the same way.

phd
  • 82,685
  • 13
  • 120
  • 165
  • Well, that did solve it, thanks. But I still don't understand (a) how you knew my pip3 was using Python 3.8, (b) why I've been able to use pip3 to install new packages without a problem until I tried cvxopt, and (c) why my pip3 and python3 are different versions (or how to fix that). – Thomas VanDrunen Apr 05 '21 at 14:27
  • @user2524581 Sorry for late answer — I was on a vacation; I wrote the answer above, turned off my computer and headed to the airport. :-) Now I'm back. Ok, let's see… "*how you knew my pip3 was using Python 3.8*" Easy: `pip3 install cvxopt` installed cvxopt-1.2.6-**cp38-cp38**-macosx_10_9_x86_64.whl. – phd Apr 11 '21 at 23:11
  • "*why I've been able to use pip3 to install new packages without a problem until I tried cvxopt*" Why not? `pip3 install` installed the package successfully, just not for your primary Python but for a different Python. "*why my pip3 and python3 are different versions (or how to fix that)*" In Unix the interpreter used to run a script is written in the [shebang line](https://en.wikipedia.org/wiki/Shebang_(Unix)). So your `pip3` has the shebang pointed to (I think) `/usr/bin/python3.7` while your default Python is `/usr/bin/python3` which actually is `/usr/bin/python3.8`. – phd Apr 11 '21 at 23:14
  • Don't try to edit the shebang line — `pip` is not a usual script, it's a `setuptools`' [entry point](https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html#entry-points) so it's not enough to just "fix" the shebang. Reinstall `pip` using command `python3 -m pip --upgrade pip`; that should reconcile `pip3` with `python3`. – phd Apr 11 '21 at 23:19
  • Sorry, the other way around: your `pip3` has the shebang pointed to `/usr/bin/python3.8` while your default Python is `/usr/bin/python3` which actually is `/usr/bin/python3.7`. – phd Apr 11 '21 at 23:23