2

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?!

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 1
    Print the `sys.path` and find if opencv package is in the path – aristotll Jul 22 '17 at 16:54
  • My vote goes to "something messed with a pth file". – Matteo Italia Jul 22 '17 at 16:58
  • @aristotll - Turns out I was using the wrong Python. I was suddenly using the system installed python instead of the brew installed Python (which is the one in which all packages are installed). I edited my question to it. Would you have any idea how I can link `python` to the brew version again? – kramer65 Jul 22 '17 at 17:05
  • @kramer65 `echo $PATH` and see if `/usr/local/bin` is before `/usr/bin` – aristotll Jul 22 '17 at 17:14
  • @aristotll - I just followed pretty much all tips from this list of answers https://stackoverflow.com/questions/5157678/python-homebrew-by-default but nothing seems to work. I edited my question to include all the things I've tried. Do you have any other ideas? – kramer65 Jul 22 '17 at 17:24

2 Answers2

2

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!

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • Sounds like it was due to this issue: https://github.com/Homebrew/homebrew-core/issues/15746 There's lot of comments on that issue so it's hopefully helpful to anyone else struggling with this – pir Jul 20 '20 at 01:28
0

In my case the installed modules seemed to disappear because macOS installed a new minor version and the python3 symlink was updated to point to that new version.

This can be checked by running: ls -laFG /usr/local/bin. As you can see, python3 is pointing to v3.11: Contents of /usr/local/bin/

...which is the new version without any modules installed:

New python modules

However, by explicitly pointing to the old version, we can see all the modules are still there:

Old python modules

LuisCien
  • 6,362
  • 4
  • 34
  • 42