Skip to content

[Bug] ImportError: cannot import name 'metadata' from 'importlib' on Python 3.7 with v4.67.2 #1705

@iFHiGM

Description

@iFHiGM
  1. Description

Since the release of v4.67.2, tqdm is broken on Python 3.7. It seems the new version attempts to import metadata from the standard importlib library, which was not introduced until Python 3.8.

  1. Checklist
  • I have marked all applicable categories:
    • exception-raising bug
  • I have visited the source website, and in particular
    read the known issues
  • I have searched through the issue tracker for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable:
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)
  1. Context / Environment

Since I cannot import tqdm, here is the manual environment info:

  • tqdm version: 4.67.2
  • Python version: 3.7.7
  • OS: Windows10
  1. Traceback
D:\>python --version
Python 3.7.7

D:\>python -m virtualenv --version
virtualenv 20.26.6 from C:\Data\4_software\Portable\Python\3.7.7\lib\site-packages\virtualenv\__init__.py

D:\>python -m virtualenv .venv_for_test_tqdm_import_error
created virtual environment CPython3.7.7.final.0-64 in 726ms
  creator CPython3Windows(dest=D:\.venv_for_test_tqdm_import_error, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=J:\Data\4_software\Portable\Msys2\home\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==24.0, setuptools==68.0.0, wheel==0.42.0
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

D:\>.venv_for_test_tqdm_import_error\Scripts\activate

(.venv_for_test_tqdm_import_error) D:\>pip list
Package    Version
---------- -------
pip        24.0
setuptools 68.0.0
wheel      0.42.0

(.venv_for_test_tqdm_import_error) D:\>pip install tqdm==4.67.2
Collecting tqdm==4.67.2
  Using cached tqdm-4.67.2-py3-none-any.whl.metadata (57 kB)
Collecting colorama (from tqdm==4.67.2)
  Using cached colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB)
Using cached tqdm-4.67.2-py3-none-any.whl (78 kB)
Using cached colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Installing collected packages: colorama, tqdm
Successfully installed colorama-0.4.6 tqdm-4.67.2

(.venv_for_test_tqdm_import_error) D:\>pip list
Package    Version
---------- -------
colorama   0.4.6
pip        24.0
setuptools 68.0.0
tqdm       4.67.2
wheel      0.42.0

(.venv_for_test_tqdm_import_error) D:\>python -c "import tqdm"
Traceback (most recent call last):
  File "D:\.venv_for_test_tqdm_import_error\lib\site-packages\tqdm\version.py", line 3, in <module>
    from importlib.metadata import PackageNotFoundError, version
ModuleNotFoundError: No module named 'importlib.metadata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\.venv_for_test_tqdm_import_error\lib\site-packages\tqdm\__init__.py", line 3, in <module>
    from .cli import main  # TODO: remove in v5.0.0
  File "D:\.venv_for_test_tqdm_import_error\lib\site-packages\tqdm\cli.py", line 11, in <module>
    from .version import __version__
  File "D:\.venv_for_test_tqdm_import_error\lib\site-packages\tqdm\version.py", line 5, in <module>
    from importlib_metadata import PackageNotFoundError, version
ModuleNotFoundError: No module named 'importlib_metadata'

(.venv_for_test_tqdm_import_error) D:\>pip install tqdm==4.67.1
Collecting tqdm==4.67.1
  Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)
Requirement already satisfied: colorama in d:\.venv_for_test_tqdm_import_error\lib\site-packages (from tqdm==4.67.1) (0.4.6)
Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)
Installing collected packages: tqdm
  Attempting uninstall: tqdm
    Found existing installation: tqdm 4.67.2
    Uninstalling tqdm-4.67.2:
      Successfully uninstalled tqdm-4.67.2
Successfully installed tqdm-4.67.1

(.venv_for_test_tqdm_import_error) D:\>pip list
Package    Version
---------- -------
colorama   0.4.6
pip        24.0
setuptools 68.0.0
tqdm       4.67.1
wheel      0.42.0

(.venv_for_test_tqdm_import_error) D:\>python -c "import tqdm"

(.venv_for_test_tqdm_import_error) D:\>
  1. Possible Fix

In tqdm/version.py(changed in release 4.67.2: https://github.com/tqdm/tqdm/pull/1704/changes#diff-689bd83ef38098c85bf4e5d41d964dfc3ab83dcbce7dd39b2a588794e6f526cfL3), the import logic for metadata should probably check the Python version and fallback to importlib_metadata for Python < 3.8, and importlib-metadata should be explicitly listed in install_requires for these versions.

Metadata

Metadata

Assignees

No one assigned

    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