我制作了一个需要执行C函数的Python应用程序。为此,我使用gcc在共享库中编译了C函数,并使用ctypes在我的Python脚本中调用了库。
我试图将我的应用程序打包到一个pip包中,但是在pip install上找不到创建共享库的方法。
我尝试了以下(setup.py):
from setuptools import setup
from setuptools.command.install import install
import subprocess
class compileLibrary(install):
def run(self):
install.run(self)
command = "cd packageName"
command += " && git clone https://mygit.com/myAwesomeCLibrary.git"
command += " && gcc -my -many -options"
process = subprocess.Popen(command, shell=True)
process.wait()
setup(
name='packageName',
version='0.1',
packages=['packageName'],
install_requires=[
...
],
cmdclass={'install': compileLibrary},
)
这在执行python setup.py install
时有效,但是当从pip安装时,在安装过程成功时,共享库不在包文件夹(pythonx.x/site-packages/packageName
)中。
我尝试使用具有相同结果的distutils。
根据问题Run Makefile on pip install,我还尝试了以下setup.py:
from setuptools import setup
from distutils.command.build import build
import subprocess
class Build(build):
def run(self):
command = "cd packageName"
command += " && git clone https://mygit.com/myAwesomeCLibrary.git"
command += " && gcc -my -many -options"
process = subprocess.Popen(command, shell=True)
process.wait()
build.run(self)
setup(
name='packageName',
version='0.1',
packages=['packageName'],
install_requires=[
...
],
cmdclass={'build': Build},
)
这是我的包的架构
packageName/
├── packageName/
│ ├── __init__.py
│ ├── myFunctions.c
├── MANIFEST.in
├── README.md
├── setup.py
注意:使用pip安装软件包后,我在安装文件夹(__init__.py
)中只有__pycache__
和pythonx.x/site-packages/packageName
。
如何在pip安装期间创建共享库,以便我的包可以使用它?
答案 0 :(得分:1)
经过数小时的研究,我找到了解决方案。主要问题是:
Extension
中的setuptools
来编译库git clone
并在结尾调用构造函数 ,以便在编译之前发生git clone
。这是工作setup.py
:
from setuptools import setup, Extension
from setuptools.command.install import install
import subprocess
import os
class CustomInstall(install):
def run(self):
command = "git clone https://mygit.com/myAwesomeCLibrary.git"
process = subprocess.Popen(command, shell=True, cwd="packageName")
process.wait()
install.run(self)
module = Extension('packageName.library',
sources = ['packageName/library.c'],
include_dirs = ['packageName/include'],
extra_compile_args=['-fPIC'])
setup(
name='packageName',
version='0.1',
packages=['packageName'],
install_requires=[
...
],
cmdclass={'install': CustomInstall},
include_package_data=True,
ext_modules=[module],
)
答案 1 :(得分:1)
您可以在MANIFEST.in中添加共享库。
像这样:
include *.so *.dylib