我如何编写一个 python 脚本,它将在任何 Red Hat 机器上 运行?
How do I write a python script that will run on any Red Hat machine?
如果 python 脚本 运行 在没有安装模块的 RHEL 5 (2.4.3) 上运行,是否保证在任何 RHEL 5、6 或 7 机器上 运行 ?如何处理“/usr/bin/env python”不是 python 2.x 的机器?
不保证可以办理!
RHEL 5 中的 python 版本可能不是 RHEL 6 或 7 中的默认版本。
但这不是什么大问题,需要的版本may/may不能安装在其他机器上。
无论如何,在获得所需版本后,您可以轻松地 运行 使用该版本编写脚本。
喜欢python2 python2-script.py
您的脚本是否需要 运行 在 Python 2 中,或者是否可以将其转换为与 Python 2 和 3 兼容?
否则你可以 运行 像这样的 bash 脚本来检查 python 在你 运行 脚本之前安装的版本并在 运行宁它。
# Change this to whatever the name of your script is
myscript=<The_name_of_your_script>
if python -V | grep -iq 'Python 2'; then
python $myscript
else
# Install prerequisites
yum install gcc openssl-devel bzip2-devel
# Make temp dir for python source
if [ -d ~/tmp ]; then
mkdir -p ~/tmp
fi
cd ~/tmp
# Download and compile python 2
wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
tar xzfv Python-2.7.16.tgz
cd Python-2.7.16
./configure --enable-optimizations
make altinstall
# Run your script with python 2
/usr/local/bin/python-2.7 $myscript
fi
如果 python 脚本 运行 在没有安装模块的 RHEL 5 (2.4.3) 上运行,是否保证在任何 RHEL 5、6 或 7 机器上 运行 ?如何处理“/usr/bin/env python”不是 python 2.x 的机器?
不保证可以办理!
RHEL 5 中的 python 版本可能不是 RHEL 6 或 7 中的默认版本。
但这不是什么大问题,需要的版本may/may不能安装在其他机器上。 无论如何,在获得所需版本后,您可以轻松地 运行 使用该版本编写脚本。
喜欢python2 python2-script.py
您的脚本是否需要 运行 在 Python 2 中,或者是否可以将其转换为与 Python 2 和 3 兼容?
否则你可以 运行 像这样的 bash 脚本来检查 python 在你 运行 脚本之前安装的版本并在 运行宁它。
# Change this to whatever the name of your script is
myscript=<The_name_of_your_script>
if python -V | grep -iq 'Python 2'; then
python $myscript
else
# Install prerequisites
yum install gcc openssl-devel bzip2-devel
# Make temp dir for python source
if [ -d ~/tmp ]; then
mkdir -p ~/tmp
fi
cd ~/tmp
# Download and compile python 2
wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
tar xzfv Python-2.7.16.tgz
cd Python-2.7.16
./configure --enable-optimizations
make altinstall
# Run your script with python 2
/usr/local/bin/python-2.7 $myscript
fi