Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从编译库到Fortran模块的Numpy.distutils路径_Python_Fortran_Ffi_Distutils - Fatal编程技术网

Python 从编译库到Fortran模块的Numpy.distutils路径

Python 从编译库到Fortran模块的Numpy.distutils路径,python,fortran,ffi,distutils,Python,Fortran,Ffi,Distutils,我正在围绕C和Fortran库创建Python包装器。我希望这个C/Fortran库中的代码由pip编译,这样用户在运行pip之前就不需要自己安装库了。我目前使用Numpy Distutils实现这一点,setup.py与此类似: #!/usr/bin/env python def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration

我正在围绕C和Fortran库创建Python包装器。我希望这个C/Fortran库中的代码由pip编译,这样用户在运行pip之前就不需要自己安装库了。我目前使用Numpy Distutils实现这一点,setup.py与此类似:

#!/usr/bin/env python
def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('name', parent_package, top_path)
    f90_src = [<list of .f90 files>]
    c_src = [<list of .c files>]
    src = c_src + f90_src
    config.add_library('libname', sources=src,
                       extra_f90_compile_args=['-pthread', '-fno-underscoring'])
    config.add_extension(name='wrapper_name', sources=['wrapper_name.f90'],
                         extra_f90_compile_args=['-pthread'], libraries=['libname'],
                         module_dirs=['build/temp.linux-x86_64-3.7/'])

    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(configuration=configuration)
#/usr/bin/env python
def配置(父\u包=“”,顶部\u路径=无):
从numpy.distutils.misc_util导入配置
config=Configuration('name',父\u包,顶部\u路径)
f90_src=[]
c_src=[]
src=c_src+f90_src
config.add_库('libname',sources=src,
额外\u f90 \u编译\u参数=['-pthread','-fno下划线']))
config.add_扩展名(name='wrapper_name',sources=['wrapper_name.f90'],
额外的\u f90\u编译\u参数=['-pthread'],库=['libname'],
模块_dirs=['build/temp.linux-x86_64-3.7/']
返回配置
如果名称=“\uuuuu main\uuuuuuuu”:
从numpy.distutils.core导入设置
设置(配置=配置)
库中包含Fortran模块,因此在编译调用库的包装器(以及
使用
s其模块)时,我需要告诉编译器在哪里可以找到模块。这是
模块目录
选项。问题是我不得不硬编码。是否有一种可移植且健壮的方法来查找此目录的路径

请注意,我必须使用
add_library
编译我正在包装的代码,而不是将其放入
add_extension
sources
中,就像我使用后者一样
f2py
将尝试包装该代码中的所有接口,但是它使用了Fortran的一些特性,而
f2py
不支持这些特性,因此编译失败

一种解决方法可能是将代码更改为使用接口而不是模块,但我正在包装的代码非常大,对其进行任何如此大的修改都是不可行的


config.get\u build\u temp\u dir()
函数听起来很有希望,但是如果在需要它的地方调用它(说它还没有初始化),就会出现错误。

但是,这不是您特定问题的解决方案,为什么不编译和链接你的C/Fortran代码并发布共享文件,而不是把原始的C/Fortran代码交给用户自己编译呢?目前市场上大部分可用的体系结构是amd64。因此,如果您为这种体系结构构建了库,那么您已经支持了大部分用户社区。谢谢,这是可能的。它并不像我希望的那样优雅,对于使用其他操作系统的用户来说可能会有问题,而且很遗憾,当我目前的方法似乎非常接近理想的解决方案时(我只需要避免这种硬编码的路径),不得不走这条路,但这可能是最好的。