如何使Shebang能够在python3和python3.5之间选择正确的Python解释器 [英] How to make the Shebang be able to choose the correct Python interpreter between python3 and python3.5

查看:95
本文介绍了如何使Shebang能够在python3和python3.5之间选择正确的Python解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python3开发一组脚本,因为我使用了它:

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

#!/usr/bin/env python3

一切正常,但是在某些执行了虚拟机的虚拟机中,解释器的名称为python3.5.我希望能够在两个环境中都可以执行我的脚本,但是我不能更改虚拟机的文件系统(因此,我放弃了将诸如从python3.5链接到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 )

我看着env的男人,但是找不到任何指定搜索模式的方法.

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

我尝试在指向正确的python解释器的会话开始时设置alias,但是env不使用它.

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!

推荐答案

无需引入单独的shell和python脚本,单个文件可以同时使用!

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

按以下顺序替换您的shebang行:

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)

这篇关于如何使Shebang能够在python3和python3.5之间选择正确的Python解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆