2017-09-06 3 views
1

C 함수가있는 서브 패키지가 포함 된 패키지를 빌드하는 데 문제가 있습니다. 그것을 설명하기 위해 테스트 케이스를 만들었습니다.서브 디렉토리에서 C 함수를 호출하는 패키지

디렉토리 트리입니다

여기
README.rst 
MANIFEST.in 
setup.py 
testcase/ 
    get_triple.py 
    __init__.py 
    spin/ 
    __init__.py 
    setup.py 
    spin.c 
    spin.h 

이 파일의 내용입니다. testcase 디렉토리에서 첫 번째.

get_triple.py

:

import spin 
print "here is the spin dir" 
print dir(spin) 

def get_triple(number): 

    """ triple that number with a C func """ 

    new_number = spin.make_that_triple(number) 

    return new_number 

__init__.py :

from .get_triple import * 

setup.py :

from setuptools import setup 

def readme(): 
    with open('README.rst') as f: 
     return f.read() 

setup(name='testcase', 
    version='0.1', 
    description='test the C function called from a package', 
    classifiers=[ 
     'Development Status :: 0 - Alpha', 
     'License :: Friand_Dur', 
     'Programming Language :: Python :: 2.7', 
     'Topic :: Scientific/Engineering :: Bio-Informatics', 
    ], 
    author='test', 
    author_email='test', 
    license='test', 
    package_dir = { 
     'testcase': 'testcase', 
     'testcase.spin': 'testcase/spin', 
    }, 
    packages=['testcase', 'testcase.spin'], 
    install_requires=[ 
     # nothing, it's a test 
    ], 
    zip_safe=False) 

그리고 스핀 디렉토리에서

.

spin.c

:

#include <stdio.h> 
#include <stdlib.h> 
#include <python2.7/Python.h> 
#include <math.h> 
#define PY_ARRAY_UNIQUE_SYMBOL get_spinangle_traj_ARRAY_API 
/* This one makes the compilation crash if not commented, despite the warning message if it is */ 
//~ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 
#include "numpy/arrayobject.h" 
/* Not typecasting anything */ 
#include "spin.h" 

/* Docstring */ 
static char module_docstring[] = 
    "triple using C"; 
static char make_that_triple_docstring[] = 
    "triple using C"; 

static PyObject *make_that_triple(PyObject *self, PyObject *args){ 

    /* Just get a number */ 
    int number; 
    double tripled_number = 0.0; 

    /* Should get the python objects */ 
    if (!PyArg_ParseTuple(args,"i",&number)){ 
     PyErr_SetString(PyExc_ValueError,"Error while parsing the trajectory coordinates in get_spinangle_traj"); 
     return NULL; 
    } 

    /* Call the external C function to get the triple */ 
    tripled_number = tripleeee(number); 

    PyObject *ret = Py_BuildValue("d", tripled_number); 
    return ret; 
} 

double tripleeee(int number){ 

    return number * 3; 

} 

static PyMethodDef module_methods[] = { 
    {"make_that_triple",make_that_triple,METH_VARARGS,make_that_triple_docstring}, 
    {NULL,NULL,0,NULL} 
}; 

PyMODINIT_FUNC initspin(void) 
{ 
    PyObject *m = Py_InitModule3("spin",module_methods,module_docstring); 
    if (m == NULL) 
     return; 
    /* Load Numpy */ 
    import_array(); 
} 

spin.h :

static PyObject *make_that_triple(PyObject *self, PyObject *args); 
double tripleeee(int number); 

setup.py :

from distutils.core import setup, Extension 
import numpy.distutils.misc_util 

setup(
    ext_modules=[Extension("spin", ["spin.c"])], 
    include_dirs=numpy.distutils.misc_util.get_numpy_include_dirs(), 
) 

그리고 __init__.py가 비어 있습니다.

우선 spin 하위 디렉토리에 python setup.py build_ext --inplace 서브 디렉토리를 가지고 C 패키지를 컴파일 한 다음 sudo python setup.py install을 사용하는 메인 패키지를 컴파일했다. ... 작동하지 않습니다

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import testcase 
print "here is the testcase dir" 
print dir(testcase) 

# Let's multiply this by 3 
number = 25 

new_number = testcase.get_triple(number) 

print new_number 

:

here is the spin dir 
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__'] 
here is the testcase dir 
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'get_triple', 'spin'] 
Traceback (most recent call last): 
    File "./spindihed_totable_pl60.py", line 17, in <module> 
    new_number = testcase.get_triple(number) 
    File "/usr/local/lib/python2.7/dist-packages/testcase-0.1-py2.7.egg/testcase/get_triple.py", line 9, in get_triple 
    new_number = spin.make_that_triple(number) 
AttributeError: 'module' object has no attribute 'make_that_triple' 

그것은 비록 testcase 패키지에서 spin 내에서 C 기능에 액세스 할 수 없습니다

는 또한 테스트 스크립트를 그 안에 spin이 존재하며, 나는이 단계에서 잠시 머물러있었습니다.

>>> import spin 
>>> print spin.make_that_triple(3) 
9.0 

나는 무엇을 그리워 않은 : 나는 spin 하위 디렉토리에 cd, 그러나 나는 그것이 C의 문제가 아니에요 말해 보인다 C의 기능을 사용할 수 있습니까? 내가 놓친 매우 기본적인 것이 있다고 생각합니다. 나는 또한 시험을 쉽게하기 위해 자식에 모든 것을 넣어 :

답변

1

당신은 여기에 몇 가지 문제를 가지고 당신의 도움을 https://github.com/msidore/testcase

감사합니다. --inplace을 빌드 한 후에는 testcase/spin/spin에 확장 모듈을 만들었습니다.그래서, 해당 모듈 있도록 testcase.spin.spin입니다 : 최상위 setup.py이 확장 모듈에 대해 알고하지 않기 때문에

>>> import testcase.spin.spin 
>>> dir(testcase.spin.spin) 
['__doc__', '__file__', '__name__', '__package__', 'make_that_triple'] 

그러나

, 그것은 경우 python setup.py install를 설치되지 않습니다. 내가 할 수있는 장소에서 이러한 변경으로

from setuptools import setup, Extension 
import numpy.distutils.misc_util 

setup(name='testcase', 
     packages=['testcase'], 
     ext_modules=[Extension("testcase.spin", ["testcase/spin.c"])], 
     include_dirs=numpy.distutils.misc_util.get_numpy_include_dirs(), 
     zip_safe=False) 

:

setup.py 
testcase/get_triple.py 
testcase/__init__.py 
testcase/spin.c 
testcase/spin.h 

을 그리고 그것은 확장 모듈에 대해 알고 setup.py 있도록 최고 수준을 수정 :이 같은 일을-배열을 다시 제안

virtualenv .venv 
. .venv/bin/activate 
pip install numpy 
pip install . 
python testscript.py 

그리고 testscript.py에서 출력으로 얻을 :

here is the spin dir 
['__doc__', '__file__', '__name__', '__package__', 'make_that_triple'] 
here is the testcase dir 
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'get_triple', 'spin'] 
75.0 
+0

고맙습니다. 나는 나의 문제를 이해하고 있다고 생각한다. (길을 따라 이상한 일들을 만들어 낸다. 나는 일하기 전에 수동으로 제거해야한다.) – Marlon

관련 문제