Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name wraps

Tags:

I'm using python 2.7.6 on Ubuntu 14.04.2 LTS. I'm using mock to mock some unittests and noticing when I import mock it fails importing wraps.

Not sure if there's a different version of mock or six I should be using for it's import to work? Couldn't find any relevant answers and I'm not using virtual environments.

mock module says it's compatible with python 2.7.x: https://pypi.python.org/pypi/mock

mock==1.1.3 six==1.9.0

Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from mock import Mock Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/lib/python2.7/dist-packages/mock/__init__.py", line 2, in <module>     import mock.mock as _mock   File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 68, in <module>     from six import wraps ImportError: cannot import name wraps 

also tried with sudo with no luck.

$ sudo python -c 'from six import wraps' Traceback (most recent call last):   File "<string>", line 1, in <module> ImportError: cannot import name wraps 
like image 417
Michael Avatar asked Jul 14 '15 21:07

Michael


People also ask

How to Fix ImportError Cannot import name?

The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.

What is import error in Python?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.


2 Answers

Installed mock==1.0.1 and that worked for some reason. (shrugs)

edit: The real fix for me was to updated setuptools to the latest and it allowed me to upgrade mock and six to the latest. I was on setuptools 3.3. In my case I also had to remove said modules by hand because they were owned by OS in '/usr/local/lib/python2.7/dist-packages/'

check versions of everything

pip freeze | grep -e six -e mock easy_install --version 

Update everything

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python pip install mock --upgrade pip install six --upgrade 

Thanks @lifeless

like image 129
Michael Avatar answered Sep 18 '22 20:09

Michael


I encountered the same issue on my mac, which I was able to fix by realizing that my python's sys.path contained both

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/ 

and

/Library/Python/2.7/site-packages/ 

with the former earlier than the latter.

You can test if this is happening to you by running the following in the python console.

import six six.__version__ 

my python was loading an outdated six.py from the former directory (which didn't have wrapper), even though pip had installed a newer version six in the second directory. (It seems mac's framework comes with a version of six by default.)

I was able to fix it by moving six.py and six.pyc out of the first directory (requires sudo access), so that python would find the newer version of six in the second directory. I'm sure you could also change the ordering of the paths in sys.path.

To find the older version of six that need to be deleted run this from the terminal console

find /System/Library/Frameworks/Python.framework/Versions -name six.py* 
like image 33
galv Avatar answered Sep 18 '22 20:09

galv