本地打包,setup安装
一、distutils
使用:distutils 进行打包,步骤如下,以单一文件为例。
1、创建文件
在同一目录下。写一个foo.py
文件:
#-*- coding:utf-8 -*- def sum(*values): s = 0 for v in values: i = int(v) s = s + i print s def output(): print 'print something'
写一个setup.py
文件:
#-*- coding:utf-8 -*- from distutils.core import setup setup(name='foo', version='1.0', py_modules=['foo'], )
2、创建源码包
运行python setup.py sdist
可以为模块创建一个源码包,在当前目录下,创建dist
目录。
dist
里面有个文件名为foo-1.0.tar.gz
,这个就是可以分发的包。使用者拿到这个包后,解压。
3、安装脚本
可以在源码包foo-1.0
目录下执行:python setup.py install --record log
。
也可以直接在最开始的setup.py
处,执行:python setup.py install --record log
。
foo.py
就会被拷贝到python类路径下,可以被导入使用。
使用--record log
:会把安装的信息记录在log
下,然后在要卸载的时候,使用cat log | xargs rm -rf
就可以卸载。
二、setuptools
以模块(一个文件夹下多个py文件)为例。
1、创建文件
创建文件目录:
/example_pkg /example_pkg __init__.py setup.py LICENSE README.md
setup.py
文件:
# -*- coding:utf-8 -*- import setuptools with open("README.md", "r") as fh: long_description = fh.read() setuptools.setup( name="foo", version="0.0.1", author="Example Author", author_email="author@example.com", description="A small example package", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/pypa/sampleproject", packages=setuptools.find_packages(), # 指定需要安装的模块 # packages=find_packages(exclude=['contrib', 'docs', 'tests*']), # py_modules=["six"], # 剔除不属于包的单文件Python模块 # install_requires=['peppercorn'], # 指定项目最低限度需要运行的依赖项 # python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4', # python的依赖关系 # package_data={ # 'sample': ['package_data.dat'], # }, # 包数据,通常是与软件包实现密切相关的数据 classifiers=[ # How mature is this project? Common values are # 3 - Alpha # 4 - Beta # 5 - Production/Stable 'Development Status :: 3 - Alpha', # Indicate who your project is intended for 'Intended Audience :: Developers', 'Topic :: Software Development :: Build Tools', # Pick your license as you wish (should match "license" above) 'License :: OSI Approved :: MIT License', # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. 'Programming Language :: Python :: 2', # 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', # 'Programming Language :: Python :: 3', # 'Programming Language :: Python :: 3.2', # 'Programming Language :: Python :: 3.3', # 'Programming Language :: Python :: 3.4', "Operating System :: OS Independent", ], )
name:包的名字
version:版本号
author,author_email:作者信息
description:是一个简短的,一句话的包的总结
long_description:是包的详细说明。这显示在Python Package Index的包详细信息包中。在这种情况下,加载长描述README.md是一种常见模式
long_description_content_type:索引什么类型的标记用于长描述。在这种情况下,它是Markdown
url:项目主页的URL
packages:自动发现所有包和子包,而不是手动列出每个包。在这种情况下,包列表将是example_pkg,因为它是唯一存在的包。
创建README.md
:
# Example Package This is a simple example package. You can use [Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content.
创建LICENSE
:
上传到Python Package Index的每个包都包含许可证,这一点很重要。这告诉用户安装您的软件包可以使用您的软件包的条款。有关选择许可证的帮助,请参阅 https://choosealicense.com/。选择许可证后,打开 LICENSE并输入许可证文本。例如,如果您选择了MIT许可证:
Copyright (c) 2018 The Python Packaging Authority Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2、安装脚本
安装:python setup.py install --record log
。
卸载:cat log | xargs rm -rf
简单例子:
https://github.com/longgb246/lgblearn/blob/master/setup.py
相关参考:
https://packaging.python.org/tutorials/packaging-projects/
https://packaging.python.org/guides/distributing-packages-using-setuptools/