-1

how do I identify if Python code was written for 2.7 or 3.x automatically.

Thank you

pinas
  • 2,708
  • 4
  • 21
  • 33
  • It's determined by you environment variables instead of Python script. For example `python --version` to check default Python version. – knh190 Mar 24 '19 at 16:04
  • look for things that are incompatible between the two versions. print statements, future imports, or something along those lines. – Paritosh Singh Mar 24 '19 at 16:04
  • Don't think you can – PrinceOfCreation Mar 24 '19 at 16:05
  • 2
    [@pinas](https://stackoverflow.com/users/1070310/pinas), can you provide a sample of what you want to check? That may help us. – PrinceOfCreation Mar 24 '19 at 16:05
  • 1
    You can't tell a script is to be interpreted by Python 2.7 or Python 3 if I just give you `print('hello world')`. It can be interpreted by both versions. – knh190 Mar 24 '19 at 16:07
  • Go through the `six` library. Everthing provided there is a difference you can check for. – Klaus D. Mar 24 '19 at 16:08
  • @PrinceOfCreation I am more looking for a generic answer. I frequently have to review code snippets of various size and I'd like to have a possibility to identify if the python version it is meant for :) – pinas Mar 24 '19 at 16:09
  • I guess you could try running the stuff under both versions and seeing which one works – PrinceOfCreation Mar 24 '19 at 16:09
  • @pinas then you should google "difference between Python2 and 3". Also read PEP if you can. For example some new keywords are added to Python 3 like `await` and `async` – knh190 Mar 24 '19 at 16:10

1 Answers1

1

You could develop an algorithm that searches for print() functions, f-strings, and other python3-exclusive features

Alec
  • 8,529
  • 8
  • 37
  • 63