Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hash bang
#1
something i am trying to come up with is a hash bang string to begin Python scripts with, that will try to run Python3, but will fall back to the version specified by the name without a version number on it, and if that is not available, fall further back to Python2, without setting up or installing any files or doing any config changes on the system that the script with this hash bang string is being run on.
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
I think you need a shell script that finds the correct python interpreter then calls the exec command with that interpreter and the command line arguments that were passed to the script. I suppose all this is for linux?

You could use a python script if you write a version-agnostic code that will work with the system's default python interpreter. Then you would call os.execl.
Reply
#3
yes, for linux, or bsd, or unix. i do write lots of version-agnostic code. i have used os.execvp() often enough. but i would like to do it with no more files than the one that uses the clever hash bang ... e.g. the whole solution on that one line.
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
I don't know if there is a limit on the length of the shebang line, but you can perhaps do something by using python -c together with a line created with this module that converts every python script into a one liner.

Unfortunately, there IS a limit of the length of the shebang line: see here.

The solution is to add at least one file in your system, or to use unpleasant tricks such as writing all your python code in a literal string.
Reply
#5
putting all the code in a literal would still be on the limited hash bang line. so it sounds like a separate file will be needed.

or somehow we need to get python packaged so some particular name always references the latest installed version. too bad they (whoever they is) didn't make "python" do that everywhere. on ubuntu 16.04.3 it references python2 (2.7.12), not python3 (3.5.2). this may be because a lot of ubuntu init code references "python" expecting it to be python2 and they don't want to update their hash bang lines in all that old code (so ubuntu users are still experiencing older python). i should inventory what python usage exists in ubuntu.
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
Why is it a problem to add a separate file?
Reply
#7
(Feb-17-2018, 10:31 AM)Gribouillis Wrote: Why is it a problem to add a separate file?
because this will be part of an initialization system being run on AWS EC2 instances at launch time via user data.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Feb-18-2018, 04:27 AM)Skaperen Wrote: because this will be part of an initialization system being run on AWS EC2 instances at launch time via user data.
This is very opaque. Normally if you're initializing something you can add the files you want to the file system. A script doesn't need to be installed system-wide, it can be installed in a user's directory for example.
Reply
#9
when launching a fresh new instance, it gives you a field called "userdata" which is available to the instance. a package call "cloud-init" checks for it, and if available, and begins with #!/bin/bash (or maybe some other shell, but i have not gotten it to run python, yet), then it runs it (apparently under the selected shell interpreter). the other way to customize a launch is to build your own AMI with the files in that and launch it. but lots of people prefer to launch generic AMIs and use userdata.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
How about the script just making a call to the python code in AWS Lambda?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

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