我正在创建一个带有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 site-packages中的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}
)
我在site-packages中获得了package_py文件夹(包含.so文件)和package_py-0.0.1-py3.6.egg-info。现在该模块适用于所有文件夹。
因为我想扩展python包也使用setup_requires,instal_requires我想真正使用setuptools。如何使用setuptools而不是distutils.core
使程序包在所有文件夹中工作答案 0 :(得分:2)
如果你正在使用C扩展,你应该使用Extension
或setuptools
中的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']
)
希望这有助于或至少让您走上正轨。