Python Forum
Check Python version from inside script? Run Pythons script in v2 compatibility mode?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check Python version from inside script? Run Pythons script in v2 compatibility mode?
#1
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
Reply
#2
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__
Reply
#3
(Jul-07-2017, 06:27 AM)pstein Wrote: I am thinking about a command parameter like (python.exe is v3.*):

python.exe -execute_as_v2 myscript.py
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-packages
So 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to include one script into another? MorningWave 8 307 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 218 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 403 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 443 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 339 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Combine console script + GUI (tkinter) dejot 2 361 Feb-27-2024, 04:38 PM
Last Post: deanhystad
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 278 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  OBS Script Troubleshooting Jotatochips 0 253 Feb-10-2024, 06:18 PM
Last Post: Jotatochips
  Is possible to run the python command to call python script on linux? cuten222 6 634 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 321 Jan-26-2024, 07:02 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020