• NameError: name '__version__' is not defined

    From Loris Bennett@21:1/5 to All on Fri Oct 27 09:29:16 2023
    Hi,

    I have two applications. One uses the system version of Python, which
    is 3.6.8, whereas the other uses Python 3.10.8 installed in a non-system
    path. For both applications I am using poetry with a pyproject.toml
    file which contains the version information and __init__.py at the root
    which contains

    try:
    import importlib.metadata as importlib_metadata
    except ModuleNotFoundError:
    import importlib_metadata

    __version__ = importlib_metadata.version(__name__)

    For the application with the system Python this mechanism works, but for
    the non-system Python I get the error:

    NameError: name '__version__' is not defined

    For the 3.6 application I have

    PYTHONPATH=/nfs/local/lib/python3.6/site-packages
    PYTHONUSERBASE=/nfs/local
    PYTHON_VERSION=3.6
    PYTHON_VIRTUALENV=

    and for the 3.10 application I have

    PYTHONPATH=/nfs/easybuild/software/Python/3.10.8-GCCcore-12.2.0/easybuild/python:/nfs/local/lib/python3.10/site-packages
    PYTHONUSERBASE=/nfs/local
    PYTHON_VERSION=3.10
    PYTHON_VIRTUALENV=

    The applications are installed in /nfs/local/lib/python3.6/site-packages
    and /nfs/local/lib/python3.10/site-packages, respectively.

    Can anyone see where this is going wrong? I thought it should be
    enough that the packages with the metadata is available via PYTHONPATH,
    but this seems not to be sufficient. So I must be overseeing something.

    Cheers,

    Loris

    --
    This signature is currently under constuction.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Loris Bennett@21:1/5 to Loris Bennett on Fri Oct 27 10:54:35 2023
    "Loris Bennett" <loris.bennett@fu-berlin.de> writes:

    Hi,

    I have two applications. One uses the system version of Python, which
    is 3.6.8, whereas the other uses Python 3.10.8 installed in a non-system path. For both applications I am using poetry with a pyproject.toml
    file which contains the version information and __init__.py at the root
    which contains

    try:
    import importlib.metadata as importlib_metadata
    except ModuleNotFoundError:
    import importlib_metadata

    __version__ = importlib_metadata.version(__name__)

    For the application with the system Python this mechanism works, but for
    the non-system Python I get the error:

    NameError: name '__version__' is not defined

    For the 3.6 application I have

    PYTHONPATH=/nfs/local/lib/python3.6/site-packages
    PYTHONUSERBASE=/nfs/local
    PYTHON_VERSION=3.6
    PYTHON_VIRTUALENV=

    and for the 3.10 application I have

    PYTHONPATH=/nfs/easybuild/software/Python/3.10.8-GCCcore-12.2.0/easybuild/python:/nfs/local/lib/python3.10/site-packages
    PYTHONUSERBASE=/nfs/local
    PYTHON_VERSION=3.10
    PYTHON_VIRTUALENV=

    The applications are installed in /nfs/local/lib/python3.6/site-packages
    and /nfs/local/lib/python3.10/site-packages, respectively.

    Can anyone see where this is going wrong? I thought it should be
    enough that the packages with the metadata is available via PYTHONPATH,
    but this seems not to be sufficient. So I must be overseeing something.

    If in the 3.10 application I add

    print(f"__init__ Version: {__version__}")

    to __init__.py the correct version is printed. So the problem is that
    the variable is not available at the point I am trying access it. The
    relevant code (a far as I can tell) in main.py looks like this:

    import typer

    app = typer.Typer()


    @app.callback()
    def version_callback(value: bool):
    if value:
    typer.echo(f"Version: {__version__}")
    raise typer.Exit()


    @app.callback()
    def common(
    ctx: typer.Context,
    version: bool = typer.Option(None, "--version",
    help="Show version",
    callback=version_callback),
    ):
    pass

    if __name__ == "__main__":

    app()

    This is the first time I have used typer, so it is more than likely that
    I have made some mistakes.

    Cheers,

    Loris

    --
    This signature is currently under constuction.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Loris Bennett@21:1/5 to Loris Bennett on Fri Oct 27 11:20:02 2023
    "Loris Bennett" <loris.bennett@fu-berlin.de> writes:

    "Loris Bennett" <loris.bennett@fu-berlin.de> writes:

    Hi,

    I have two applications. One uses the system version of Python, which
    is 3.6.8, whereas the other uses Python 3.10.8 installed in a non-system
    path. For both applications I am using poetry with a pyproject.toml
    file which contains the version information and __init__.py at the root
    which contains

    try:
    import importlib.metadata as importlib_metadata
    except ModuleNotFoundError:
    import importlib_metadata

    __version__ = importlib_metadata.version(__name__)

    For the application with the system Python this mechanism works, but for
    the non-system Python I get the error:

    NameError: name '__version__' is not defined

    For the 3.6 application I have

    PYTHONPATH=/nfs/local/lib/python3.6/site-packages
    PYTHONUSERBASE=/nfs/local
    PYTHON_VERSION=3.6
    PYTHON_VIRTUALENV=

    and for the 3.10 application I have

    PYTHONPATH=/nfs/easybuild/software/Python/3.10.8-GCCcore-12.2.0/easybuild/python:/nfs/local/lib/python3.10/site-packages
    PYTHONUSERBASE=/nfs/local
    PYTHON_VERSION=3.10
    PYTHON_VIRTUALENV=

    The applications are installed in /nfs/local/lib/python3.6/site-packages
    and /nfs/local/lib/python3.10/site-packages, respectively.

    Can anyone see where this is going wrong? I thought it should be
    enough that the packages with the metadata is available via PYTHONPATH,
    but this seems not to be sufficient. So I must be overseeing something.

    If in the 3.10 application I add

    print(f"__init__ Version: {__version__}")

    to __init__.py the correct version is printed. So the problem is that
    the variable is not available at the point I am trying access it. The relevant code (a far as I can tell) in main.py looks like this:

    import typer

    app = typer.Typer()


    @app.callback()
    def version_callback(value: bool):
    if value:
    typer.echo(f"Version: {__version__}")
    raise typer.Exit()


    @app.callback()
    def common(
    ctx: typer.Context,
    version: bool = typer.Option(None, "--version",
    help="Show version",
    callback=version_callback),
    ):
    pass

    if __name__ == "__main__":

    app()

    This is the first time I have used typer, so it is more than likely that
    I have made some mistakes.

    OK, I worked it out. Instead of

    typer.echo(f"Version: {__version__}")

    I need

    typer.echo(f"Version: {mypackage.__version__}")

    Thanks for the help :-)

    Even if no-one replies, it still helps me to have to formulate the
    problem for an audience of people who probably know more than I do.

    Cheers,

    Loris

    --
    This signature is currently under constuction.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Loris Bennett on Fri Oct 27 20:33:15 2023
    Loris Bennett wrote at 2023-10-27 09:29 +0200:
    ...
    For the application with the system Python this mechanism works, but for
    the non-system Python I get the error:

    NameError: name '__version__' is not defined

    If you get exceptions (they usually end in `Error` (such as `NameError`)),
    look at the traceback. The traceback informs you about
    the calling context that led to the exception, espeically where
    in the code the exception occurred.

    If you cannot resolve the problem at your own,
    include the traceback in your call for assistence.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)