Python Forum
a python script i would like to have - 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: a python script i would like to have (/thread-397.html)



a python script i would like to have - Skaperen - Oct-09-2016

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.


RE: a python script i would like to have - wavic - Oct-09-2016

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.


RE: a python script i would like to have - metulburr - Oct-09-2016

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.


RE: a python script i would like to have - Skaperen - Oct-15-2016

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: