Bottom Page

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
buran wrote Jul-07-2017, 07:08 AM:
Please, use proper tags when post code, traceback, output, etc.
Quote
#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__
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Upgrade Python Version ? JohnnyCoffee 2 100 2 hours ago
Last Post: Larz60+
  Windows 10 Python Version DevSrc8 6 269 Yesterday, 12:36 PM
Last Post: DevSrc8
  Execute full script samuelbachorik 0 70 Aug-06-2020, 08:09 PM
Last Post: samuelbachorik
  How to run diskpart from python script obgocs 1 76 Aug-06-2020, 05:46 AM
Last Post: Gribouillis
  Learning Python with telnetlib. Script seems to stall John_Williamson 1 121 Jul-23-2020, 05:52 PM
Last Post: John_Williamson
  The script won't take voice input ShishirModi 2 98 Jul-21-2020, 05:56 AM
Last Post: ShishirModi
  run a health check script on cloud server through paramiko amritjsr 4 186 Jul-21-2020, 02:30 AM
Last Post: amritjsr
  How to create a Kibana Visualisation with python script? adzadz 0 88 Jul-20-2020, 06:41 AM
Last Post: adzadz
  Need help creating a simple script Nonameface 12 359 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  Run script on startup in Debian with systemd? MrGlasspoole 5 332 Jul-12-2020, 11:48 AM
Last Post: MrGlasspoole

Forum Jump:


Users browsing this thread: 1 Guest(s)