Python Forum
a python script i would like to have
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a python script i would like to have
#1
i would like to have a python script that, given the path to another script, will indicate if there is a reason that other script would need to be run under python 3 instead of python 2.  it should have a quiet option that also causes it to exit with an exit status value of 2 for scripts that need to run in python 2.  if there is a slash-bang and it runs python 2 or 3 explicitly then it should presume that is the version it needs.  if the slash-bang does not specify an explicit (e.g "python" with no version given in the name) version then it should look further on for clues.  things like usage of "long" or "xrange" (in real code, not in comments, string literals. or as part of other identifiers) indicate a need for python 2.

sounds like a big project to me.  i have built a little system to run python scripts with an explicit interpreter but i want it to know if version 2 is needed or use version 3 by default.  i want to add a means like this to figure it out.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If the required version is no provided in the shebang you can write your script to use the default version of Python and just check it
sys.version_info.major
For exit code just sys.exit(2) for example.
Note that exit code 2 has a special meaning in linux.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Quote:if there is a reason that other script would need to be run under python 3 instead of python 2.  it should have a quiet option that also causes it to exit with an exit status value of 2 for scripts that need to run in python 2
Most of the time now a days, there is very little reason to quit a script due to version conflict. You should only be doing this if a 3rd party library in which that script uses is not available for one version or the other. Which as time passes, the less libraries there are that are not ported. 

But 99% of the time code that is restricted to one or the other version can be fixed on the behalf of the programmer....such as.

import sys
if sys.version[0] == '3':
    import tkinter as tk
    import urllib.request as req
else:
    import Tkinter as tk
    import urllib2 as req
    range = xrange

#use print()
#use range()
#use tk
#use req.urlopen()
The script this is ran in can be used by both python2.x and python3.x versions. In the script you would use print(), range(), and req.urlopen(). If is ran in python2.x then xrange will be used, otherwise range will be used. And you dont need to import from future to run print() in python 2.7.X. And library location or name changes can be handled in the example of tk. In this case we would use tk to prefix such as tk.Tk(). The same for the script would be using req.urlopen() to open a url.
Recommended Tutorials:
Reply
#4
one reason (not the only reason) i want a script like this is to scan a group of scripts to determine if python 2 needs to by installed in a dynamically installed cloud instance (to be automated).

normally i avoid including the reason in a post like this because there is a lot of history of people spend (wasting) time on specific reasons rather than the question :huh:
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,793 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,254 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,583 May-11-2019, 08:12 PM
Last Post: keames
  How to run python script which has dependent python script in another folder? PrateekG 1 3,105 May-23-2018, 04:50 PM
Last Post: snippsat
  How to call one python script and use its output in another python script lravikumarvsp 3 32,288 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,790 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 6,136 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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