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:找到模块中所有包