想让自己写的python库可以使用pip install xxx
安装。
setuptools
和twinw
pip install setuptools
pip install twine # 简化将库发布到PYPI流程的工具
pip install wheel # 制作whl文件需要
参考python项目打包结构
|--mypackage
__init__.py
__version__.py
|--common
|--config
|--src
setup.py
requirements.txt
LICENSE
README.rst
MANIFEST.in
.gitnore
目录文件的作用:
include DrissionPage/configs/configs.ini
include DrissionPage/*.pyi
include DrissionPage/*/*.py
include DrissionPage/*/*.pyi
# -*- coding:utf-8 -*-
from setuptools import setup, find_packages
with open("README.md", "r", encoding='utf-8') as fh:
long_description = fh.read()
setup(
name="DrissionPage",
version="3.2.31",
author="g1879",
author_email="[email protected]",
description="Python based web automation tool. It can control the browser and send and receive data packets.",
long_description=long_description,
long_description_content_type="text/markdown",
license="BSD",
keywords="DrissionPage",
url="https://gitee.com/g1879/DrissionPage",
include_package_data=True,
packages=find_packages(),
zip_safe=False,
install_requires=[
'lxml',
'requests',
'cssselect',
'DownloadKit>=1.0.0',
'FlowViewer>=0.3.0',
'websocket-client',
'click',
'tldextract'
],
classifiers=[
"Programming Language :: Python :: 3.6",
"Development Status :: 4 - Beta",
"Topic :: Utilities",
"License :: OSI Approved :: BSD License",
],
python_requires='>=3.6',
entry_points={
'console_scripts': [
'dp = DrissionPage.commons.cli:main',
],
},
)
字段说明:
version:这个简单,就是包的发布的版本,可以直接写在这,也可以从其他地方引用过来。
long_description:默认是rst(reStructuredText )格式的,也可以是其他格式,使用long_description_content_type
字段指定,这个里面的内容是显示在pypi包首页上。
packages:申明你的包里面要包含的目录,比如 [‘mypackage’, ‘mypackage_test’] 可以是这种使用我的示例,让setuptools自动决定要包含哪些包
install_requires:申明依赖包,安装包时pip会自动安装
LICENSE
BSD 3-Clause License
Copyright (c) 2020, g1879
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
项目简介,markdown格式
项目按照上述结构组织好之后,就可以进行打包了
python setup.py check
若没问题,无报错信息。
python setup.py bdist_wheel # 编译生成wheel文件,会产生build,dist,XX.egg-info三个文件夹
pip wheel --wheel-dir=whl文件保存路径 setup.py文件所在路径 # 直接生成whl文件,不产生中间编译结果
python setup.py sdist # 生成tar包,即源码包
python setup.py sdist upload # 上传source包
python setup.py bdist_wheel upload # 上传二进制包
# 源码包安装,解压+编译+安装
python setup.py build
python setup.py install
# whl包
pip install XX.whl
python setup.py develop
# 该方法不会真正的安装包,而是在系统环境中创建一个软链接指向包实际所在目录。
# 这边在修改包之后不用再安装就能生效,便于调试