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

一笑奈何 提交于 2019-12-30 06:40:34

问题


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!


回答1:


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)



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/47882916/how-to-make-the-shebang-be-able-to-choose-the-correct-python-interpreter-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!