带导入pyfits的py2exe导致错误

时间:2015-07-02 10:24:57

标签: python py2exe pyfits

py-file中的py2exe何时导入pyfits,导致错误。

如何避免此错误?

我准备了test.py和test_setup.py(用于py2exe),如下所示。

test.py是一个简单的程序,用于显示由参数指定的fitsfile的数据。

  • 操作系统:Windows7 64位
  • python:2.7
  • pyfits:3.3
  • numpy:1.9.2
cmd.exe上的

\python test_setup.py py2exe

=>成功

\python test.py test.fits
[[[574 574 579 ..., 579 585 581]
  [574 582 583 ..., 582 579 577]
  [581 583 578 ..., 572 580 580]
  ...,
  [584 585 580 ..., 584 582 579]
  [585 583 582 ..., 586 575 578]
  [584 580 583 ..., 585 585 589]]]

=>成功

\dist\test.exe test.fits
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import pyfits
  File "pyfits\__init__.pyo", line 11, in <module>
  File "pyfits\core.pyo", line 42, in <module>
  File "pyfits\convenience.pyo", line 62, in <module>
  File "pyfits\hdu\__init__.pyo", line 2, in <module>
  File "pyfits\hdu\compressed.pyo", line 21, in <module>
  File "pyfits\hdu\table.pyo", line 646, in <module>
  File "pyfits\hdu\table.pyo", line 909, in BinTableHDU
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

=&GT;错误

test.py

# -*- coding: utf-8 -*-

import sys
import pyfits

def showfits(path):
    hdulist = pyfits.open(path)
    imgs = hdulist[0].data
    hdulist.close()
    print imgs

if __name__ == "__main__":
    showfits(sys.argv[1])

test_setup.py

# -*- coding: utf-8 -*- 

from distutils.core import setup
import py2exe

option = {
    "compressed"    :    1    ,
    "optimize"      :    2    ,
    "bundle_files"  :    3    
}

setup(
    options = {
        "py2exe"    :    option
    },

    console = [
        {
            "script"   :    "test.py"
        }
    ],
    zipfile = None
)

1 个答案:

答案 0 :(得分:0)

查看line it's crashing on

dump.__doc__ += _tdump_file_format.replace('\n', '\n        ')
似乎pyfits正在进行一些类级文档字符串操作。这可以合理地被认为是一个错误,因为不能总是假设一个方法的__doc__将是一个字符串,就像在使用优化编译(就像你)一样,其中包括条带了解所有文档字符串。

我认为最简单的解决方法是要么至少暂时禁用优化,要么修补pyfits以删除该行以及任何类似的行。