前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Python numpyf2py链接库

Python numpyf2py链接库

原创
作者头像
华科云商小徐
发布2025-02-12 11:24:08
发布2025-02-12 11:24:08
7000
代码可运行
举报
文章被收录于专栏:小徐学爬虫小徐学爬虫
运行总次数:0
代码可运行

1、问题背景

用户在使用 Python 的 numpy/f2py 模块将 FORTRAN 程序包装成 Python 模块时遇到了问题。在链接 FORTRAN 程序中用到的外部库时,遇到了错误信息:

代码语言:javascript
代码运行次数:0
复制
gfortran:f77: /var/folders/46/l1mrxgls07s6tpwb6tgpvhpr0000gn/T/tmpPCM7Ne/src.macosx-10.9-intel-2.7/progs-f2pywrappers.f
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/bin/f2py", line 24, in <module>
    main()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/f2py/f2py2e.py", line 588, in main
    run_compile()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/f2py/f2py2e.py", line 574, in run_compile
    setup(ext_modules = [ext])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/core.py", line 186, in setup
    return old_setup(**new_attr)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/command/build.py", line 37, in run
    old_build.run(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build.py", line 127, in run
    self.run_command(cmd_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/command/build_ext.py", line 232, in run
    self.build_extensions()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build_ext.py", line 448, in build_extensions
    self.build_extension(ext)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/command/build_ext.py", line 425, in build_extension
    build_temp=self.build_temp,**kws)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 691, in link_shared_object
    extra_preargs, extra_postargs, build_temp, target_lang)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/distutils/fcompiler/__init__.py", line 643, in link
    libraries)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 571, in gen_lib_options
    runtime_library_dirs, libraries)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 1086, in gen_lib_options
    lib_file = compiler.find_library_file([lib_dir], lib_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 777, in find_library_file
    raise NotImplementedError
​
So, it looks like I am getting the error from the file ccompiler.py from within the python library.
The error corresponds to the following lines of code in the file:
def find_library_file (self, dirs, lib, debug=0):
        """Search the specified list of directories for a static or shared
        library file 'lib' and return the full path to that file.  If
        'debug' true, look for a debugging version (if that makes sense on
        the current platform).  Return None if 'lib' wasn't found in any of
        the specified directories.
        """
        raise NotImplementedError
​
I am a little confused about what I need to do to solve this problem as I am new to Python. It seems as if this method has not been implemented yet. Do I just need to implement it? How exactly do I implement this? Would I need to rebuild the f2py executable on making this change? If so, how would I do this?
Thanks!

2、解决方案

  1. 答案1中给出的方法并没有直接回答问题,但提供了一种解决方法,即直接引用所有 *.o 文件,而不是将它们放入库中。这种方法适用于库基于单个文件的情况。如果库基于多个文件,则只需包含整组 *.o 名称即可。
  2. 问题中的错误信息表明 ccompiler.py 文件中的 find_library_file() 方法未实现。要解决此问题,需要实现此方法。
  3. 您可以在 ccompiler.py 文件中找到该方法的定义。该方法需要搜索指定目录列表中的静态或共享库文件,并返回该文件所在路径。如果在任何指定目录中都找不到库文件,则返回 None
  4. 实现该方法的一种方法是使用 os.listdir() 函数获取目录中的所有文件列表,然后检查每个文件是否与库文件名称匹配。如果找到匹配的文件,则返回该文件的路径。
  5. 以下代码示例展示了如何实现 find_library_file() 方法:
代码语言:javascript
代码运行次数:0
复制
def find_library_file(self, dirs, lib, debug=0):
    """Search the specified list of directories for a static or shared
    library file 'lib' and return the full path to that file.  If
    'debug' true, look for a debugging version (if that makes sense on
    the current platform).  Return None if 'lib' wasn't found in any of
    the specified directories.
    """
    for dir in dirs:
        lib_file = os.path.join(dir, lib)
        if os.path.isfile(lib_file):
            return lib_file
    return None
  1. 实现该方法后,需要重新编译 f2py 可执行文件。重新编译的步骤如下:
  • 确保已安装 f2py 模块。
  • 在终端中导航到 f2py 源代码目录。
  • 运行以下命令:
代码语言:javascript
代码运行次数:0
复制
python setup.py build
  • 运行以下命令:
代码语言:javascript
代码运行次数:0
复制
python setup.py install
  • 重新编译 f2py 可执行文件后,您应该能够在没有错误的情况下链接外部库。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档