Today I wanted to run a (self written) Python script on my OSX laptop, but all of a sudden, all the imports returned an ImportError
. The script was running fine about a month ago and in the meantime I didn't change anything to Python. Furthermore I'm sure that I didn't use a virtualenv back then.
So I just started reinstalling all the packages again (even pip
needed a reinstall). I also need OpenCV, for which I run brew install opencv3
, but that gives me:
Warning: homebrew/science/opencv3 3.2.0 is already installed
even though I still can't import it in Python:
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
I can of course uninstall and reinstall OpenCV, but it really makes me wonder; how can this have happened? What could possibly erase all Python packages?
All tips are welcome!
EDIT
Ok, I just found out that before I was using the Python installed by brew, but that the python
command somehow linked back to /usr/bin/python
instead of /usr/local/Cellar/python/2.7.13_1/bin/python2
in which all packages are still installed correctly.
So to link python
back to the brew version I ran brew unlink python && brew link python
, but which python
still refers to /usr/bin/python
Which brilliant soul can guide me back to using the brew Python?
EDIT2
I just checked out this list of suggestions to link python
to the brew version again, but nothing seems to work. Let me show you what I did:
$ echo $PATH
/usr/local/opt/opencv3/bin:/opt/local/bin:/opt/local/sbin:/usr/local/heroku/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/bin:/Users/hielke/Library/Android/sdk:/Users/hielke/Library/Android/sdk/tools:/Users/hielke/Library/Android/sdk/platform-tools:/usr/local/mysql/bin:/Users/hielke/.composer/vendor/bin
# which shows `/usr/local/bin` before `/usr/bin`
$ brew link --overwrite python
Warning: Already linked: /usr/local/Cellar/python/2.7.13_1
To relink: brew unlink python && brew link python
$ which python
/usr/bin/python # <= STILL RUNNING THE SYSTEM PYTHON
$ brew unlink python && brew link python
Unlinking /usr/local/Cellar/python/2.7.13_1... 26 symlinks removed
Linking /usr/local/Cellar/python/2.7.13_1... 26 symlinks created
$ which python
/usr/bin/python # <= STILL RUNNING THE SYSTEM PYTHON
$ cat /etc/paths
/usr/local/bin
/usr/bin # THIS SEEMS TO BE CORRECT
/bin
/usr/sbin
/sbin
I then restarted the terminal, but which python
still gives me /usr/bin/python
.
So then I restarted the whole OS, but frustratingly which python
still gives me /usr/bin/python
.
Who can get me out of this brew mess?!
Ok, after a lot of messing around, I found that the folder /usr/local/Cellar/python/2.7.13_1/bin/
didn't contain a symlink called python
, just python2
and python2.7
.
So finally I solved it by creating a new symlink in /usr/local/Cellar/python/2.7.13_1/bin/
like this:
ln -s ../Frameworks/Python.framework/Versions/2.7/bin/python python
After that I ran
brew unlink python && brew link python
which solved all my problems.
Thanks for your attention and continuous inspiration!
ps. Although this was a solution to my troubles, I'm still unsure how this could have happened. If anybody can enlighten me that is of course still very welcome!