yt-project / unyt

Handle, manipulate, and convert data with units in Python
https://unyt.readthedocs.io
BSD 3-Clause "New" or "Revised" License
358 stars 46 forks source link

'import unyt' fails: ImportError: cannot import name '__version__' from 'unyt._version' #469

Closed yurivict closed 7 months ago

yurivict commented 8 months ago

Description

In version 3.0.1 'import unyt' fails, and tests fail:

____________________________________________________________________ ERROR collecting unyt/tests/test_array_functions.py ____________________________________________________________________
ImportError while importing test module '/usr/ports/math/py-unyt/work-py39/unyt-3.0.1/unyt/tests/test_array_functions.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
../__init__.py:62: in <module>
    from ._version import __version__
E   ImportError: cannot import name '__version__' from 'unyt._version' (/usr/ports/math/py-unyt/work-py39/unyt-3.0.1/unyt/_version.py)
______________________________________________________________________ ERROR collecting unyt/tests/test_dask_arrays.py ______________________________________________________________________
ImportError while importing test module '/usr/ports/math/py-unyt/work-py39/unyt-3.0.1/unyt/tests/test_dask_arrays.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
../__init__.py:62: in <module>
    from ._version import __version__
E   ImportError: cannot import name '__version__' from 'unyt._version' (/usr/ports/math/py-unyt/work-py39/unyt-3.0.1/unyt/_version.py)

Tests ran fine in 2.9.5

What I Did

pytest

neutrinoceros commented 8 months ago

Hi @yurivict So the difference between v2.9.5 and v3.1.0 is that we switched from versioneer to setuptools_scm to generate the __version__ attribute. I don't know off hand how the old system worked but now (with setuptools_scm), unyt._version is generated at build time, so you need to somehow integrate a build step before you run tests (e.g. using https://pypa-build.readthedocs.io/en/latest/)

neutrinoceros commented 8 months ago

Ah, I see now that unyt._version.py seems to exist in your traceback, just not unyt._version.__version__, so I'm not sure my first comment is actually relevant. Can you show what you did in more detail ?

yurivict commented 8 months ago

Here is the log.

neutrinoceros commented 8 months ago

Thanks. I can see that build is used, and that unyt/_version.py is generated, but then I don't understand what's wrong here. Can you show the content of that module, as generated in your pipeline ?

yurivict commented 8 months ago
$ pkg info -l py39-unyt-3.0.1
py39-unyt-3.0.1:
        /usr/local/lib/python3.9/site-packages/unyt-3.0.1.dist-info/LICENSE
        /usr/local/lib/python3.9/site-packages/unyt-3.0.1.dist-info/METADATA
        /usr/local/lib/python3.9/site-packages/unyt-3.0.1.dist-info/RECORD
        /usr/local/lib/python3.9/site-packages/unyt-3.0.1.dist-info/WHEEL
        /usr/local/lib/python3.9/site-packages/unyt-3.0.1.dist-info/top_level.txt
        /usr/local/lib/python3.9/site-packages/unyt/__init__.py
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/__init__.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/__init__.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_array_functions.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_array_functions.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_deprecation.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_deprecation.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_on_demand_imports.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_on_demand_imports.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_parsing.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_parsing.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_physical_ratios.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_physical_ratios.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_pint_conversions.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_pint_conversions.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_unit_lookup_table.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_unit_lookup_table.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_version.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/_version.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/array.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/array.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/dask_array.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/dask_array.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/dimensions.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/dimensions.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/equivalencies.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/equivalencies.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/exceptions.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/exceptions.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/mpl_interface.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/mpl_interface.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/physical_constants.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/physical_constants.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/testing.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/testing.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_object.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_object.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_registry.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_registry.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_symbols.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_symbols.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_systems.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/__pycache__/unit_systems.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/_array_functions.py
        /usr/local/lib/python3.9/site-packages/unyt/_deprecation.py
        /usr/local/lib/python3.9/site-packages/unyt/_mpl_array_converter/__init__.py
        /usr/local/lib/python3.9/site-packages/unyt/_mpl_array_converter/__pycache__/__init__.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/_mpl_array_converter/__pycache__/__init__.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/_on_demand_imports.py
        /usr/local/lib/python3.9/site-packages/unyt/_parsing.py
        /usr/local/lib/python3.9/site-packages/unyt/_physical_ratios.py
        /usr/local/lib/python3.9/site-packages/unyt/_pint_conversions.py
        /usr/local/lib/python3.9/site-packages/unyt/_unit_lookup_table.py
        /usr/local/lib/python3.9/site-packages/unyt/_version.py
        /usr/local/lib/python3.9/site-packages/unyt/array.py
        /usr/local/lib/python3.9/site-packages/unyt/dask_array.py
        /usr/local/lib/python3.9/site-packages/unyt/dimensions.py
        /usr/local/lib/python3.9/site-packages/unyt/equivalencies.py
        /usr/local/lib/python3.9/site-packages/unyt/exceptions.py
        /usr/local/lib/python3.9/site-packages/unyt/mpl_interface.py
        /usr/local/lib/python3.9/site-packages/unyt/physical_constants.py
        /usr/local/lib/python3.9/site-packages/unyt/testing.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/__init__.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/__init__.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/__init__.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_array_functions.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_array_functions.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_dask_arrays.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_dask_arrays.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_define_unit.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_define_unit.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_mpl_interface.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_mpl_interface.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_no_duplicates.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_no_duplicates.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unit_registry.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unit_registry.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unit_systems.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unit_systems.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_units.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_units.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unyt_array.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unyt_array.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unyt_testing.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/__pycache__/test_unyt_testing.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/data/__init__.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/data/__pycache__/__init__.cpython-39.opt-1.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/data/__pycache__/__init__.cpython-39.pyc
        /usr/local/lib/python3.9/site-packages/unyt/tests/data/old_json_registry.txt
        /usr/local/lib/python3.9/site-packages/unyt/tests/data/unyt_array_sympy1.8.pickle
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_array_functions.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_dask_arrays.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_define_unit.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_mpl_interface.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_no_duplicates.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_unit_registry.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_unit_systems.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_units.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_unyt_array.py
        /usr/local/lib/python3.9/site-packages/unyt/tests/test_unyt_testing.py
        /usr/local/lib/python3.9/site-packages/unyt/unit_object.py
        /usr/local/lib/python3.9/site-packages/unyt/unit_registry.py
        /usr/local/lib/python3.9/site-packages/unyt/unit_symbols.py
        /usr/local/lib/python3.9/site-packages/unyt/unit_systems.py
        /usr/local/share/licenses/py39-unyt-3.0.1/LICENSE
        /usr/local/share/licenses/py39-unyt-3.0.1/MIT
        /usr/local/share/licenses/py39-unyt-3.0.1/catalog.mk
neutrinoceros commented 8 months ago

Sorry I wasn't clear. I meant, can you show the content of unyt/_version.py ?

yurivict commented 8 months ago
$ cat /usr/local/lib/python3.9/site-packages/unyt/_version.py
# coding: utf-8
# file generated by setuptools_scm
# don't change, don't track in version control
version = '3.0.1'
version_tuple = (3, 0, 1)
neutrinoceros commented 8 months ago

Thank you ! I guess you must be using a different version of setuptools_scm than what I get from PyPI by default. Here's what this file is supposed to look like, as of setuptools_scm 8.0.4

# file generated by setuptools_scm
# don't change, don't track in version control
TYPE_CHECKING = False
if TYPE_CHECKING:
    from typing import Tuple, Union
    VERSION_TUPLE = Tuple[Union[int, str], ...]
else:
    VERSION_TUPLE = object

version: str
__version__: str
__version_tuple__: VERSION_TUPLE
version_tuple: VERSION_TUPLE

__version__ = version = '3.0.1'
__version_tuple__ = version_tuple = (3, 0, 1)
neutrinoceros commented 8 months ago

I did some experiments and found that the version of setuptools_scm that is currently indicated as minimal (6.2) doesn't work and leads to the bug you're seeing. The actual minimal requirement is 7.0.1 (excluding 7.0.0 because it was yanked from PyPI)

yurivict commented 8 months ago

We use py39-setuptools_scm-6.4.2 because 8.0.1 broke some other ports, so setuptools_scm update is still pending.

neutrinoceros commented 8 months ago

I suppose a solution for you would be to patch this file with the content I generated with setuptools_scm 8.0.4