Oct-25-2019, 04:08 PM
(This post was last modified: Oct-25-2019, 04:09 PM by Gribouillis.)
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()()