Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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 使用numpy和测试套件进行打包 介绍_Python_Numpy_Fortran_Distutils_F2py - Fatal编程技术网

Python 使用numpy和测试套件进行打包 介绍

Python 使用numpy和测试套件进行打包 介绍,python,numpy,fortran,distutils,f2py,Python,Numpy,Fortran,Distutils,F2py,免责声明:我对使用distutils的python打包非常陌生。到目前为止,我只是将所有内容都隐藏到模块中,并手动打包并在此基础上开发。我以前从未编写过setup.py文件 我有一个Fortran模块,我想在使用numpy的python代码中使用它。我认为最好的方法是f2py,因为它包含在numpy中。为了自动化构建过程,我想使用distutils和相应的numpy增强功能,其中包括用于f2py包装器的方便功能 我不明白我应该如何组织我的文件,以及如何包含我的测试套件 我想要的是能够使用/set

免责声明:我对使用distutils的python打包非常陌生。到目前为止,我只是将所有内容都隐藏到模块中,并手动打包并在此基础上开发。我以前从未编写过
setup.py
文件

我有一个Fortran模块,我想在使用numpy的python代码中使用它。我认为最好的方法是f2py,因为它包含在numpy中。为了自动化构建过程,我想使用distutils和相应的numpy增强功能,其中包括用于f2py包装器的方便功能

我不明白我应该如何组织我的文件,以及如何包含我的测试套件

我想要的是能够使用
/setup.py
进行构建、安装、测试和开发

我的目录结构如下所示:

volterra
├── setup.py
└── volterra
    ├── __init__.py
    ├── integral.f90
    ├── test
    │   ├── __init__.py
    │   └── test_volterra.py
    └── volterra.f90
setup.py
文件包含以下内容:

def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('volterra', parent_package, top_path)
    config.add_extension('_volterra',
                         sources=['volterra/integral.f90', 'volterra/volterra.f90'])
    return config


if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())
运行
/setup.py build
后,我得到一个

build/lib.linux-x86_64-2.7/
└── volterra
    └── _volterra.so
它既不包括
\uuuu init\uuuu.py
文件,也不包括测试

问题
  • 是否真的需要将路径添加到扩展名的每个源文件中?(例如,
    volterra/integral.f90
    )我不能给出一个参数,表示在
    volterra/
    中查找内容吗?
    top\u路径
    package\u目录
    参数没有起作用
  • 当前,
    \uuuuu init\uuuuuu.py
    文件未包含在生成中。为什么呢
  • 如何在此设置中运行测试
  • 在这样的环境中进行开发的最佳工作流是什么?我不想为我所做的每一次更改安装我的软件包。当您需要编译一些扩展模块时,如何在源目录中进行开发

    • 这是我制作的一个项目的setup.py。我发现搞清楚setup.py/packaging会让人沮丧,因为没有可靠的答案,而且绝对不是pythonic,因为只有一种明显的方法来做某事。希望这会有所帮助

      您可能会发现以下几点很有用:

      • find_packages
        ,它消除了包含大量文件或在生成清单时乱来的繁琐工作
      • package_data
        允许您轻松指定要包含的非.py文件
      • 安装要求
        /
        测试要求
      如果尚未找到distribute_setup.py的源代码,则需要找到它

      • 是否真的需要将路径添加到每个源文件 分机?(即volterra/integral.f90)我不能给出一个参数吗 也就是说,在volterra/中寻找东西?顶部路径和包目录 参数没有起作用
      • 当前,init.py文件不可用 包括在构建中。为什么呢
      希望
      find_packages()
      能够解决这两个问题。我没有太多的包装经验,但我还没有回到手动包含

      • 如何在这种情况下运行测试 设置
      我认为这可能是一个不同的问题,有很多答案取决于你是如何做测试的。也许你可以单独问一下

      作为旁注,我的印象是,标准是将测试目录放在顶层。即
      volterra/volterra
      volterra/tests

      • 在这样的环境中进行开发的最佳工作流是什么 环境我不想为每个人安装我的软件包 我会改变的。您如何在源目录中进行开发 您需要编译一些扩展模块吗
      这可能也值得再问一个问题。我不明白为什么每次更改都需要安装软件包。如果您正在上载该包,只需不要将其安装在您的开发系统上(测试安装除外),直接从您的开发副本开始工作即可。也许我遗漏了一些东西,因为我不使用编译的扩展


      这是一个例子

      try:
          from setuptools import setup, find_packages
      except ImportError:
          from distribute_setup import use_setuptools
          use_setuptools()
          from setuptools import setup, find_packages
      
      
      setup(
          # ... other stuff
          py_modules=['distribute_setup'],
          packages=find_packages(),
          package_data={'': ['*.png']},  # for me to include anything with png
          install_requires=['numpy', 'treenode', 'investigators'],
          tests_require=['mock', 'numpy', 'treenode', 'investigators'],
      )
      

      下面是一个适用于我的setup.py:

      # pkg - A fancy software package
      # Copyright (C) 2013  author (email)
      #
      # This program is free software: you can redistribute it and/or modify
      # it under the terms of the GNU General Public License as published by
      # the Free Software Foundation, either version 3 of the License, or
      # (at your option) any later version.
      #
      # This program is distributed in the hope that it will be useful,
      # but WITHOUT ANY WARRANTY; without even the implied warranty of
      # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      # GNU General Public License for more details.
      #
      # You should have received a copy of the GNU General Public License
      # along with this program.  If not, see http://www.gnu.org/licenses/gpl.html.
      """pkg: a software suite for 
      
      Hey look at me I'm a long description
      But how long am I?
      
      """
      
      from __future__ import division, print_function
      
      #ideas for setup/f2py came from:
      #    -numpy setup.py: https://github.com/numpy/numpy/blob/master/setup.py 2013-11-07
      #    -winpython setup.py: http://code.google.com/p/winpython/source/browse/setup.py 2013-11-07
      #    -needing to use 
      #        import setuptools; from numpy.distutils.core import setup, Extension: 
      #        http://comments.gmane.org/gmane.comp.python.f2py.user/707 2013-11-07
      #    -wrapping FORTRAN code with f2py: http://www2-pcmdi.llnl.gov/cdat/tutorials/f2py-wrapping-fortran-code 2013-11-07
      #    -numpy disutils: http://docs.scipy.org/doc/numpy/reference/distutils.html 2013-11-07
      #    -manifest files in disutils: 
      #        'distutils doesn't properly update MANIFEST. when the contents of directories change.'
      #        https://github.com/numpy/numpy/blob/master/setup.py         
      #    -if things are not woring try deleting build, sdist, egg directories  and try again: 
      #        https://stackoverflow.com/a/9982133/2530083 2013-11-07
      #    -getting fortran extensions to be installed in their appropriate sub package
      #        i.e. "my_ext = Extension(name = 'my_pack._fortran', sources = ['my_pack/code.f90'])" 
      #        Note that sources is a list even if one file: 
      #        http://numpy-discussion.10968.n7.nabble.com/f2py-and-setup-py-how-can-I-specify-where-the-so-file-goes-tp34490p34497.html 2013-11-07
      #    -install fortran source files into their appropriate sub-package 
      #        i.e. "package_data={'': ['*.f95','*.f90']}# Note it's a dict and list":
      #        https://stackoverflow.com/a/19373744/2530083 2013-11-07
      #    -Chapter 9 Fortran Programming with NumPy Arrays: 
      #        Langtangen, Hans Petter. 2013. Python Scripting for Computational Science. 3rd edition. Springer.
      #    -Hitchhikers guide to packaging :
      #        http://guide.python-distribute.org/
      #    -Python Packaging: Hate, hate, hate everywhere : 
      #        http://lucumr.pocoo.org/2012/6/22/hate-hate-hate-everywhere/
      #    -How To Package Your Python Code: 
      #        http://www.scotttorborg.com/python-packaging/
      #    -install testing requirements: 
      #        https://stackoverflow.com/a/7747140/2530083 2013-11-07
      
      import setuptools
      from numpy.distutils.core import setup, Extension
      import os
      import os.path as osp
      
      def readme(filename='README.rst'):
          with open('README.rst') as f:
              text=f.read()
          f.close()
          return text
      
      def get_package_data(name, extlist):
          """Return data files for package *name* with extensions in *extlist*"""
          #modified slightly from taken from http://code.google.com/p/winpython/source/browse/setup.py 2013-11-7
          flist = []
          # Workaround to replace os.path.relpath (not available until Python 2.6):
          offset = len(name)+len(os.pathsep)
          for dirpath, _dirnames, filenames in os.walk(name):
              for fname in filenames:            
                  if not fname.startswith('.') and osp.splitext(fname)[1] in extlist:
      #                flist.append(osp.join(dirpath, fname[offset:]))
                      flist.append(osp.join(dirpath, fname))
          return flist
      
      DOCLINES = __doc__.split("\n")
      CLASSIFIERS = """\
      Development Status :: 1 - Planning
      License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
      Programming Language :: Python :: 2.7
      Topic :: Scientific/Engineering
      """
      
      NAME = 'pkg'
      MAINTAINER = "me"
      MAINTAINER_EMAIL = "me@me.com"
      DESCRIPTION = DOCLINES[0]
      LONG_DESCRIPTION = "\n".join(DOCLINES[2:])#readme('readme.rst')
      URL = "http://meeeee.mmemem"
      DOWNLOAD_URL = "https://github.com/rtrwalker/geotecha.git"
      LICENSE = 'GNU General Public License v3 or later (GPLv3+)'
      CLASSIFIERS = [_f for _f in CLASSIFIERS.split('\n') if _f]
      KEYWORDS=''
      AUTHOR = "me"
      AUTHOR_EMAIL = "me.com"
      PLATFORMS = ["Windows"]#, "Linux", "Solaris", "Mac OS-X", "Unix"]
      MAJOR = 0
      MINOR = 1
      MICRO = 0
      ISRELEASED = False
      VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
      
      INSTALL_REQUIRES=[]
      ZIP_SAFE=False
      TEST_SUITE='nose.collector'
      TESTS_REQUIRE=['nose']
      
      DATA_FILES = [(NAME, ['LICENSE.txt','README.rst'])]
      PACKAGES=setuptools.find_packages()
      PACKAGES.remove('tools')
      
      PACKAGE_DATA={'': ['*.f95','*f90']}               
      ext_files = get_package_data(NAME,['.f90', '.f95','.F90', '.F95'])
      ext_module_names = ['.'.join(osp.splitext(v)[0].split(osp.sep)) for v in ext_files]
      EXT_MODULES = [Extension(name=x,sources=[y]) for x, y in zip(ext_module_names, ext_files)]      
      
      
      setup(
          name=NAME,
          version=VERSION,
          maintainer=MAINTAINER,
          maintainer_email=MAINTAINER_EMAIL,
          description=DESCRIPTION,
          long_description=LONG_DESCRIPTION,
          url=URL,
          download_url=DOWNLOAD_URL,
          license=LICENSE,
          classifiers=CLASSIFIERS,
          author=AUTHOR,
          author_email=AUTHOR_EMAIL,
          platforms=PLATFORMS,
          packages=PACKAGES,
          data_files=DATA_FILES,
          install_requires=INSTALL_REQUIRES,
          zip_safe=ZIP_SAFE,
          test_suite=TEST_SUITE,
          tests_require=TESTS_REQUIRE,
          package_data=PACKAGE_DATA,    
          ext_modules=EXT_MODULES,
          )
      
      要安装,请在命令行中使用:

      python setup.py install
      python setup.py clean --all
      
      我唯一的问题似乎是一个小问题。当我在站点包中查找我的包时,它安装在egg文件夹
      C:\Python27\Lib\site packages\pkg-0.1.0-py2.7-win32.egg\pkg
      中。我看到的大多数其他软件包都有一个
      C:\Python27\Lib\site packages\pkg
      文件夹与egg文件夹分开。有人知道如何得到分离吗

      至于测试,安装后,我在命令行中键入以下内容:

      nosetests package_name -v
      
      尝试调查
      python setup.py develope
      ()是否在每次更改后都不必安装软件包

      正如我在代码中所评论的,我发现以下内容很有用:

      • numpy setup.py:2013-11-07
      • winpython setup.py:2013-11-07
      • 需要使用 导入设置工具;从numpy.distutils.core导入设置,扩展名: 2013-11-07
      • 用f2py包装FORTRAN代码:2013-11-07
      • 纽比分裂:2013-11-07
      • 不完整的清单文件: 'distutils无法正确更新清单。当目录的内容更改时。”
      • 如果情况不正常,请尝试删除生成目录、sdist目录和egg目录,然后重试: 2013-11-07
      • 在相应的子包中安装fortran扩展 i、 e.“my_ext=扩展名(name='my_pack.\u fortran',sources=['my_pack/code.f90'])” 请注意,即使一个文件: 2013-11-07
      • 将fortran源文件安装到相应的子包中 i、 e.“数据包={':['.f95','.f90']}#注意这是一个dict和list”: 2013-11-07
      • 第9章使用NumPy阵列进行Fortran编程: 朗坦根,汉斯·佩特。2013用于计算科学的Python脚本。第三版。斯普林格
      • 搭便车者包装指南:
      • Python打包:恨,恨,恨ev