本文介绍了setup.py检查是否存在非python库依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试为 cgal-bindings 建立setup.py。要安装此版本,用户需要至少具有特定版本的CGAL。此外,如果用户具有某些库(例如Eigen3),则应构建一些可选目标CGAL。 Python中是否有跨平台的方法来检查这一点? 。 <-实际上这并不是一直有效,某些库仅是标头,例如eigen3,它是C ++模板库。 仅使用 setup()的 install_requires 自变量适用于Python库,而CGAL是C / C ++库。 解决方案是否应根据某些库版本的可用性来编译特定的扩展模块,可以通过动态生成 setup.py 中 setup()的code> ext_modules 个参数。 / p> 对于 ruamel.yaml 的 _yaml.so 模块,只有在我执行的系统上安装了 libyaml 开发库时,才应编译该文件: import from textwrap import dent def check_extensions():检查是否可以通过尝试编译C来构建C模块针对libyaml开发库的小型程序 导入临时文件导入shutdownil 导入distutils.sysconfig 导入distutils.ccompiler来自distutils.errors的导入CompileError,LinkError 库= ['yaml'] #写一个tempora ry .c文件来编译 c_code = dedent( #include< yaml.h> int main(int argc,char * argv []) { yaml_parser_t解析器; parser =解析器; / *防止警告* / 返回0; } ) tmp_dir = tempfile.mkdtemp(前缀='tmp_ruamel_yaml_') bin_file_name = os.path.join(tmp_dir,'test_yaml') file_name = bin_file_name +'.c'以open(file_name,'w')as fp: fp.write(c_code) #并尝试编译编译器= distutils.ccompiler.new_compiler()断言isinstance(编译器,distutils.ccompiler.CCompiler) distutils.sysconfig.customize_compiler(compiler) try: Compiler.link_executable( Compiler.compile([file_name]), bin_file_name, library = libraries,)除了CompileError: print('libyaml编译错误') ret_val =无除外LinkError: print('libyaml链接错误') ret_val =无其他: ret_val = [扩展名('_yaml',来源= ['ext / _yaml.c'],库=库,),] shutil.rmtree(tmp_dir) return ret_val 这样,您不需要分发中的其他文件。即使您不能在编译时根据版本号进行编译,您也应该能够从临时目录运行生成的程序并检查退出值和/或输出。 I'm trying to make a setup.py for cgal-bindings. To install this, the user needs to have at least a certain version of CGAL. In addition, CGAL has a few optional targets that should be built if the user has some libraries (like Eigen3). Is there a cross-platform way in Python to check for this?. <-- This doesn't actually work all the time, some libraries are header-only like eigen3, which is a C++ template library.Using the install_requires argument of setup() only works for Python libraries and CGAL is a C/C++ library. 解决方案 Whether a particular extension module should be compiled depending on the availability of some library version, can be accomplished by dynamically generating the ext_modules argument of setup() in setup.py.For the _yaml.so module of ruamel.yaml, that only should be compiled when the libyaml development libraries have been installed on the system I do:import osfrom textwrap import dedentdef check_extensions(): """check if the C module can be build by trying to compile a small program against the libyaml development library""" import tempfile import shutil import distutils.sysconfig import distutils.ccompiler from distutils.errors import CompileError, LinkError libraries = ['yaml'] # write a temporary .c file to compile c_code = dedent(""" #include <yaml.h> int main(int argc, char* argv[]) { yaml_parser_t parser; parser = parser; /* prevent warning */ return 0; } """) tmp_dir = tempfile.mkdtemp(prefix = 'tmp_ruamel_yaml_') bin_file_name = os.path.join(tmp_dir, 'test_yaml') file_name = bin_file_name + '.c' with open(file_name, 'w') as fp: fp.write(c_code) # and try to compile it compiler = distutils.ccompiler.new_compiler() assert isinstance(compiler, distutils.ccompiler.CCompiler) distutils.sysconfig.customize_compiler(compiler) try: compiler.link_executable( compiler.compile([file_name]), bin_file_name, libraries=libraries, ) except CompileError: print('libyaml compile error') ret_val = None except LinkError: print('libyaml link error') ret_val = None else: ret_val = [ Extension( '_yaml', sources=['ext/_yaml.c'], libraries=libraries, ), ] shutil.rmtree(tmp_dir) return ret_valThis way you require no extra files in the distribution. Even if you cannot fail to compile based on the version number at compile time, you should beable to run the resulting program from the temporary directory and check the exit value and/or output. 这篇关于setup.py检查是否存在非python库依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 13:35