Search code examples
pythonpython-poetry

Can python snippets be placed in pyproject.toml?


I would like to read the "version" from a file

# The "version" is no longer taken from here but rather from the "version" file
version = "0.3.13"    # This should be read from a file..

How can this be done inside the pyproject.toml .. or is that not possible? If not possible is there a way to read the contents from a file into a variable inside the pyproject.toml? Ultimately is that file just static ?


Solution

  • toml is a static file format. See https://toml.io So there is no way to read content from a different place.

    Because you are talking about pyproject.toml and version I guess your goal is to have single source of truth for the version number of your python project, so that it can be used during runtime. If so the correct way to do it, to have the version in the pyproject.toml. Once the project is installed you can use importlib.metadata to get the version number in your program:

    import importlib.metadata
    
    
    version = importlib.metadata.version("my-package")