Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
on a system w/o python3
#1
lots of my scripts work fine on python3.  a few even work better when run on python3.  only a few others need python2 and i hope to eventually recode those to get them working on python3.  about half now have a #! (hash-bang) to run python3.  but a problem i have is that the Amazon Linux distro i use for instances in the AWS cloud (Amazon Web Services) only has python2.  python3 won't install right (it is in the repository) due to conflicts.  and the system is very dependent on python so i can't uninstall python2 and reinstall python3.  i could run Ubuntu Linux like i do on my laptop but the Ubuntu Linux server edition available on AWS does not work with IPv6.  so i am stuck with Amazon Linux and stuck with python2.  that means half my scripts won't even run.  so i am trying to figure out a way to run them on python3 where that is available but have them run on python2 where that is the only version available.

before i follow up with my (perhaps silly) ideas, i want to find out if there are any solutions to this kind of problem that already exist.  i don't know what kind of search terms i could use on google for this.

out of curiosity, i'd like to know how Microsoft Windows deals with scripts using different interpreters.  does it support has-bang (#!) or have something equivalent?  can it even do a mix of python2 and python3?
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
Quote:run them on python3 where that is available but have them run on python2 where that is the only version available.
before i follow up with my (perhaps silly) ideas, i want to find out if there are any solutions to this kind of problem that already exist.
import tkinter and if there is an importerror than import Tkinter for example
Recommended Tutorials:
Reply
#3
what will import Tkinter do for/to me?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
You can have multiple versions of Python on Windows. Beginning with Python 3.4 (I think), the use of the shebang line to specify a Python version was added. See https://docs.python.org/3/using/windows.html (scroll down to section 3.4.2 for more information).

I believe he is giving an example of what you could do to as a way of 'testing' which version is installed.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
to start with, i need to change all the hash-bang references back to just plain python (from python3). for those scripts that work better on python3 i add code (running on python2) that checks for the existence of python3, and if it does exit, switch over to python3, which most likely will mean something like:
if sys.version_info.major<3:
    for p in os.environ.get('PATH','').split(':'):
        if p and os.path.exists(p+'/python3'):
            os.execvp('python3',['python3']+sys.argv)
or maybe
if sys.version_info.major<3:
    for p in os.environ.get('PATH','').split(':'):
        if p and os.path.exists(p+'/python3'):
            os.execvp(p+'/python3',['python3']+sys.argv)
where the difference is the 1st argument in the call to os.execvp

anyone know of a way, at least in Linux, in python2, to get the full path of the interpreter being used?

so in Windows does Windows support the #! or does Python.  when a script is run in Windows, what decides which interpreter starts first?  if Python is interpreting the #! can it handle other language interpreters if it is the first interpreter to start?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
In [1]: import subprocess

In [2]: subprocess.check_output(['which', 'python']).decode('ascii')
Out[2]: '/usr/bin/python\n'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
When I decided not to use python 2. whatever anymore, the effort involved with converting legacy
applications was minimal, taking only minutes per application.
The only place where it took longer, was when images were used, as that conversion was a bit
more complex than print or raw_input

I don't write anything with python 2.x in mind. If it works, so be it, but I won't exert any effort to make it so.
There are just over two years of python 2 support remaining. Writing code for 2.x is an effort in futility.
Reply
#8
Can just catch the ImportError,then will work python 2 and 3 in both Windows and Linux.
try:
    # Python 2
    import Tkinter as tk
except ImportError:
     # Python 3
    import tkinter as tk

root = tk.Tk()
w = tk.Label(root, text="Hello, world!")
w.pack()
root.mainloop()
Reply
#9
(Nov-07-2017, 04:29 PM)snippsat Wrote: Can just catch the ImportError,then will work python 2 and 3 in both Windows and Linux.
try:
    # Python 2
    import Tkinter as tk
except ImportError:
     # Python 3
    import tkinter as tk

root = tk.Tk()
w = tk.Label(root, text="Hello, world!")
w.pack()
root.mainloop()

Yeah this is what i meant. Sorry i was on a tablet last night when writing that and i am not fond of writing code on mobile devices.

But you can do this for almost everything. Write the code to be 3.x compliant. Then when 2.x is no more, you can just get rid of the try except clauses and your code remains the same.

You can do more than that though
from __future__ import division
try:
    import Tkinter as tk
    xrange = range
except ImportError:
    import tkinter as tk
But i do this for my games, and then i dont have to worry what system its on, what version is running it, etc. and i will write the code to be 3.x. 5 years ago i would of fought for the ability to code for both 2.x and 3.x, but lately ... 2.x is dead. You might as well just convert the code. At some point everyone is going to be using 3.x. Most are already doing so.

To convert scripts you usually just have to bite the bullet and convert it. This will include adding code as the previous to allow it to run in both interpreters.
Recommended Tutorials:
Reply
#10
(Nov-07-2017, 04:00 PM)Larz60+ Wrote: Writing code for 2.x is an effort in futility.

unfortunately, platforms remain that only have Python2 working, and i need to run one of them and run many of my scripts on them.  a scenario that may affect many is the need to use a module that has not been upgraded to python3.  maybe after python2 is no longer supported, these situations will begin to be resolved.

the first issue i ran into was that #!/usr/bin/env python3 was failing on platforms w/o python3.  it's obvious that something else is needed.  a standard way to "run the latest" version of python on the platform would have been helpful, such as #!/usr/bin/env python-latest, but this does not exist, now.
Tradition is peer pressure from dead people

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


Forum Jump:

User Panel Messages

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