Skip to content

pkg_resources' working_set caching breaks transformers import on google colab #10964

Description

@konstin

Environment info

  • transformers version: 4.4.2
  • Platform: google colab
  • Python version: 3.7.10
  • PyTorch version (GPU?): n/a
  • Tensorflow version (GPU?): n/a
  • Using GPU in script?: n/a
  • Using distributed or parallel set-up in script?: n/a

Who can help

CC @stas00 as this was implemented in #8645

To reproduce

You can find a complete example in this google colab, also exported to this gist.

To reproduce, first create a new google colab notebook. Let's install recent transformers and tqdm versions in it:

!pip install -U pip
!pip install -U "transformers<5.0.0,>=4.0.0" "tqdm<5.0.0,>=4.45.0"

This currently install transformers 4.4.2 and tqdm 4.59.0.

Surprisingly, now running import transformers fails. We get an error in pkg_resources, which is looking for the .dist-info of tqdm 4.41.1, when the installed version is 4.59.0:

[...]
/usr/local/lib/python3.7/dist-packages/pkg_resources/__init__.py in _get(self, path)
   1609 
   1610     def _get(self, path):
-> 1611         with open(path, 'rb') as stream:
   1612             return stream.read()
   1613 

FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/lib/python3.7/dist-packages/tqdm-4.41.1.dist-info/METADATA'

The cause is that pkg_resources uses the cached WorkingSet, which contains the state before the pip install. We can confirm this by recreating pkg_resources' cache manually:

import pkg_resources

pkg_resources.working_set = pkg_resources.WorkingSet()

Afterwards, importing transformers works.

The above example is the minimized version of our real notebooks examples:

!pip install -U pip
!pip install -U bio_embeddings[all]

from bio_embeddings.embed import SeqVecEmbedder # This line fails with the tqdm .dist-info not found error

Expected behavior

transformers should use the actual installed versions for checking compatibility instead of pkg_resources cache. This could be achieved e.g. by using importlib_metadata instead of pkg_resources here or by recreating pkg_resourcescache withpkg_resources.working_set = pkg_resources.WorkingSet()` before checking versions.

I've used the following snippet to check that importlib_metadata works, which prints 4.41.1 and 4.59.0:

import pkg_resources
import importlib_metadata

print(pkg_resources.get_distribution("tqdm").version)
print(importlib_metadata.version("tqdm"))

I can prepare a pull request for either solution.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions