ImportError: cannot import name wraps #298

Closed
grantmcconnaughey opened this Issue Jul 28, 2015 · 10 comments

Projects

None yet

5 participants

@grantmcconnaughey

I am getting an ImportError any time I run tests where I use mock. I'm using mock 1.3.0.

The error is ImportError: cannot import name wraps. It doesn't seem that installing mock requires six in setup.py when it probably should. And if it requires a specific version of six (one that has a function called wraps) then it should specify that as well.

Looks like I'm not the only one with this issue: http://stackoverflow.com/questions/31417964/importerror-cannot-import-name-wraps

Traceback:

ImportError: Failed to import test module: my_app.tests.test_forms
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "/Users/grant/my_app/tests/test_forms.py", line 2, in <module>
    from mock import patch, MagicMock
  File "/Users/grant/.virtualenvs/sidekick/lib/python2.7/site-packages/mock/__init__.py", line 2, in <module>
    import mock.mock as _mock
  File "/Users/grant/.virtualenvs/sidekick/lib/python2.7/site-packages/mock/mock.py", line 68, in <module>
    from six import wraps
ImportError: cannot import name wraps
@rbtcollins
Member

We have a versioned dependency on six 1.7 or newer. What version of six do you have installed?

@grantmcconnaughey

I think we're using 1.5.

Your dependency is in requirements.txt, not in setup.py, therefore six is not installed when mock is installed. setup.py needs changed to this:

setuptools.setup(
    install_requires=['six>=1.7'],
    setup_requires=['pbr>=1.3'],
    pbr=True)
@kylios
kylios commented Jul 28, 2015

I am also getting this error using mock 1.3.0 and six 1.9.0:

$ sudo pip install mock
Password:
Collecting mock
  Downloading mock-1.3.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 57kB 431kB/s 
Collecting funcsigs (from mock)
  Downloading funcsigs-0.4-py2.py3-none-any.whl
Collecting pbr>=0.11 (from mock)
  Downloading pbr-1.3.0-py2.py3-none-any.whl (83kB)
    100% |████████████████████████████████| 86kB 453kB/s 
Collecting six>=1.7 (from mock)
  Downloading six-1.9.0-py2.py3-none-any.whl
Installing collected packages: funcsigs, pbr, six, mock
Successfully installed funcsigs-0.4 mock-1.3.0 pbr-1.3.0 six-1.9.0

My error:

$ python tests/test_cli.py
Traceback (most recent call last):
  File "tests/test_cli.py", line 8, in <module>
    from mock import patch
  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
@rbtcollins
Member

@grantmcconnaughey - pbr reflects requirements.txt into setup.py metadata, no change to setup.py should be needed if thats working correctly. The log from @kylios shows that it does indeed work:
Collecting six>=1.7 (from mock)
Downloading six-1.9.0-py2.py3-none-any.whl

If you could include the transcript of your pip install showing it not work, plus a 'pip list' showing the versions of pip and setuptools, we might be able to figure out why it didn't work for you.

@rbtcollins
Member

@kylios thats very weird: I just checked that I can do 'from mock import patch' in a clean venv: just doing:

mkvirtualenv test
pip install -U pip
pip install mock
python -c 'from mock import patch'

worked quite happily.

Can you check that you don't e.g. have a 'six' egg in your current directory or something?
perhaps print six.path from the top of tests/test_cli.py ?

@vitalybe

perhaps print six.path

Ugh, that was the problem for me. I had six installed twice in:

/usr/lib/python2.7/dist-packages/
and in
/usr/local/lib/python2.7/dist-packages/

After I removed the six from /usr/lib/ it worked fine:
rm /usr/lib/python2.7/dist-packages/*six*

@rbtcollins
Member

Ok, so we need @grantmcconnaughey to do the same checks, but so far I'm not seeing an actual mock / mock packaging bug here.

@grantmcconnaughey

We had six pinned to version 1.5.2. I can't remember...does PIP not install a higher version if we have it set to a specific version in our requirements.txt file? If not then that was the issue and this can be disregarded.

@rbtcollins
Member

Yes, in all current releases of pip, setting a version on something in a requirements file will then ignore all dependencies that have different versions.

In future, when pip has a resolver (I have a branch that does this), such setups will error instead.

@rbtcollins rbtcollins closed this Jul 29, 2015
@jackiesyu

@vitalybe I have the same issue with the two paths...

@theY4Kman theY4Kman added a commit to jasonkeene/python-ubersmith that referenced this issue Aug 28, 2015
@theY4Kman theY4Kman fix(reqs): upgrade six>=1.7 to fix mock in tests 2df51ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment