Check Python version from inside script? Run Pythons script in v2 compatibility mode? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Check Python version from inside script? Run Pythons script in v2 compatibility mode? (/thread-3906.html) |
Check Python version from inside script? Run Pythons script in v2 compatibility mode? - pstein - Jul-07-2017 Can I somehow check from inside a Python script if the executing Python engine is major version v2 or v3? I am thinking about a code similar to if (os.python-majorversion<3) print hello else print (hello)Additional question: Is there a way to execute a python script with v3 python engine in v2 compatibility mode? I am thinking about a command parameter like (python.exe is v3.*): python.exe -execute_as_v2 myscript.py RE: Check Python version from inside script? Run Pythons script in v2 compatibility mode? - buran - Jul-07-2017 for version information you can use either platform or sys modules from the standard library also, you may use from __future__ import to import certain python3 features in python2 script, see __future__
RE: Check Python version from inside script? Run Pythons script in v2 compatibility mode? - snippsat - Jul-07-2017 (Jul-07-2017, 06:27 AM)pstein Wrote: I am thinking about a command parameter like (python.exe is v3.*):Have one version that you use most set in Path. Look at my tutorial here. So main version is 3.6 here. C:\1 λ python -V Python 3.6.1 C:\1 λ pip -V pip 9.0.1 from c:\python36\lib\site-packages (python 3.6) C:\1 λ python version.py Hello from 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] # version.py import sys print('Hello from {}'.format(sys.version))I have had om some computer with many version maybe 7-8 installed on Windows. Running other version and install with pip. C:\1 λ py -2.7 version.py Hello from 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] C:\1 λ py -2.7 -m pip install requests Requirement already satisfied: requests in c:\python27\lib\site-packagesSo here running Python 2.7 and can install with pip using -m command.This work for all version installed. C:\1 λ py -3.4 version.py Hello from 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] C:\1 λ py -3.4 -m pip install requests Collecting requests Downloading requests-2.18.1-py2.py3-none-any.whl (88kB) 100% |################################| 92kB 385kB/s Successfully installed certifi-2017.4.17 chardet-3.0.4 idna-2.5 requests-2.18.1 urllib3-1.21.1 C:\1 λ py -3.5 version.py Hello from 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] C:\1 λ py -3.5 -m pip install requests Requirement already satisfied: requests in c:\python35\lib\site-packages |