4

I have a python program that can be run both: with python or python3:

# that
python app.py

# or that
python3 app.py

How to detect inside app.py program which command was used?
I tried to use sys.argv, but it doesn't contains such info.
Any ideas?

1
  • 1
    You could use sys.version_info at the top of the file? Commented Apr 17, 2020 at 12:29

2 Answers 2

6

Try sys.version_info, specifically sys.version_info.major, which should be 2 or 3, respectively.

A different way is using the six package, with the booleans six.PY2 and six.PY3. This package should give other useful utilities for 2/3 compatibility.

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

Comments

3
import sys

print(sys.version)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.