Skip to content

"import pkg_resources" fails with UnicodeDecodeError while parsing /usr/lib/pymodules/python2.7/rpl-1.5.5.egg-info #719

@micklat

Description

@micklat

My ubuntu has an egg-info that pkg_resources fails to read with a UnicodeDecodeError. This made all sorts of things fail in my virtual environment.

> git clone https://github.com/pypa/setuptools
...
> cd setuptools/
> python -c "import pkg_resources"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "pkg_resources/__init__.py", line 2964, in <module>
    @_call_aside
  File "pkg_resources/__init__.py", line 2950, in _call_aside
    f(*args, **kwargs)
  File "pkg_resources/__init__.py", line 2977, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "pkg_resources/__init__.py", line 626, in _build_master
    ws = cls()
  File "pkg_resources/__init__.py", line 619, in __init__
    self.add_entry(entry)
  File "pkg_resources/__init__.py", line 675, in add_entry
    for dist in find_distributions(entry, True):
  File "pkg_resources/__init__.py", line 1988, in find_on_path
    path_item, entry, metadata, precedence=DEVELOP_DIST
  File "pkg_resources/__init__.py", line 2376, in from_location
    py_version=py_version, platform=platform, **kw
  File "pkg_resources/__init__.py", line 2717, in _reload_version
    md_version = _version_from_file(self._get_metadata(self.PKG_INFO))
  File "pkg_resources/__init__.py", line 2341, in _version_from_file
    line = next(iter(version_lines), '')
  File "pkg_resources/__init__.py", line 2509, in _get_metadata
    for line in self.get_metadata_lines(name):
  File "pkg_resources/__init__.py", line 1879, in get_metadata_lines
    return yield_lines(self.get_metadata(name))
  File "pkg_resources/__init__.py", line 1869, in get_metadata
    metadata = f.read()
  File "/usr/lib/python2.7/codecs.py", line 296, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb6 in position 147: invalid start byte in /usr/lib/pymodules/python2.7/rpl-1.5.5.egg-info

The problematic egg-info file is attached, with an added extension (txt) to fool github into accepting the attachment.
rpl-1.5.5.egg-info.txt

This is with python 2.7.2 and setuptools aad4a69.

To make my virtualenv work properly, I had to patch pkg_resources/init.py, replacing the line:

        md_version = _version_from_file(self._get_metadata(self.PKG_INFO))

in EggInfoDistribution._reload_version with this:

        try: 
            md_version = _version_from_file(self._get_metadata(self.PKG_INFO))
        except UnicodeDecodeError as e: 
            warnings.warn(
                'failure to read version number of %s at %s due to: %s' % (self.project_name, self.location, e))
            md_version = None

I can make a pull request out of this if you think this is a good solution.

BTW, the stack trace looks similar to that of #531 . You can also see multiple other people running into the same problem on stackoverflow.com, here, here, and here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions