11

I have a python project with a pyproject.toml file. Typically I store the project's version number in pyproject.toml like this:

% grep version pyproject.toml version = "0.0.2" % 

I want to get that version number into a Makefile variable regardless of how many spaces wind up around the version terms.

What should I do to extract the pyproject.toml version string into a Makefile environment variable called VERSION?

1

5 Answers 5

8

This seemed to work out the best... I put this in my Makefile

# grep the version from pyproject.toml, squeeze multiple spaces, delete double # and single quotes, get 3rd val. This command tolerates # multiple whitespace sequences around the version number VERSION := $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) 

Special thanks to @charl-botha for the -m 1 grep argument... both gnu and bsd grep support -m in this context.

Extra info: As of 2024, I stopped grepping through my pyproject.toml for version numbers... now I embed the version in my __about__.py and manage all versions with hatch, which is just awesome for building Python projects (it replaces tools like poetry).

Sign up to request clarification or add additional context in comments.

4 Comments

This will break if there are multiple "version =" statements in the file, e.g. in additional dependency version constraints further down. Use -m 1 argument to grep to use only the first occurrence.
I just ran into this: uvicorn = {extras = ["standard"], version = "^0.17.6"} -- which necessitated the -m 1 in our case. HTH?
The -m 1 option also ONLY works if the first occurence is the correct one. If you have a comment or anything else mentioning "version" before the actual version this approach still fails.
I had a word in my pyproject.toml which contained the string "version" so I used the following to fix it. VERSION := $(shell grep -m 1 'version =' pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)
6

If you have a Python available with the tomli package installed, or you're on Python 3.11 with toml built-in (in which case you would import tomllib instead of tomli), and your pyproject.toml follows poetry convention, something like the following would work well:

VERSION := $(shell python -c 'import tomli; print(tomli.load(open("pyproject.toml", "rb"))["tool"]["poetry"]["version"])') 

Test on command-line with simply:

python -c 'import tomli; print(tomli.load(open("pyproject.toml", "rb"))["tool"]["poetry"]["version"])' 

Obviously you can easily adapt the location of the desired version entry in the case of other pyproject.toml conventions.

BTW, tomllib is in fact tomli, see https://peps.python.org/pep-0680/#rationale

Comments

3

An alternative solution to parse major.minor.patch, based on @Mike Pennington's answer:

grep -m 1 version pyproject.toml | grep -e '\d.\d.\d' -o 

1 Comment

grep -e '\d.\d.\d' -o is a good addition
1

If that's all that file really contains, that's sufficiently close to makefile syntax that you can just include it as a makefile:

include pyproject.toml all: ; echo 'version = $(version)' $ make echo 'version = "0.0.2"' version = "0.0.2" 

If you don't want to, or can't, do that, I'd use sed for it:

VERSION := $(shell sed -n 's/^ *version.*=.*"\([^"]*\)".*/\1/p' pyproject.toml) all: ; echo 'version = $(VERSION)' $ make echo 'version = 0.0.2' version = 0.0.2 

4 Comments

Thanks! I thought that include was a slick way to get pyproject.toml variables into a Makefile, but make choked when it hit this line: [tool.poetry]... it dumped me into the bash prompt. I think grepping the version out is probably better.
As I said, if that line you quoted is the ONLY line in the file, then it will work. If there's other lines in the file, that don't match makefile syntax (such as [tool.poetry]) then include won't work.
I wasn’t completely sure what you meant at the time, but please understand that ‘pyproject.toml’ requires section headings in brackets… the config is useless without section headers.
Yes, so, the include option won't work in that situation.
0
poetry version --short 

You many need to poetry install first.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.