2017-06-02 63 views
0

我創建一個Python包與C++擴展setuptools的。我試圖用setuptools來做到這一點,因爲這似乎是首選解決方案。鑑於它允許setup_requires,install_requires,我同意並將其用於我的python only軟件包。但是,當我有一個C++擴展模塊時,我無法使它工作。然後我使用distutils.core來使其工作。我想知道如何使用setuptools來使用它。我的安裝腳本看起來像distutils.core VS與C++擴展

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-信息在網站-packages。現在該模塊在所有文件夾中工作。

,因爲我想延長Python包也使用setup_requires,instal_requires我想真正使用setuptools的。我怎樣才能獲得包使用setuptools的,而不是distutils.core

回答

0

如果您使用的是C擴展,你應該使用無論從setuptoolsdistutils以及Extension,它不是一個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'] 
    ) 

希望這有助於或至少可以讓你在正確的軌道上。