Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从sdist安装时,如何制作pip/setup.py生成扩展(build_ext)?_Python_Pip_Package_Cython_Setuptools - Fatal编程技术网

Python 从sdist安装时,如何制作pip/setup.py生成扩展(build_ext)?

Python 从sdist安装时,如何制作pip/setup.py生成扩展(build_ext)?,python,pip,package,cython,setuptools,Python,Pip,Package,Cython,Setuptools,我正在尝试创建一个sdist,我可以将它上传到PyPI,然后用户可以成功地将它安装到pip。正在讨论的项目是,我已经将一个sdist上载到PyPI()。文件是 这个项目使用Cython。根据,分发Cython代码的正确方法是捆绑cythonize生成的.c和.cpp文件,我目前正在这样做 问题在于pip install cubical和pip install path/to/sdist都安装了该软件包,但没有正确构建和链接扩展。也就是说,不会生成.so文件,因此安装的软件包无法导入其Cython

我正在尝试创建一个sdist,我可以将它上传到PyPI,然后用户可以成功地将它安装到pip。正在讨论的项目是,我已经将一个sdist上载到PyPI()。文件是

这个项目使用Cython。根据,分发Cython代码的正确方法是捆绑cythonize生成的.c和.cpp文件,我目前正在这样做

问题在于
pip install cubical
pip install path/to/sdist
都安装了该软件包,但没有正确构建和链接扩展。也就是说,不会生成.so文件,因此安装的软件包无法导入其Cython模块。我知道setup.py脚本在从源代码构建时可以工作(例如git克隆repo,对扩展进行cythonizing,并运行
pip安装-e CubiCal/

总之,如何更改setup.py脚本以确保从sdist安装时正确处理扩展

Setup.py:

import os
import sys
import glob

import cubical

from setuptools import setup, find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from setuptools import Command

with open('README.md') as f:
    long_description = f.read()

# Try get location of numpy headers. Compilation requires these headers. 

try:
    import numpy as np
except ImportError:
    include_path = ''
else:
    include_path = np.get_include()

# Use Cython if available.

try:
    from Cython.Build import cythonize
    import Cython.Compiler.Options as CCO
except ImportError:
    cythonize = None

cmpl_args = ['-ffast-math',
             '-O2', 
             '-march=native',  
             '-mtune=native', 
             '-ftree-vectorize' ]

cmpl_args_omp = cmpl_args + ['-fopenmp']

link_args = []

link_args_omp = link_args + ['-lgomp']

# which extensions need to compile through C++ rather than C
cpp_extensions = "cytf_plane", "cyf_slope", "cyt_slope", "rebinning"


class gocythonize(Command):
    """ Cythonise CubiCal kernels. """

    description = 'Cythonise CubiCal kernels.'

    user_options = [('force', 'f', 'Force cythonisation.'),]

    def initialize_options(self):
        pass

    def finalize_options(self):
        self.force = self.force or 0

    def run(self):

        CCO.buffer_max_dims = 9

        extensions = []

        for source in glob.glob("cubical/kernels/*.pyx"):
            name, ext = os.path.splitext(source)
            omp = name.endswith("_omp")
            # identify which kernels need to go via the C++ compiler
            cpp = any([x in name for x in cpp_extensions])

            extensions.append(
                Extension(name.replace("/","."), [source],
                          include_dirs=[include_path],
                          extra_compile_args=cmpl_args_omp if omp else cmpl_args,
                          extra_link_args=link_args_omp if omp else link_args,
                          language="c++" if cpp else "c"))

        cythonize(extensions, compiler_directives={'binding': True}, annotate=True, force=self.force)


extensions = []
for source in glob.glob("cubical/kernels/*.pyx"): 
    name, _ = os.path.splitext(source)
    is_cpp = any([s in name for s in cpp_extensions])
    is_omp = name.endswith("_omp")

    extensions.append(
        Extension(name.replace("/","."), [name + ".cpp" if is_cpp else name + ".c"],
                  include_dirs=[include_path],
                  extra_compile_args=cmpl_args_omp if is_omp else cmpl_args,
                  extra_link_args=link_args_omp if is_omp else link_args))

# Check for readthedocs environment variable.

on_rtd = os.environ.get('READTHEDOCS') == 'True'

if on_rtd:
    requirements = ['numpy', 
                    'cython', 
                    'futures', 
                    'matplotlib',
                    'scipy']
else:
    requirements = ['numpy', 
                    'futures', 
                    'python-casacore>=2.1.2', 
                    'sharedarray', 
                    'matplotlib',
                    'cython',
                    'scipy',
                    'astro-tigger-lsm']

setup(name='cubical',
      version=cubical.VERSION,
      description='Fast calibration implementation exploiting complex optimisation.',
      url='https://github.com/ratt-ru/CubiCal',
      classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
        "Operating System :: POSIX :: Linux",
        "Programming Language :: Python",
        "Topic :: Scientific/Engineering :: Astronomy"],
      author='Jonathan Kenyon',
      author_email='jonosken@gmail.com',
      license='GNU GPL v3',
      long_description=long_description,
      long_description_content_type='text/markdown',

      cmdclass={'build_ext': build_ext,
                'gocythonize': gocythonize},

      packages=['cubical', 
                'cubical.data_handler',
                'cubical.machines',
                'cubical.tools', 
                'cubical.kernels', 
                'cubical.plots',
                'cubical.database',
                'cubical.madmax'],
      install_requires=requirements,
      include_package_data=True,
      zip_safe=False,
      ext_modules = extensions,
      entry_points={'console_scripts': ['gocubical = cubical.main:main']},           
)