PIO looking for .py in a .cpp project

Hi!

I would like to know why PIO is looking for .py file while compiling a .cpp project.

I’m trying to compile this project: GitHub - universam1/iSpindel: electronic Hydrometer

I’m getting this error:

File "git_rev.py", line 5
    print(f'\'-D FIRMWAREVERSION="{version}"\'')
                                              ^
SyntaxError: invalid syntax
OSError: 'python git_rev.py' exited 1:
  File "/home/fernandogarcia/.platformio/penv/lib/python3.6/site-packages/platformio/builder/main.py", line 177:
    env.SConscript("$BUILD_SCRIPT")

Here a screenshot of VSCode:

Thanks in advance!

Best regards.

The project you’re linking to explicitly tells PlatformIO to execute a python script to get build flags from, as you can see in

This is supported per documentation. Thus the file git_rev.py which is contained in the repo will be executed

You can see that it uses format-strings (or, f-strings). You seem to be on a Python 3.6 version in which this isn’t supported though

Try upgrading PlatformIO (PIO Home → Quick Access → Update all) or reinstall it, so that it installs itself with a higher Python version. You can also rewrite the f-string expression to a normal string expression, as e.g.

import subprocess
# Get 0.0.0 version from latest Git tag
tagcmd = "git describe --tags"
version = subprocess.check_output(tagcmd, shell=True).decode().strip()
print('\'-D FIRMWAREVERSION="' + str(version) + '"\'')

Hi!

Thanks for your answer @maxgerhardt !

I have upgraded everything and now I’m running Python 3.7.5 but the problem remains.

I did an alias to execute python3 as python.

Running the command bellow from terminal I had the correct answer.

❯ python git_rev.py
'-D FIRMWAREVERSION="6.5.1-11-gf439bc9"'

But running pio run on terminal or inside of VSCode I got the same compiling error above.

I think PIO is using wrong Python version to compile the code.

Running ~/.platformio/penv/bin/python --version inside of VSCode I have the correct version it’s Python 3.7.5

Using your suggested syntax works to Python 2.7.17 and 3.7.5.

I’ll send a PR for this project.

There’s anyway to select Python version inside of PIO?

Best regards.

There’s in strange behaviour on PIO.

I did a clone of the project to send a PR and the project is compiled without any error and without changes.

Compiling the project downloaded as zip from Github I have compiling error.

Could someone try reproduce this behaviour?

If I clone the project via git and compile and build the project, I get no errors whatsoever. But I also only have a Python 3.8.6 version installed, and no Python 2.x version.

Well yes because it executes git describe --tags but if you download it is a .zip file, then it’s not a git repository anymore in which git commands can be executed – it’s just a plain folder. Hence the execution errors out, this makes sense.

Of course, the Python script could be a bit better there and check if the project folder isn’t an initialized git repository at all…

Hi!

In fact the compiling error for files from zip folder is:

fatal: not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
  File "git_rev.py", line 4, in <module>
    version = subprocess.check_output(tagcmd, shell=True).decode().strip()
  File "/usr/lib/python3.7/subprocess.py", line 411, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)

The error about syntax disappears after specify python version in build_flags.

build_flags = !python3 git_rev.py

Thank you for your help.

Best regards.