holoviz / param

Param: Make your Python code clearer and more reliable by declaring Parameters
https://param.holoviz.org
BSD 3-Clause "New" or "Revised" License
412 stars 69 forks source link

'import param' fails: ImportError: cannot import name '__version__' from 'param._version' (/usr/local/lib/python3.9/site-packages/param/_version.py) #897

Closed yurivict closed 5 months ago

yurivict commented 6 months ago

ALL software version info

Version: 2.0.1 Python 3.9 py39-hatch-vcs-0.4.0 py39-hatchling-1.20.0 FreeBSD 14.0

Description of expected behavior and the observed behavior

>>> import param
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/param/__init__.py", line 89, in <module>
    raise FileNotFoundError
FileNotFoundError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/param/__init__.py", line 93, in <module>
    from ._version import __version__
ImportError: cannot import name '__version__' from 'param._version' (/usr/local/lib/python3.9/site-packages/param/_version.py)

Complete, minimal, self-contained example code that reproduces the issue

# code goes here between backticks
hoxbro commented 6 months ago

How did you install param? It looks like it was done with a Linux distribution, which did not package the _version file.

yurivict commented 6 months ago

param was built and installed using the PEP517-compliant build and install projects within the FreeBSD ports framework.

maximlt commented 6 months ago

Hi @yurivict, you mention the install project, is it thin one https://pypi.org/project/install/?

I've just tried the following with success:

Could you please report the exact steps that lead you to the error you reported?

yurivict commented 5 months ago

Here is the log.

maximlt commented 5 months ago

Thanks @yurivict, but I'm still confused by this issue.

I've tried to reproduce locally some of the steps you shared in this log file (get sdist, install build/installer/hatchling/hatch-vcs, isolated wheel build, wheel installed with installer) and could import param without any issue.

Coming back to your traceback, it clearly shows that the param/_version.py module exists (otherwise you would see a ModuleNotFoundError) but that it does not contain a __version__ variable, resulting in an ImportError. However, I can confirm that the sdist on PyPI has a __version__ variable in the param/_version.py module:

❯ curl -sL "https://files.pythonhosted.org/packages/14/c8/b42882ed1ba40478d5c1daa6fbb953c8180ea69fe94b9e6783060f5564ae/param-2.0.1.tar.gz" | tar -xzO "param-2.0.1/param/_version.py"
# 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 = '2.0.1'
__version_tuple__ = version_tuple = (2, 0, 1)

Maybe you could check the content of param/_version.py in your pipeline?

maximlt commented 5 months ago

Oh wait wait wait, I think I know, do you download param-2.0.1.tar.gz from Github?

yurivict commented 5 months ago

Oh wait wait wait, I think I know, do you download param-2.0.1.tar.gz from Github?

No, the tarball is downloaded from the PYPI site: https://pypi.org/packages/source/p/param/param-2.0.1.tar.gz

maximlt commented 5 months ago

Ok ok. I'm at a loss there :/ Really wish I could unblock you. I'd be curious to see the content of param._version.py at different stages of your pipeline.

yurivict commented 5 months ago

Before build:

$ cat ./work-py39/param-2.0.1/param/_version.py
# 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 = '2.0.1'
__version_tuple__ = version_tuple = (2, 0, 1)

After build:

$ cat ./work-py39/param-2.0.1/param/_version.py
# coding: utf-8
# file generated by setuptools_scm
# don't change, don't track in version control
version = '2.0.1'
version_tuple = (2, 0, 1)
yurivict commented 5 months ago

py39-setuptools_scm-6.4.2 is used.

yurivict commented 5 months ago

The latest is setuptools-scm 8.0.4, but it wasn't yet adopted.

Now I vaguely remember that a similar problem happened with some other port because setuptools-scm was 6.x, and they had to patch the project in some way because setuptools-scm changed behavior between versions.

maximlt commented 5 months ago

Indeed __version__ was added in _version.py to setuptools-scm in version 7.0.0 (part of this massive PR https://github.com/pypa/setuptools_scm/pull/580/files#diff-bdefa0298003efada7c465428643cc6d9c129168afb3eb5c551b40fcc77feac1R42).

To add compatibility to older versions of setuptools-scm it seems we could try to import version if importing __version__ fails. I also see in their docs they suggest using:

from importlib.metadata import version, PackageNotFoundError

try:
    __version__ = version("package-name")
except PackageNotFoundError:
    # package is not installed
    pass
maximlt commented 5 months ago

Feel free to have a look at https://github.com/holoviz/param/pull/903 :) We plan to release 2.0.2 soon, it's very likely it'll include this fix.