5

I am trying to run Python unit test using mock on IDE: PyCharm.

Since I am using Mac (El Capiton), I am unable to do:
pip install mock

Therefore I did:
sudo -H pip install --ignore-installed six
sudo -H pip install --ignore-installed mock

It installed successfully. Now when I write a test, it throws following error:

File "/Library/Python/2.7/site-packages/mock/__init__.py", line 2, in <module>
import mock.mock as _mock
  File "/Library/Python/2.7/site-packages/mock/mock.py", line 68, in <module>
from six import wraps
ImportError: cannot import name wraps

How can I resolve this?

Kreena Mehta
  • 303
  • 1
  • 5
  • 16
  • Unistalled `mock 1.3.0` and installed `mock 1.0.1`. Since the latter does not depend on six, the issue got resolved! – Kreena Mehta Mar 18 '16 at 21:23

2 Answers2

3

Mock requires six version 1.7 or newer. pip show six to get the version and pip install --upgrade six to upgrade.

If that doesn't work, check which version of six is being loaded.

  • Check the Python Version

    thomas:~$ python -c 'import six; print(six.__version__)'
    1.10.0
    
  • Check the pip Version using pip show six

    thomas:~$ pip show six | grep ^Version
    Version: 1.10.0
    
  • Check the Python Location

    thomas:~$ python -c 'import six; print(six.__file__)'
    /Library/Python/2.7/site-packages/six.pyc
    

Delete the python version and pip version are different, delete six.py and six.pyc from the Python Location

Sources:

Stackoverflow

Github

Github

Community
  • 1
  • 1
  • I ran following on my terminal: `find /System/Library/Frameworks/Python.framework/Versions -name six.py*` And found: `/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/lib/six.py /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/lib/six.pyc /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc`. Which one should be deleted? – Kreena Mehta Mar 17 '16 at 23:40
  • There's definitely an old version of `six`. Check `pip show six` and see where pip installed it. Whats your [python path](http://stackoverflow.com/a/16269941/6051898)? Use `python -c "import six; print six.__file__"` to see what version python is loading. – Thomas Zumsteg Mar 17 '16 at 23:45
  • `pip show six` is showing `Location: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python` – Kreena Mehta Mar 18 '16 at 00:01
  • `python -c "import sys; print sys.path` is returning: `['', '/Library/Python/2.7/site-packages/Django-1.9.2-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', ` ... and so on – Kreena Mehta Mar 18 '16 at 00:04
  • What's actually being loaded? `python -c "import six; print six.__file__"`. If it's the same as `pip show six`, what's the version from `pip show six`. six version 1.10.0 is working for me. `pip install --upgrade six` to upgrade. – Thomas Zumsteg Mar 18 '16 at 00:32
3

The issue is that there is an old version of six (1.4.1) in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python and in sys.path this directory comes before /Library/Python/2.7/site-packages where the newer version of six is installed. This looks to me like a configuration issue of the python Apple provides.

The easiest solution I could come up with is setting the PYTHONPATH environment variable explicitly to include /Library/Python/2.7/site-packages, so it ends up in sys.path before the /System/... path:

export PYTHONPATH="/Library/Python/2.7/site-packages:$PYTHONPATH"

I suppose installing a correctly configured python in /usr/local would do the trick as well.

user686249
  • 669
  • 6
  • 17