本地打包,setup安装
一、distutils
使用:distutils 进行打包,步骤如下,以单一文件为例。
在同一目录下。写一个foo.py文件: 1、创建文件
def sum(*values):
s = 0
for v in values:
i = int(v)
s = s + i
print s
def output():
print ('print something')
写一个setup.py文件:
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文件:
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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(),
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
"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:
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
2、安装脚本
安装:python setup.py install --record log。
卸载:cat log | xargs rm -rf
|