Python Forum
run my script in a spefic version or any later one
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run my script in a spefic version or any later one
#6
Skaperen Wrote:i have a snippet of code i sometimes insert into the beginning of scripts that checks for the minimum version needed and looks around for a usable version and reruns itself with that engine.

In an old python2 project, I used a custom interpreter and my shebang line was #!/usr/local/bin/chathon. This file was a symlink to a file named chathon.py which would select a python interpreter known as ~/.chabei-apps/chabeibox/bin/python, create a custom environment and launch a subprocess with this interpreter and the correct input, output and error streams.

Such a scheme would work in your case because the chathon command was called before the program's syntaxic analysis. Here is the program chathon.py exactly how it was in this project, you can try and adapt it to your case
#!/usr/bin/env python
# -*-coding: utf8-*-
'''doc
'''
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
import os
import subprocess as sp
import sys
pjoin = os.path.join

def chabeibox(*args):
    return pjoin(os.path.expanduser('~'), '.chabei-apps', 'chabeibox', *args)

class main(object):
    def __call__(self):
        chapython = chabeibox('bin', 'python')
        env = dict(os.environ)
        env['PYTHONPATH'] = ''
        self.nettoie_path(env)
        i, o, e = range(3)
        proc = sp.Popen([chapython] + sys.argv[1:], stdin=i, stdout=o, stderr=e, env=env)
        proc.wait()

    def nettoie_path(self, env):
        L = [x for x in env['PATH'].split(':') if x.startswith(('/usr/', '/bin', '/sbin')) and 'bin' in x]
        L[0:0] = [chabeibox('bin')]
        env['PATH'] =  ':'.join(L)

if __name__ == '__main__':
    main()()
Reply


Messages In This Thread
RE: run my script in a spefic version or any later one - by Gribouillis - Oct-25-2019, 04:08 PM

Forum Jump:

User Panel Messages

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