Python 如何在setup.py中使用distutils或setuptools使cython扩展可导入(无需在每次导入前附加到sys.path)?

Python 如何在setup.py中使用distutils或setuptools使cython扩展可导入(无需在每次导入前附加到sys.path)?,python,cython,setuptools,setup.py,cythonize,Python,Cython,Setuptools,Setup.py,Cythonize,我有cython扩展,我用以下方式安装: from distutils.core import setup from Cython.Build import cythonize setup(ext_modules=cythonize( "package.pyx", language="c++") ) 当我要导入此包时,我需要使用以下命令将生成文件夹附加到路径: import sys sys.path.append(~/package/build/....) 在安装过程中需要

我有cython扩展,我用以下方式安装:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize(
    "package.pyx",
    language="c++")
)
当我要导入此包时,我需要使用以下命令将生成文件夹附加到路径:

import sys
sys.path.append(~/package/build/....)
在安装过程中需要更改哪些内容才能将模块安装到Linux中,并且不必附加到path即可导入


我也乐于使用setuptools。

尝试使用我的
setup.py
作为模板。。。这些事情并没有很好的记录。这里需要记住的一点是,如果您在原地构建
,您可能必须从projectname.module导入模块

try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

module = 'MyModuleName' # this assumes your .pyx and your import module have the same names
# ignore the below extra options if you don't need them (i.e. comment out `#`)
ext_modules = [Extension(module, sources=[module + ".pyx"],
              include_dirs=[],
              library_dirs=[], 
              extra_compile_args=[],
              language='c++')]

setup(
    name = module,
    ext_modules = ext_modules,
    cmdclass = {'build_ext': build_ext},
    include_dirs = [np.get_include(), os.path.join(np.get_include(), 'numpy')]
    )

python setup.py build\u ext--inplace
可能就是您想要的。