在 Windows 上构建 C 扩展 Python 包

Building c-extended Python package on Windows

我正在尝试在 64 位 Windows[=33 上构建 'nwalign' Python 程序包 =] 使用 64 位 Python 版本 机器。 (包可以在这里找到:https://pypi.python.org/pypi/nwalign/?)

我尝试使用 'python setup.py install' 构建和安装 Python 2.7 和 3.6 中的软件包,但出现以下链接错误(我已将一些信息替换为“...”):

(C:\...Anaconda3\envs\Python2) C:\...Desktop\nwalign-0.3.1>python setup.py 
...
building 'nwalign/cnwalign' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\nwalign
C:\...AppData\Local\Programs\Common\Microsoft\Visual C++ for Python.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\...Anaconda3\envs\Python2\lib\site-packages\numpy\core\include -Inwalign -IC:\...Anaconda3\envs\Python2\include -IC:\...Anaconda3\envs\Python2\PC /Tcnwalign/cnwalign.c /Fobuild\temp.win-amd64-2.7\Release\nwalign/cnwalign.obj
cnwalign.c
c:\...anaconda3\envs\python2\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(12) : Warning Msg: Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
...
nwalign/cnwalign.c(7433) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
C:\...AppData\Local\Programs\Common\Microsoft\Visual C++ for Python.0\VC\Bin\amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\...Anaconda3\envs\Python2\libs /LIBPATH:C:\...Anaconda3\envs\Python2\PCbuild\amd64 /LIBPATH:C:\...Anaconda3\envs\Python2\PC\VS9.0\amd64 /EXPORT:initnwalign/cnwalign build\temp.win-amd64-2.7\Release\nwalign/cnwalign.obj /OUT:build\lib.win-amd64-2.7\nwalign\cnwalign.pyd /IMPLIB:build\temp.win-amd64-2.7\Release\nwalign\cnwalign.lib /MANIFESTFILE:build\temp.win-amd64-2.7\Release\nwalign\cnwalign.pyd.manifest
LINK : error LNK2001: unresolved external symbol initnwalign/cnwalign
build\temp.win-amd64-2.7\Release\nwalign\cnwalign.lib : fatal error LNK1120: 1 unresolved externals
error: command 'C:\...AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\link.exe' failed with exit status 1120

setup.py 文件:

from setuptools import setup, find_packages
from distutils.extension import Extension
#from Cython.Distutils import build_ext

version = '0.3.1'
import numpy
np_include = numpy.get_include()
try:
    import nwalign
    doc = nwalign.__doc__
except:
    doc = ""

setup(name='nwalign',
      version=version,
      description="Needleman-Wunsch global sequence alignment",
      long_description=doc,
      ext_modules=[ Extension("nwalign/cnwalign",
                      sources=["nwalign/cnwalign.c"],
                      include_dirs=[np_include, "nwalign"])],
      keywords='sequence bioinformatics alignment needleman-wunsch',
      url='http://bitbucket.org/brentp/biostuff/',
      #download_url='http://bitbucket.org/brentp/biostuff/get/tip.tar.gz',
      author='brentp',
      author_email='bpederse@gmail.com',
      license='BSD',
      test_suite='nose.collector',
      include_package_data=True,
      zip_safe=False,
      packages=['nwalign'],
      #package_dir={'': 'nwalign'},
      package_data = {'nwalign': ['*.pyx', "*.c"]},
      install_requires=[
          'numpy', 'cython'
      ],
      entry_points= {
          # -*- Entry points: -*-
          'console_scripts': ['nwalign = nwalign:main']
          },
    classifiers   = [
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Scientific/Engineering :: Bio-Informatics',
        'Topic :: Scientific/Engineering',
        'Topic :: Text Processing'
        ],
)

我已尝试使用适当的 bat 文件将我的 MSCV 编译器定向到 64 位,但无法缓解该问题。 (我 运行 'vcvarsall amd64' 在终端中为 'C:...\AppData\Local\Programs\Common\Microsoft\Visual C++ 来自 Python.0 ')

另一个 nwalign 用户在这里 https://bitbucket.org/brentp/biostuff/issues/1/instalation-issue 提出的相同问题和相关解决方案(更改 .def 文件和删除导出)对我不起作用,因为我的构建不创建 .def 文件,只有cnwalign.obj.

有什么想法吗?

问题是因为 [MS.Docs]: /EXPORT (Exports a Function) 链接器指令使用不正确 (/EXPORT:initnwalign/cnwalign: initnwalign/cnwalign 不是有效的函数标识符)。
那(很容易看出)是由扩展名称触发的:nwalign/cnwalign.

引自 Python2.7.13extension.py 文件 (${ PYTHON2.7.13_INSTALL_DIR}\Lib\distutils\extension.py:31class Extension的一部分):

Instance attributes:
   name : string
     the full name of the extension, including any packages -- ie.
     *not* a filename or pathname, but Python dotted name

所以,在setup.py中,修改实例化Extension的方式,来自:

ext_modules=[ Extension("nwalign/cnwalign",

至:

ext_modules=[ Extension("nwalign.cnwalign",

应该避免错误,并构建 .pyd.