Mailing List Archive

NameError: name '__version__' is not defined
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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: NameError: name '__version__' is not defined [ In reply to ]
"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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: NameError: name '__version__' is not defined [ In reply to ]
"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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: NameError: name '__version__' is not defined [ In reply to ]
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.
--
https://mail.python.org/mailman/listinfo/python-list