NameError: name '__version__' is not defined

Loris Bennett loris.bennett at fu-berlin.de
Fri Oct 27 05:20:02 EDT 2023


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

> "Loris Bennett" <loris.bennett at 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.


More information about the Python-list mailing list