如何使用已编译的 Fortran 扩展模块构建 Python 轮而不需要用户系统上的特定 MinGW 版本?

How to build a Python wheel with compiled Fortran extension module without requiring a specific MinGW version on the user's system?

据我了解,通过轮子分发 Python 包的主要优点之一是我可以以编译形式包含扩展模块。然后,包的用户不需要有一个允许编译源代码的系统。

现在我设法为我的包构建了一个包含 Fort运行 扩展模块的轮子。我构建的计算机具有 Windows7 64 和 Python 3.6.

为了获得一切 运行ning,我按照这个非常有用 guideline (many thanks to Michael Hirsch). One of the steps was to install MinGW-64 进行了以下设置:体系结构:x86_64、线程:posix、异常: seh.

然后我在另一台测试机(Win10 64,Python 3.6)上安装了 Python 软件包:

D:\dist2>pip install SMUTHI-0.2.0a0-cp36-cp36m-win_amd64.whl
Processing d:\dist2\smuthi-0.2.0a0-cp36-cp36m-win_amd64.whl
Requirement already satisfied: scipy in c:\programdata\anaconda3\lib\site-packages (from SMUTHI==0.2.0a0)
Requirement already satisfied: sympy in c:\programdata\anaconda3\lib\site-packages (from SMUTHI==0.2.0a0)
Requirement already satisfied: argparse in c:\programdata\anaconda3\lib\site-packages (from SMUTHI==0.2.0a0)
Requirement already satisfied: numpy in c:\programdata\anaconda3\lib\site-packages (from SMUTHI==0.2.0a0)
Requirement already satisfied: matplotlib in c:\programdata\anaconda3\lib\site-packages (from SMUTHI==0.2.0a0)
Requirement already satisfied: pyyaml in c:\programdata\anaconda3\lib\site-packages (from SMUTHI==0.2.0a0)
Requirement already satisfied: six>=1.10 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->SMUTHI==0.2.0a0)
Requirement already satisfied: python-dateutil in c:\programdata\anaconda3\lib\site-packages (from matplotlib->SMUTHI==0.2.0a0)
Requirement already satisfied: pytz in c:\programdata\anaconda3\lib\site-packages (from matplotlib->SMUTHI==0.2.0a0)
Requirement already satisfied: cycler>=0.10 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->SMUTHI==0.2.0a0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=1.5.6 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->SMUTHI==0.2.0a0)
Installing collected packages: SMUTHI
Successfully installed SMUTHI-0.2.0a0

但是,当我开始测试运行程序时,遇到了以下错误:

D:\dist2>smuthi example_input.dat
Traceback (most recent call last):
  File "c:\programdata\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\programdata\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\ProgramData\Anaconda3\Scripts\smuthi.exe\__main__.py", line 5, in <module>
  File "c:\programdata\anaconda3\lib\site-packages\smuthi\__main__.py", line 4, in <module>
    import smuthi.read_input
  File "c:\programdata\anaconda3\lib\site-packages\smuthi\read_input.py", line 3, in <module>
    import smuthi.simulation
  File "c:\programdata\anaconda3\lib\site-packages\smuthi\simulation.py", line 8, in <module>
    import smuthi.t_matrix as tmt
  File "c:\programdata\anaconda3\lib\site-packages\smuthi\t_matrix.py", line 6, in <module>
    import smuthi.nfmds.t_matrix_axsym as nftaxs
  File "c:\programdata\anaconda3\lib\site-packages\smuthi\nfmds\t_matrix_axsym.py", line 11, in <module>
    import smuthi.nfmds.taxsym
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.

扩展名 .pyd 文件 (taxsym.cp36-win_amd64.pyd) 在其位置 - 只是 Python 无法加载它。

接下来,我从测试机器上卸载了 MinGW,并使用我在构建机器上使用的相同设置重新安装了 MinGW-64(见上文)。之后,我可以运行程序,并且Python能够正确加载扩展模块。

我的问题是:有没有人知道错误首先发生的原因?我怎样才能避免我的 Python 包的用户必须安装特定版本的 MinGW(甚至任何版本)才能使包正常工作?


编辑:一个重现错误的小例子:

最小示例

文件结构:

setup.py
example/
    __init__.py
    run_hello.py
    extension_package/
        __init__.py             
        fortran_hello.f90

setup.py 内容为:

import setuptools
from numpy.distutils.core import Extension
from numpy.distutils.core import setup

setup(
   name="example",
   version="0.1",
   author="My Name",
   author_email="my@email.com",
   description="Example package to demonstrate wheel issue",
   packages=['example', 'example.extension_package'],
   ext_modules=[Extension('example.extension_package.fortran_hello',
                          ['example/extension_package/fortran_hello.f90'])],
)

run_hello.py 内容为:

import example.extension_package.fortran_hello
example.extension_package.fortran_hello.hello()
          

fortran_hello.f90 内容为:

subroutine hello
print *,"Hello World!"
end subroutine hello

造轮子

I 运行 python setup.py bdist_wheel 生成文件 example-0.1-cp36-cp36m-win_amd64.whl

在具有正确 MinGW 版本的机器上安装包

D:\dist>pip install example-0.1-cp36-cp36m-win_amd64.whl
Processing d:\dist\example-0.1-cp36-cp36m-win_amd64.whl
Installing collected packages: example
Successfully installed example-0.1

D:\dist>python
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import example.run_hello
 Hello World!
>>> exit()

这是应该的。

在没有正确 MinGW 版本的机器上安装包

为了重现错误,我将测试机器上的 MinGW 文件夹重命名为其他名称,然后:

D:\dist>pip install example-0.1-cp36-cp36m-win_amd64.whl
Processing d:\dist\example-0.1-cp36-cp36m-win_amd64.whl
Installing collected packages: example
Successfully installed example-0.1

D:\dist>python
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import example.run_hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\example\run_hello.py", line 1, in <module>
    import example.extension_package.fortran_hello
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.

我最近 运行 通过单独编译和 link 编写我自己的 f2py 构建工具链来解决这个问题。如果尚未在路径中找到所需的编译器,则该脚本会自动查找或安装所需的编译器。对于 gfort运行 工具不在路径上但在机器上的情况,我能够将正确的环境变量注入 os.environ 并使用 Popen 和一组环境变量,以便 pyd 可以编译。但是在那个 python 实例之外,环境变量对于 pyd 到 运行 是不正确的,即使在编译 pyds 但没有的同一台计算机上我也遇到了相同的 DLL load failed 错误正确的路径设置。

因此,由于我单独编译所有步骤,仅使用 f2py 生成 f 和 c 包装器,我只是将 -static -static-libgfortran -static-libgcc 添加到我的 link 步骤,这导致 pyd在那些没有正确环境变量的机器上将所需的库包含到 运行。

使用 numpy.distutils 实现相同的目标是可能的(感谢 https://github.com/numpy/numpy/issues/3405):

from numpy.distutils.core import Extension, setup


if __name__ == "__main__":
    setup(
        name="this",
        ext_modules=[
            Extension("fortmod_nostatic",
                      ["src/code.f90"],
                      ),
            Extension("fortmod_withstatic",
                      ["src/code.f90"],
                      extra_link_args=["-static", "-static-libgfortran", "-static-libgcc"]
                      )
        ]
    )

我将上面的内容放在一个文件 test.py 中并用 python test.py build_ext --inplace --compiler=mingw32 --fcompiler=gnu95 -f

构建

比较清楚size difference. Inspecting the pyd's with dependency walker shows the nostatic one depends on libgfortran-4.dll whereas the extra flags generate a pyd that does not depend on this library。在我的例子中,在添加静态标志后,没有正确环境变量的机器能够 运行 pyds,我怀疑这种情况与你的相似,因为对 libgfort运行 的依赖被删除了。

希望对您有所帮助!我的第一个 SO post..

我可以确认 中描述的步骤创建了一个可以从没有安装 MinGW 的机器导入的 pyd 文件。这真的很有帮助!

为了创建二进制轮,我执行以下操作:

  1. 按照 中描述的步骤来生成静态链接的 pyd 文件。
  2. 运行 python setup.py bdist_wheel 之后。

对于第二步,我关闭了 pyd 文件的编译(例如,通过从 setup() 调用中删除 ext_modules 关键字),因为我希望在使用第一步(而不是新创建的,可能不是静态链接的)。

我不知道这是否有意义,但它似乎有效...