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

Amo*_*gel 4 python dll fortran mingw python-wheel

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

现在我设法为我的包构建了一个轮子,其中包含一个 Fortran 扩展模块。我构建的计算机有 Windows7 64 和 Python 3.6。

为了让一切正常运行,我遵循了这个非常有用的指南(非常感谢 Michael Hirsch)。步骤之一是使用以下设置安装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
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

扩展.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
Run Code Online (Sandbox Code Playgroud)

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 Code Online (Sandbox Code Playgroud)

run_hello.py读取:

import example.extension_package.fortran_hello
example.extension_package.fortran_hello.hello()
          
Run Code Online (Sandbox Code Playgroud)

fortran_hello.f90读取:

subroutine hello
print *,"Hello World!"
end subroutine hello
Run Code Online (Sandbox Code Playgroud)

轮子的创造

我跑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()
Run Code Online (Sandbox Code Playgroud)

这是应该的。

在没有正确 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.
Run Code Online (Sandbox Code Playgroud)

小智 5

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

因此,由于我单独编译所有步骤,仅使用 f2py 生成 f 和 c 包装器,因此我只是将其添加-static -static-libgfortran -static-libgcc到链接步骤中,这会导致 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"]
                      )
        ]
    )
Run Code Online (Sandbox Code Playgroud)

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

为了比较,有明显的尺寸差异。检查 pyd's with dependency walker 显示nostatic依赖,libgfortran-4.dll 而额外的标志生成一个不依赖于这个 library的 pyd 。在我的情况下,在添加静态标志后,没有正确环境变量的机器能够运行 pyds,我怀疑这种情况与您的情况类似,因为对 libgfortran 的依赖已被删除。

希望有帮助!我的第一个 SO 帖子..