Python – ImportError: cannot import name ContextualZipFile


I bumped into an error while installing pip in my Mac system and it took me couple of hours to get it resolved. In this article, I will explain the error details and how I was able to resolve it.

Other-python-icon

If you are using Mac, an easy way to install pip is by using easy_install —

$ sudo easy_install pip

However when I ran this command I got an error — ‘ImportError: cannot import name ContextualZipFile’


Traceback (most recent call last):
File “/usr/bin/easy_install-2.7”, line 11, in <module>
load_entry_point(‘setuptools==18.5’, ‘console_scripts’, ‘easy_install’)()
File “/Users/sbehara1/Library/Python/2.7/lib/python/site-packages/pkg_resources/__init__.py”, line 480, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File “/Users/sbehara1/Library/Python/2.7/lib/python/site-packages/pkg_resources/__init__.py”, line 2693, in load_entry_point
return ep.load()
File “/Users/sbehara1/Library/Python/2.7/lib/python/site-packages/pkg_resources/__init__.py”, line 2324, in load
return self.resolve()
File “/Users/sbehara1/Library/Python/2.7/lib/python/site-packages/pkg_resources/__init__.py”, line 2330, in resolve
module = __import__(self.module_name, fromlist=[‘__name__’], level=0)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/command/easy_install.py”, line 46, in <module>
from setuptools.archive_util import unpack_archive
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/archive_util.py”, line 15, in <module>
from pkg_resources import ensure_directory, ContextualZipFile
ImportError: cannot import name ContextualZipFile


Looking at the archive_util.py, I could find the problematic ‘ContextualZipFile’ and most probably the root cause of this issue. It somehow seems that I have an older version of setuptools (or just pkg_resources) in my system site packages, and somehow that’s interfering. Upgrading the setuptools is potentially a resolution here.

archive_util

Pip can be installed in multiple ways — either through your system package manager or through  ensurepip or through the get-pip.py bootstrap module.
setuptools distribution actually provides two packages (pkg_resources and setuptools). You will need to make sure that you install these 2 packages from a single process.

The ensurepip package provides support for bootstrapping the pip installer into an existing Python installation or virtual environment.
The command line interface is invoked using the interpreter’s -m switch. Running the below command worked for me and pip was successfully installed —

$ sudo -H python -m ensurepip

After pip was successfully installed, I noticed that the pkg_resources and setuptools were updated —

easyinstall

I looked at the archive_util.py and did not find the ‘ContextualZipFile’ —

contextualzipfile

Give this a shot and let me know if this fixes your problem.



Categories: Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: