简体   繁体   English

distutils.core与带有C ++扩展名的setuptools

[英]distutils.core vs setuptools with c++ extension

I am creating a python package with a c++ extension. 我正在创建一个具有c ++扩展名的python包。 I am trying to do this with setuptools as that seems to be the preferred solution. 我正在尝试使用setuptools进行此操作,因为这似乎是首选的解决方案。 Given that it allows for setup_requires, install_requires, I agree and use it for my python only packages. 鉴于它允许setup_requires,install_requires,我同意并将其用于仅python的软件包。 However, I am unable to get it to work when I have a c++ extension module. 但是,当我有c ++扩展模块时,我无法使其工作。 Then I resort to distutils.core to get it to work. 然后,我诉诸distutils.core使其开始工作。 I would like to know how to get it to work using setuptools. 我想知道如何使用setuptools使其工作。 My setup script looks like 我的安装脚本看起来像

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}
    )

This results in a package_py-0.0.1-py3.6.egg file in my python site-packages. 这导致在我的python站点软件包中的package_py-0.0.1-py3.6.egg文件。 The package only works when used from the installation folder. 该软件包在从安装文件夹使用时才有效。

Changing the first line to use distutils.core instead of setuptools 更改第一行以使用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}
    )

I get a package_py folder (with .so file) and package_py-0.0.1-py3.6.egg-info in site-packages. 我在站点软件包中得到一个package_py文件夹(带有.so文件)和package_py-0.0.1-py3.6.egg-info。 Now the module works in all folders. 现在,该模块可在所有文件夹中使用。

As I would like to extend the python package to also use setup_requires, instal_requires I want to really use setuptools. 因为我想扩展python包以也使用setup_requires,所以instal_requires我想真正使用setuptools。 How can I get the package to work in all folders using setuptools instead of distutils.core 如何使用setuptools而不是distutils.core使软件包在所有文件夹中工作

If you're using a C extension, you should use Extension from either setuptools or distutils as well, it's not a python package, so I'm surprised it's even managing to install. 如果您使用的是C扩展名,则还应该使用setuptoolsdistutils Extension ,它不是python软件包,因此令我惊讶的是它甚至可以安装。 If you plan on distributing the package to others, you shouldn't compile the extension beforehand, but have it compiled during the package installation so that the user's system compiles it appropriately (ie, an .so file doesn't help a Windows user who needs it compiled into a .dll file, etc). 如果打算将软件包分发给其他人,则不应该事先编译该扩展名,而应在软件包安装过程中对其进行编译,以使用户系统正确地编译该扩展名(即, .so文件对Windows用户不起作用。需要将其编译成.dll文件等)。

Try something like this: 尝试这样的事情:

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']
    )

Hope this helps or at least gets you on the right track. 希望这有助于或至少使您走上正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM