Python Forum
hash bang - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: hash bang (/thread-8212.html)

Pages: 1 2


hash bang - Skaperen - Feb-10-2018

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.


RE: hash bang - Gribouillis - Feb-15-2018

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.


RE: hash bang - Skaperen - Feb-16-2018

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.


RE: hash bang - Gribouillis - Feb-16-2018

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.


RE: hash bang - Skaperen - Feb-17-2018

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.


RE: hash bang - Gribouillis - Feb-17-2018

Why is it a problem to add a separate file?


RE: hash bang - Skaperen - Feb-18-2018

(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.


RE: hash bang - Gribouillis - Feb-18-2018

(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.


RE: hash bang - Skaperen - Feb-20-2018

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.


RE: hash bang - wavic - Feb-20-2018

How about the script just making a call to the python code in AWS Lambda?