Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect is python program run with python or python3

Tags:

python

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?

like image 358
Legotin Avatar asked Dec 31 '22 04:12

Legotin


1 Answers

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.

like image 160
Ami Tavory Avatar answered Jan 11 '23 14:01

Ami Tavory