Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Shebang be able to choose the correct Python interpreter between python3 and python3.5

I'm developing a set of script in python3, as shebang I use this:

#!/usr/bin/env python3

Everything goes ok, but in some virtual machines where are executed the name of interpreter is python3.5. I will like to be able to execute my scripts in both enviroment but I can't change the filesystem of virtual machine (so I discard solutions like make a link from python3.5 to python3 )

I look at man of env but I don't find any way to specify a searching pattern or something like that.

I try to set an alias at begining of my sessions pointing to right python interpreter but env don't use it.

My unique solution is call my scripts saying which interpreter must use but is very anoying:

python3.5 myscript.py

Any idea is welcome!, thanks!

like image 728
Joan Esteban Avatar asked Dec 19 '17 08:12

Joan Esteban


People also ask

How do you use shebang in Python 3?

You need to inspect its shebang line. The shebang line is the first line of a script and it starts with #! followed by the path of the interpreter that will be used to execute the script. If the interpreter is /usr/bin/python , you should read the documentation to see whether the script can run with Python 3.

How do I select a Python interpreter?

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Note: If the Python extension doesn't find an interpreter, it issues a warning.

How do I find the Python interpreter path?

1 Answer. For finding the full path of the Python interpreter you can use sys. executable which contains the full path of the currently running Python interpreter.

What are two ways to run a program using the Python interpreter?

There are two ways to use the python interpreter: interactive mode and script mode.


3 Answers

No need to bring in separate shell and python scripts, a single file can be both!

Replace your shebang line with this sequence:

#!/bin/sh

# Shell commands follow
# Next line is bilingual: it starts a comment in Python, and is a no-op in shell
""":"

# Find a suitable python interpreter (adapt for your specific needs) 
for cmd in python3.5 python3 /opt/myspecialpython/bin/python3.5.99 ; do
   command -v > /dev/null $cmd && exec $cmd $0 "$@"
done

echo "OMG Python not found, exiting!!!!!11!!eleven" >2

exit 2

":"""
# Previous line is bilingual: it ends a comment in Python, and is a no-op in shell
# Shell commands end here
# Python script follows (example commands shown)

import sys
print ("running Python!")
print (sys.argv)
like image 52
n. 1.8e9-where's-my-share m. Avatar answered Sep 29 '22 08:09

n. 1.8e9-where's-my-share m.


If you can install scripts, you can also install a wrapper called python3.5 which simply dispatches python3.

#!/bin/sh
exec env python3 "$@"

You'll obviously need to chmod a+x this script just like the others you install.

You'll have to add the script's directory to your PATH after the system python3.5 directory to avoid having this go into an endless loop, and only use this script as a fallback when the system doesn't already provide python3.5.

As you noted, env doesn't know or care about your personal shell aliases or functions, and doesn't provide for any dynamic calculation of the binary to run by itself; but you have the shell at your disposal (and Python of course, once you find it!) -- it simply uses the PATH so if you can install your other scripts in a directory which is in your PATH (which must be the case for the #!/usr/bin/env shebang to make sense in the first place) you can store this script there, too.

As noted in comments, it's weird and user-hostile to only install python3.5 and not at least optionally make python3 a symlink to it, so perhaps you could eventually persuade whoever maintains the image you are installing into to provide this.

like image 32
tripleee Avatar answered Sep 29 '22 10:09

tripleee


You could create a shell script that uses python 3.5 if it is installed, otherwise uses python 3 and executes your script with the correct version.
No need for python shebang.
In your shell script you may test if which python3.5 returns something; if it does, then python3.5 is installed, otherwise you'd have to use python3

like image 25
Mattia Costamagna Avatar answered Sep 29 '22 08:09

Mattia Costamagna