Skip to content

Instantly share code, notes, and snippets.

@spookyahell
Last active January 2, 2019 15:39
Show Gist options
  • Save spookyahell/2d70ab61030882ed7d528e3770215351 to your computer and use it in GitHub Desktop.
Save spookyahell/2d70ab61030882ed7d528e3770215351 to your computer and use it in GitHub Desktop.
py for Linux - Ever wanted sth like the py launcher but for linux... just copy this do /bin and chmod +x /bin/py to make it executable
#!/usr/bin/python3.6
from subprocess import call
from shutil import which
from sys import exit as sexy_exit, argv
from re import fullmatch
from os import environ
from os.path import join as path_join, isfile
command = 'python3.6'
PATH = path_join(environ['HOME'],'pydef')
if isfile(PATH):
with open(PATH) as x:
command = x.read()
del argv[0]
set_def = False
for idx,arg in enumerate(argv):
if arg == '-sad':
arg = '--set-as-default'
if arg == '--set-as-default':
del argv[idx]
set_def = True
break
for idx,arg in enumerate(argv):
x = fullmatch('-(\d).?(\d)?', arg)
if x:
del argv[idx]
vP1 = x.group(1)
vP2 = x.group(2)
if vP2:
if which(f'python{vP1}.{vP2}') is not None:
command = f'python{vP1}.{vP2}'
else:
print('You don\'t have that version of python installed')
sexy_exit(100)
else:
for i in range(0,10):
if which(f'python{vP1}.{i}'):
command = f'python{vP1}.{i}'
break
if set_def:
print('Setting default...')
with open(PATH,'w') as x:
x.write(command)
x = call([command] + argv)
sexy_exit(x)
@spookyahell
Copy link
Author

this currently does not replicate at any length py on windows for linux... it only gives you the basic features... for development I strongly suggest using the original package name like python3.6
The probably most important missing feature is traceback, which this does not handle correctly, you will only see the internal tracback from this file....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment