从 bash 脚本检查 Python 开发文件是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4848566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for existence of Python dev files from bash script
提问by CarpeNoctem
I am creating a simple bash script to download and install a python Nagios plugin. On some older servers the script may need to install the subprocess module and as a result I need to make sure the correct python-devel files are installed.
我正在创建一个简单的 bash 脚本来下载和安装一个 python Nagios 插件。在一些较旧的服务器上,脚本可能需要安装 subprocess 模块,因此我需要确保安装了正确的 python-devel 文件。
What is an appropriate and cross platform method of checking for these files. Would like to stay away from rpm or apt.
检查这些文件的合适的跨平台方法是什么。想远离rpm或apt。
If you can tell me how to do the check from within python that would work. Thanks!
如果你能告诉我如何在 python 中进行检查,那将是可行的。谢谢!
Update:
更新:
This is the best I have come up with. Anyone know a better or more conclusive method?
这是我想出的最好的。有人知道更好或更确定的方法吗?
if [ ! -e $(python -c 'from distutils.sysconfig import get_makefile_filename as m; print m()') ]; then echo "Sorry"; fi
采纳答案by Shawn Chin
That would be pretty much how I would go about doing it. Seems reasonable simple.
这几乎就是我要做的。看似合理简单。
However, if I need to be really sure that python-develfiles are installed for the current version of Python, I would look for the relevant Python.hfile. Something along the lines of:
但是,如果我需要真正确定python-devel为当前版本的 Python 安装了文件,我会查找相关Python.h文件。类似的东西:
# first, makes sure distutils.sysconfig usable
if ! $(python -c "import distutils.sysconfig.get_config_vars" &> /dev/null); then
echo "ERROR: distutils.sysconfig not usable" >&2
exit 2
fi
# get include path for this python version
INCLUDE_PY=$(python -c "from distutils import sysconfig as s; print s.get_config_vars()['INCLUDEPY']")
if [ ! -f "${INCLUDE_PY}/Python.h" ]; then
echo "ERROR: python-devel not installed" >&2
exit 3
fi
Note: distutils.sysconfigmay not be supported on all platforms so not the most portable solution, but still better than trying to cater for variations in apt, rpmand the likes.
注意:distutils.sysconfig可能并非在所有平台上都受支持,因此不是最便携的解决方案,但仍然比试图迎合 的变化要好apt,rpm等等。
If you really need to support allplatforms, it might be worth exploring what is done in the AX_PYTHON_DEVELm4 module. This module can be used in a configure.acscript to incorporate checks for python-develduring the ./configurestage of an autotools-based build.
如果您确实需要支持所有平台,则可能值得探索AX_PYTHON_DEVELm4 模块中所做的工作。该模块可以在configure.ac脚本中使用,以python-devel在基于autotools的构建./configure阶段合并检查。
回答by Enrico Carlesso
Imho your solutions works well.
恕我直言,您的解决方案效果很好。
Otherwise, a more "elegant" solution would be to use a tiny script like:
否则,更“优雅”的解决方案是使用一个小脚本,如:
testimport.py
测试导入.py
#!/usr/bin/env python2
import sys
try:
__import__(sys.argv[1])
print "Sucessfully import", sys.argv[1]
except:
print "Error!"
sys.exit(4)
sys.exit(0)
And call it with testimport.sh distutils.sysconfig
并用 testimport.sh distutils.sysconfig 调用它
You can adapt it to check for internal function if needed...
如果需要,您可以调整它以检查内部功能......
回答by Onilton Maciel
For those looking for a pure python solution that also works for python3:
对于那些正在寻找也适用于 python3 的纯 python 解决方案的人:
python3 -c 'from distutils.sysconfig import get_makefile_filename as m; from os.path import isfile; import sys ; sys.exit(not isfile(m()))')
Or as a file script check-py-dev.py:
或作为文件脚本check-py-dev.py:
from distutils.sysconfig import get_makefile_filename as m
from os.path import isfile
import sys
sys.exit(not isfile(m()))
To get a string in bash, just use the exit output:
要在 bash 中获取字符串,只需使用 exit 输出:
python3 check-py-dev.py && echo "Ok" || echo "Error: Python header files NOT found"

