1

My team has simple python scripts that use use f' strings, so Python 3.6+ is required. The problem is, some people are running on older OS that have Python3.5 installed as the base Python3 interpreter and have supplementally installed 3.6 to run these scripts, and some people are running on newer ones that are using 3.7+ which run this perfectly fine.

If I put this in the .sh script

python3 myscript.py

Then on the older systems it will pick python3.5 and fail, whereas the newer ones will pick python3.7 and succeed

If we put

python3.6 myscript.py

Then on the older systems that have installed Python3.6 manually, it will work, but on the newer systems it won't find python3.6 and will fail, even though python3.7 would have been fine.

Is there a way to say "Use any version of python >= 3.6"? Yes, I realize that you can create venv and then just use python3, but we're trying to script out that part of the setup as well. For example, we might have a .whl that requires 3.6 so we do

#!/bin/sh
python3 -m venv venv
. venv/bin/activate
pip install my.whl
python3 script_that_depends_on_wheel.py

We don't want to have to have every version of Python installed on every system. For the newer systems, there's no reason for them to install Python3.6 when 3.7 works perfectly fine. We don't want to update the system interpreter to a newer version on the off chance that it could break something.

1
  • Have you tried circumventing the problem by using future-fstrings or similar? Commented Nov 5, 2021 at 15:25

1 Answer 1

1

You could have a preamble in your BASH script that looks for appropriate versions of Python. Consider this:

#!/bin/bash
if ! command -v python3 >/dev/null 2>&1
then
  echo "No Python 3 available"
  exit 1
fi
PYTHON=""
for pv in 9 8 7 6
do
  pc="python3.$pv"
  if command -v $pc >/dev/null 2>&1
  then
    PYTHON=$pc
    break
  fi
done
echo $PYTHON

Consequently you can evaluate $PYTHON as your executable providing (obviously) that it has some non-empty value

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

1 Comment

Yah, I though about that, but it's kind of cumbersome to have to add this to all of our scripts everywhere. Plus, then you have to keep them all up to date every time a new version of the interpreter comes out. I was hoping for something more supported, but this is what I'm doing as a band-aid for now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.