How do I access the version number of the package I'm writing?

96 Views Asked by At

I'm trying to build a Python project. I use Poetry to manage dependencies. In my pyproject.toml, I have the following:

[tool.poetry]
name = "gitdida"
version = "0.2.0"

My directory structure is like:

project-root/
|-- pyproject.toml
|-- src/
|   |-- gitdida/
|       |-- __init__.py
|-- tests/
|   |-- test_script.py

I want to be able to access version in the project's code file. However, Poetry itself does not provide access to the code. I look for information on the network, and chatGPT says you can get the version number by importlib.metadata:

from importlib.metadata import version

def test_version():
    expected = "0.2.0"
    assert version("gitdida") == expected

But, after the test I get error:

importlib.metadata.PackageNotFoundError: No package metadata was found for gitdida

Why can't script find the gitdida package? I've been stuck here a long time.

I asked chatGPT 3.5 and Bard, but neither give me a solution.

1

There are 1 best solutions below

0
Aldwen On

Thanks @Brian61354270 After I run poetry install,the script test passed. Thanks.