-1

I want to write script with logic like below

if <script invoked by python3>:
   do A
elif <script invoked by python2>:
   do B

How can I achieve this?

Sudip
  • 523
  • 2
  • 7
  • 14
  • Version checking is easy: see @Philippe's answer. But what you want to do is less easy. A full answer to your question is beyond the scope of a SO posting. Whole books have been written about it. One such is Lennart Regebro's: https://www.freetechbooks.com/porting-to-python-3-an-in-depth-guide-t1009.html that I made grateful use of in a recent Python 2/3 migration. – BoarGules Mar 24 '21 at 13:02

1 Answers1

0

You are most likely after something similar to this (refer to original question and answer):

import sys
if sys.version_info.major >=  3:
    # Python 3 code
else:
    # Python 2 code
Philippe
  • 196
  • 5
  • Note, the named components (e.g. `.major`) of `sys.version_info` were introduced in 2.7/3.1. If, for some terrible reason, you need to support 2.6 or earlier, or 3.0, you'd test `if sys.version_info >= (3,):` – ShadowRanger Mar 24 '21 at 13:27