我正在创建一个具有c ++扩展名的python包。我正在尝试使用setuptools进行此操作,因为这似乎是首选的解决方案。鉴于它允许setup_requires,install_requires,我同意并将其用于仅python的软件包。但是,当我有c ++扩展模块时,我无法使其工作。然后,我诉诸distutils.core使其开始工作。我想知道如何使用setuptools使其工作。我的安装脚本看起来像

from setuptools import setup
import shutil
import os

# folder where .so is being build by cpp compilation
so_src = os.path.join(dir, 'cpp/build/')
# folder where .so should live in python package
so_des = os.path.join(dir, 'package_py/cpp/')

# extension module
lib_files = ['cpp_py.so']

# copy shared lib
for f in lib_files:
  shutil.copyfile(so_src + f, so_des + f)

# set-up script
setup(
      name=DISTNAME
    , version=FULLVERSION
    , description= DESCRIPTION
    , author= AUTHOR
    , author_email= EMAIL
    , maintainer= AUTHOR
    , maintainer_email= EMAIL
    , long_description=LONG_DESCRIPTION
    , packages=['package_py',
            'package_py.cpp']
    , package_dir={'package_py.cpp': 'package_py/cpp'}
    , package_data={'package_py.cpp': lib_files}
    )


这导致在我的python站点软件包中的package_py-0.0.1-py3.6.egg文件。该软件包仅在从安装文件夹使用时才有效。

更改第一行以使用distutils.core代替setuptools

from distutils.core import setup # only change, remainder is the same!!
import shutil
import os

# folder where .so is being build by cpp compilation
so_src = os.path.join(dir, 'cpp/build/')
# folder where .so should live in python package
so_des = os.path.join(dir, 'package_py/cpp/')

# extension module
lib_files = ['cpp_py.so']

# copy shared lib
for f in lib_files:
  shutil.copyfile(so_src + f, so_des + f)

# set-up script
setup(
      name=DISTNAME
    , version=FULLVERSION
    , description= DESCRIPTION
    , author= AUTHOR
    , author_email= EMAIL
    , maintainer= AUTHOR
    , maintainer_email= EMAIL
    , long_description=LONG_DESCRIPTION
    , packages=['package_py',
            'package_py.cpp']
    , package_dir={'package_py.cpp': 'package_py/cpp'}
    , package_data={'package_py.cpp': lib_files}
    )


我在站点软件包中得到一个package_py文件夹(带有.so文件)和package_py-0.0.1-py3.6.egg-info。现在,该模块可在所有文件夹中使用。

由于我想扩展python包以也使用setup_requires,所以instal_requires我想真正使用setuptools。如何使用setuptools而不是distutils.core使软件包在所有文件夹中工作

最佳答案

如果您使用的是C扩展名,则还应同时使用Extensionsetuptools中的distutils,它不是python软件包,因此令我惊讶的是它甚至设法安装了。如果计划将软件包分发给其他人,则不应该事先编译该扩展,而应在软件包安装期间对其进行编译,以使用户系统正确编译该扩展(即.so文件对Windows用户没有帮助)。谁需要将其编译为.dll文件等)。

尝试这样的事情:

from setuptools import setup, Extension
import shutil
import os

# I don't know how you want to build your extension or your file structure,
# so removing the build stuff.

your_modulename=Extension('_extensionname',
                          sources=['path/to/extension.cpp', 'more/file/paths'],
                          language='c'
                         )


# set-up script
setup(
      name=DISTNAME
    , version=FULLVERSION
    , description= DESCRIPTION
    , author= AUTHOR
    , author_email= EMAIL
    , maintainer= AUTHOR
    , maintainer_email= EMAIL
    , long_description=LONG_DESCRIPTION
    , ext_modules=[your_modulename]
    , packages=['package_py']
    )


希望这有助于或至少使您走上正确的道路。

关于python - distutils.core与带有C++扩展名的setuptools,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44323474/

10-13 05:04