python的打包方法

setuptools

当对python的模块进行打包时,需要写setup.py的文件描述包的相关信息,变成一种可以安装的文件。python的安装文件一般是whl文件。一般是用到两个库setuptools和wheel。

pip install setuptools wheel

其中,要进行打包,其库文件的结构如下:
库文件结构
我的文件格式为
文件

首先在setup.py中配置相关信息,

from setuptools import setup,find_packages
setup(
    name='mypage',
    author='mm',
    author_email='xx@qq.com',
    url='',
    packages=find_packages()
)

使用命令行运行

python setup.py <command>

其中command常见的包括:

  • install :安装包,将包的代码、依赖以及其他必要条件安装到python环境中。
  • develop:在开发模式下安装,会创建一个指向包源代码的符号,而不是复制源代码到python环境中。
  • sdist:创建源代码分发包,一般是tar文件
  • bdist:创建二进制分发包,一般是编译后与特定平台相关的二进制文件,安装更快
  • test:自动化运行包的测试文件
  • bdist_wheel:创建wheel格式的二进制分发包,更好的兼容性
  • bdist_dmg,bdist_msi,bdist_rpm:分别表示三种操作系统环境
  • alias:给包起别名
  • upload:上传到PyPI索引

这里有一个常见的setup模板:

from setuptools import setup, find_packages

with open('README.md') as readme_file:
    readme = readme_file.read()
setup_requirements = ['pytest-runner']

setup(
    author="anonymous",
    author_email='anonymous@anonymous.org',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
    ],
    description="Temporal label smoothing for ICU data.",
    entry_points={
        "console_scripts": ['tls = tls.run:main']   
    },
    install_requires=[],  # dependencies managed via conda for the moment
    license="MIT license",
    long_description=readme,
    include_package_data=True,
    keywords='tls',
    name='tls',
    packages=find_packages(include=['tls']),
    setup_requires=setup_requirements,
    test_suite='tests',
    tests_require=[],
    url='',
    version='1.0.0',
    zip_safe=False,
)
  • entry_points:如果用户安装包以后,setuptools会自动生成一个可执行的文件,并且放置到用户的path中,使用tls命令就可以直接执行tls.run:main函数,更好地将自己的包封装成命令行工具。
  • setup_requires:安装其他包所需要的其他python包,在安装的过程中使用,安装后就不需要了
  • install_requires:安装的依赖包
  • classifiers:用于pypi查找
  • include_package_data:是否包含包内的非代码文件。
  • find_packages:找到模块中所有包
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值