如何制作包含DLL的Python安装包并发布到PyPi

admin 2019-9-9 13834

本文来源:https://blog.csdn.net/yushulx/article/details/80491904

----------------------


当我们用C++给Python编写了扩展,可以通过下面的命令来编译和安装:

python setup.py build install

这个时候打开Python27\Lib\site-packages目录,就会看到一个.pyd文件和依赖的DLL文件。如果你通过pip安装了其它的包,你会看到它们都是有目录的。这篇文章要分享下如何把pyd和DLL文件一起打包,发布,以及通过pip来安装。


工具

安装setuptools, wheel, and twine:

python -m pip install setuptools wheel twine


使用Wheel打包Dynamsoft Barcode Reader


新建一个目录。 

在里面创建一个dbr的文件夹。dbr就是包名。 

在dbr的文件夹里放入我编译好的dbr.pyd和和依赖的DynamsoftBarcodeReaderx64.dll文件。另外还要包含一个初始化文件__init__.py:

from .dbr import *


在根目录创建MANIFEST.IN文件:

# Include the README
include *.md
# Include the license file
include LICENSE.txt


创建setup.cfg文件:

[metadata]
license_file = LICENSE.txt
[bdist_wheel]
universal = 0
python-tag = cp27

universal为0表示这是一个特定平台的安装包。使用python-tag来指定Python的版本。 


创建setup.py:

from setuptools import setup, find_packages, Distribution
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

class BinaryDistribution(Distribution):
    """Distribution which always forces a binary package with platform name"""
    def has_ext_modules(foo):
        return True

setup(
    name='dbr',  # Required
    version='6.0.0',  # Required
    description='Dynamsoft Barcode Reader Python project',  # Required
    long_description=long_description,  # Optional
    long_description_content_type='text/markdown',  # Optional (see note above)
    url='https://www.dynamsoft.com/Products/Dynamic-Barcode-Reader.aspx',  # Optional
    license = 'https://www.dynamsoft.com/Products/barcode-reader-license-agreement.aspx',
    author='Dynamsoft',  # Optional
    author_email='support@dynamsoft.com',  # Optional
    classifiers=[  # Optional
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Libraries',
        'License :: Other/Proprietary License',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
    ],
    keywords='barcode DataMatrix QRCode 1D PDF417',  # Optional
    packages=find_packages(exclude=['contrib', 'docs', 'tests']),  # Required
    install_requires=['numpy', 'opencv-python'],  # Optional
    package_data={  # Optional
        'dbr': ['DynamsoftBarcodeReaderx64.dll', 'dbr.pyd'],
    },
    distclass=BinaryDistribution
)


执行打包命令:

python setup.py bdist_wheel --plat-name win_amd64

这个时候会生成名为dbr-6.0.0-cp27-cp27m-win_amd64.whl的文件。

 

通过命令安装:

pip install dbr-6.0.0-cp27-cp27m-win_amd64.whl

包被安装到Python27\Lib\site-packages\dbr。 


在工程中使用:

import dbr


通过PyPi发布:

创建一个PyPi账号。 


用twine发布:

twine upload dist/*


登陆站点就可以看到自己发布的安装包。 


现在用pip来安装:

pip install dbr


源码

https://github.com/dynamsoft-dbr/pypi-wheel


最新回复 (0)
返回