使用setup.py将.pyx编译为.so

时间:2015-01-27 07:20:23

标签: python compilation cython

我有一个包含.pyx及其等效.cpp的模块。

当我运行python setup.py develop时,只有.cpp个文件会转换为.so。但是,由于只有.pyx文件足以让我更改它们,我希望它们转换为.so。这个模块很大,因此单独编译.pyx文件可能会很忙。

理想情况下,我希望python setup.py develop.pyx文件直接转换为.so

这是设置文件

#! /usr/bin/env python
#
# Copyright (C) 2012 Mathieu Blondel

import sys
import os

DISTNAME = 'lightning'
DESCRIPTION = "Large-scale sparse linear classification, " + \
              "regression and ranking in Python"
LONG_DESCRIPTION = open('README.rst').read()
MAINTAINER = 'Mathieu Blondel'
MAINTAINER_EMAIL = 'mathieu@mblondel.org'
URL = 'https://github.com/mblondel/lightning'
LICENSE = 'new BSD'
DOWNLOAD_URL = 'https://github.com/mblondel/lightning'
VERSION = '0.1-git'

import setuptools  # we are using a setuptools namespace
from numpy.distutils.core import setup


def configuration(parent_package='', top_path=None):
    if os.path.exists('MANIFEST'):
        os.remove('MANIFEST')

    from numpy.distutils.misc_util import Configuration
    config = Configuration(None, parent_package, top_path)

    config.add_subpackage('lightning')

    return config

if __name__ == "__main__":

    old_path = os.getcwd()
    local_path = os.path.dirname(os.path.abspath(sys.argv[0]))

    os.chdir(local_path)
    sys.path.insert(0, local_path)

    setup(configuration=configuration,
          name=DISTNAME,
          maintainer=MAINTAINER,
          include_package_data=True,
          scripts=["bin/lightning_train",
                   "bin/lightning_predict"],
          maintainer_email=MAINTAINER_EMAIL,
          description=DESCRIPTION,
          license=LICENSE,
          url=URL,
          version=VERSION,
          download_url=DOWNLOAD_URL,
          long_description=LONG_DESCRIPTION,
          zip_safe=False, # the package can run out of an .egg file
          classifiers=[
              'Intended Audience :: Science/Research',
              'Intended Audience :: Developers',
              'License :: OSI Approved',
              'Programming Language :: C',
              'Programming Language :: Python',
              'Topic :: Software Development',
              'Topic :: Scientific/Engineering',
              'Operating System :: Microsoft :: Windows',
              'Operating System :: POSIX',
              'Operating System :: Unix',
              'Operating System :: MacOS'
             ]
    )

这是Make File,

PYTHON ?= python
CYTHON ?= cython
NOSETESTS ?= nosetests
DATADIR=$(HOME)/lightning_data

# Compilation...

CYTHONSRC= $(wildcard lightning/impl/*.pyx lightning/impl/randomkit/*.pyx)
CSRC= $(CYTHONSRC:.pyx=.cpp)

inplace:
    $(PYTHON) setup.py build_ext -i

all: cython inplace

cython: $(CSRC)

clean:
    rm -f lightning/impl/*.c lightning/impl/*.html
    rm -f `find lightning -name "*.pyc"`
    rm -f `find lightning -name "*.so"`

%.cpp: %.pyx
    $(CYTHON) --cplus $<

# Tests...
#
test-code: in
    $(NOSETESTS) -s lightning

test-coverage:
    $(NOSETESTS) -s --with-coverage --cover-html --cover-html-dir=coverage \
    --cover-package=lightning lightning

test: test-code test-doc

# Datasets...
#
datadir:
    mkdir -p $(DATADIR)

# regression
download-abalone: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/abalone_scale
    mv abalone_scale $(DATADIR)

download-cadata: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/cadata
    mv cadata $(DATADIR)

download-cpusmall: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/cpusmall_scale
    mv cpusmall_scale $(DATADIR)

download-space_ga: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/space_ga_scale
    mv space_ga_scale $(DATADIR)

download-YearPredictionMSD: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/YearPredictionMSD.bz2
    bunzip2 YearPredictionMSD.bz2
    mv YearPredictionMSD $(DATADIR)
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/YearPredictionMSD.t.bz2
    bunzip2 YearPredictionMSD.t.bz2
    mv YearPredictionMSD.t $(DATADIR)

# binary classification
download-adult: datadir
    ./download.sh http://leon.bottou.org/_media/papers/lasvm-adult.tar.bz2
    tar xvfj lasvm-adult.tar.bz2
    mv adult $(DATADIR)
    rm lasvm-adult.tar.bz2

download-banana: datadir
    ./download.sh http://leon.bottou.org/_media/papers/lasvm-banana.tar.bz2
    tar xvfj lasvm-banana.tar.bz2
    mv banana $(DATADIR)
    rm lasvm-banana.tar.bz2

download-covtype: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/covtype.libsvm.binary.scale.bz2
    bunzip2 covtype.libsvm.binary.scale.bz2
    mv covtype.libsvm.binary.scale $(DATADIR)

download-ijcnn: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/ijcnn1.tr.bz2
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/ijcnn1.t.bz2
    bunzip2 ijcnn1.tr.bz2
    bunzip2 ijcnn1.t.bz2
    mv ijcnn1* $(DATADIR)

download-real-sim: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/real-sim.bz2
    bunzip2 real-sim.bz2
    mv real-sim $(DATADIR)/realsim

download-reuters: datadir
    ./download.sh http://leon.bottou.org/_media/papers/lasvm-reuters.tar.bz2
    tar xvfj lasvm-reuters.tar.bz2
    mv reuters $(DATADIR)
    rm lasvm-reuters.tar.bz2

download-url: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/url_combined.bz2
    bunzip2 url_combined.bz2
    mv url_combined $(DATADIR)

download-waveform: datadir
    ./download.sh http://leon.bottou.org/_media/papers/lasvm-waveform.tar.bz2
    tar xvfj lasvm-waveform.tar.bz2
    mv waveform $(DATADIR)
    rm lasvm-waveform.tar.bz2

download-webspam: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/webspam_wc_normalized_unigram.svm.bz2
    bunzip2 webspam_wc_normalized_unigram.svm.bz2
    mv webspam_wc_normalized_unigram.svm $(DATADIR)/webspam

# multi-class

download-dna: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/dna.scale.tr
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/dna.scale.t
    mv dna* $(DATADIR)

download-letter: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/letter.scale.tr
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/letter.scale.t
    mv letter* $(DATADIR)

download-mnist: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/mnist.scale.bz2
    bunzip2 mnist.scale.bz2
    mv mnist.scale $(DATADIR)
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/mnist.scale.t.bz2
    bunzip2 mnist.scale.t.bz2
    mv mnist.scale.t $(DATADIR)

download-news20: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/news20.scale.bz2
    bunzip2 news20.scale.bz2
    mv news20.scale $(DATADIR)
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/news20.t.scale.bz2
    bunzip2 news20.t.scale.bz2
    mv news20.t.scale $(DATADIR)

download-pendigits: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/pendigits
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/pendigits.t
    mv pendigits* $(DATADIR)

download-protein: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/protein.tr.bz2
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/protein.t.bz2
    bunzip2 protein.tr.bz2
    bunzip2 protein.t.bz2
    mv protein* $(DATADIR)

download-rcv1: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/rcv1_train.multiclass.bz2
    bunzip2 rcv1_train.multiclass.bz2
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/rcv1_test.multiclass.bz2
    bunzip2 rcv1_test.multiclass.bz2
    mv rcv1* $(DATADIR)

download-satimage: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/satimage.scale.tr
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/satimage.scale.t
    mv satimage* $(DATADIR)

download-sector: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/sector/sector.scale.bz2
    bunzip2 sector.scale.bz2
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/sector/sector.t.scale.bz2
    bunzip2 sector.t.scale.bz2
    mv sector* $(DATADIR)

download-usps: datadir
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.bz2
    bunzip2 usps.bz2
    mv usps $(DATADIR)
    ./download.sh http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.t.bz2
    bunzip2 usps.t.bz2
    mv usps.t $(DATADIR)

在编译期间,我得到了这个输出,

compile options: '-I/home/is/anaconda/lib/python2.7/site-packages/numpy/core/include -I/home/is/lightning/lightning/impl/randomkit -I/home/is/anaconda/lib/python2.7/site-packages/numpy/core/include -I/home/is/anaconda/include/python2.7 -c'
c++: svrg_fast.cpp

我希望设置编译.c代码,因为我可以轻松地将.pyx转换为.c。或者是否可以将.pyx转换为C ++?

提前致谢!

1 个答案:

答案 0 :(得分:0)

刚刚找到了办法。

假设我正在对file.pyx进行更改,我可以通过运行将其编译为.cpp

cython --cplus file.pyx

然后,我只需通过运行

重新构建模块

sudo python setup.py develop

希望这有帮助!