用Python打包传统的Fortran。可以使用setuptools和numpy.distutils吗? [英] Packaging legacy Fortran in Python. Is it OK to use setuptools and numpy.distutils?

查看:297
本文介绍了用Python打包传统的Fortran。可以使用setuptools和numpy.distutils吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的领域中的一些流行的Fortran代码制作一个python包发行版。我希望它对 setup.py 文件使用最标准的方法。相关问题有助于学习如何打包Fortran扩展



使用这种方法时,我注意到混合 setuptools numpy.distutils 。混合两者是不好的做法吗?截至2015年,似乎最好尽可能使用 setuptools



然而,我想以一种兼容的方式构建 Fortran numpy。所以我想从 numpy.distutils 导入得到 Extension setup



我使用以下基本方法:

  from setuptools.command.develop从numpy.distutils.core导入开发
导入扩展,设置
$ b $ ext_modules = [Extension(my_package。 fortran_mod,sources = ['src / fortran_mod.f'])]

class MyDevelop(开发):
def run(self):
my_script()
develop.run(self)

setup(
...
ext_modules = ext_modules,
cmdclass = {'develop':MyDevelop})

这似乎有效,但我有疑问。


  1. 通常情况下,将 setuptools numpy.distribute

  2. 我导入它们的顺序很重要吗?我是否应该总是导入 setuptools 第一个?

  3. 是否有正式的最新教程来打包扩展到 numpy的?也许甚至有人讨论了Fortran扩展?






一些链接



https://www.youtube.com/watch ?v = R4yB-8tB0J0



http://www.fortran90.org/src/best-practices.html#interfacing-with-python





这似乎有用,但我有疑问。混合setuptools和numpy.distribute是否是一种很好的做法?

  • 我导入它们的顺序很重要吗?我应该首先导入setuptools吗?

  • 是否有正式的最新教程来打包numpy扩展?也许甚至有人讨论了Fortran扩展?





    1. 您不需要再使用numpy.distribute。

    2. >特别是用numpy包装fortran代码,有流行的 f2py 。但是我个人认为必要的代码注释是多余的,因为好的fortran代码包含了所有必要的信息。 警告个人项目插件如下)



      最近发布的是清洁剂 fmodpy ,它可以在一个可理解和干净的界面中自动生成所有必要的包装代码。它支持Fortran90之前版本,但最适合Fortran90及更高版本。它可以用来生成一个干净的发行版以及代码的python接口(假设用户安装了gfortran)。


      I am trying to make a python package distribution for some popular Fortran codes in my field. I want it to use the most standard approach with a setup.py file. The related qustion was helpful for learning how to wrap Fortran extensions.

      When using this approach, I noticed some confusing behavior when mixing setuptools and numpy.distutils. Is it bad practice to mix the two? As of 2015, it seems preferable to use setuptools as much as possible.

      However, I would like to build Fortran extensions in a way that is compatible with numpy. So I would like to import from numpy.distutils to get Extension and setup.

      I'm using the following basic approach:

      from setuptools.command.develop import develop
      from numpy.distutils.core import Extension, setup
      
      ext_modules=[Extension("my_package.fortran_mod", sources=['src/fortran_mod.f'])]
      
      class MyDevelop(develop):
        def run(self):
          my_script()
          develop.run(self)
      
      setup(
        ...
        ext_modules=ext_modules,
        cmdclass={'develop':MyDevelop})
      

      This seems to work but I have questions.

      1. Is it generally good practice to mix setuptools and numpy.distribute?
      2. Does the order I import them matter? Should I always import setuptools first?
      3. Is there an official up-to-date tutorial for packaging extensions to numpy? Perhaps even one with some discussion Fortran extensions?


      Some links

      https://www.youtube.com/watch?v=R4yB-8tB0J0

      http://www.fortran90.org/src/best-practices.html#interfacing-with-python

      解决方案

      This seems to work but I have questions.

      1. Is it generally good practice to mix setuptools and numpy.distribute?
      2. Does the order I import them matter? Should I always import setuptools first?
      3. Is there an official up-to-date tutorial for packaging extensions to numpy? Perhaps even one with some discussion Fortran extensions?

      1. You should not need to use numpy.distribute anymore.

      2. ^^ Not necessary

      3. Particularly for wrapping fortran code with numpy, there is the popular f2py. However I personally find the necessary code annotations redundant, because good fortran code contains all necessary information.

      (warning personal project plug below)

      Recently released is the cleaner fmodpy, which automatically generates all necessary wrapper code in an understandable and clean interface. It supports pre-Fortran90, but is best suited for Fortran90 and later. It could be used to generate a clean distribution along with the python interface of code (presuming users have gfortran installed).

      这篇关于用Python打包传统的Fortran。可以使用setuptools和numpy.distutils吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆