美文网首页
python-setup模块

python-setup模块

作者: longgb246 | 来源:发表于2018-11-25 11:08 被阅读0次

本地打包,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

因为暂时没有需求,放到pypi上,所以没有研究这块。

简单例子:
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/

pass

相关文章

  • python-setup模块

    本地打包,setup安装 一、distutils 使用:distutils 进行打包,步骤如下,以单一文件为例。 ...

  • Python-setup环境变量&os模块

    一、timeit包(上接连载9) 1.我们对于timeit函数,可采取如下例子: 释义:setup负责把环境变量准...

  • python常用模块!!

    os模块: stat模块: sys模块: hashlib,md5模块: random模块: types模块: at...

  • 2018-08-19

    Angular 2 技能图谱 模块 自定义模块 根模块 特性模块 共享模块 核心模块 内置模块 Applicati...

  • 【时间管理100讲】精髓全在这里啦

    理论模块 精力管理。 行动管理。 学习模块。 高空模块。 反思模块。 运动模块。 阅读模块。 旅行模块。 人际关系...

  • python基础学习(三)

    常用模块 String模块 数学模块 随机模块 OS模块 os.path模块 re模块 常用函数及操作 列表操作 ...

  • day10-异常处理和pygame显示

    一、异常处理 1.模块 导入模块(自定义模块,第三方模块)import 模块 ---->模块.内容from 模块 ...

  • 重点知识复习(异常处理)

    1.模块 导入模块(自定义模块,第三方模块,系统其他模块)import 模块 ----> 模块.内容from 模...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • nodejs-模块

    nodejs模块 一、nodejs模块分类 1.核心模块 Core Module、内置模块、原生模块 fs模块 p...

网友评论

      本文标题:python-setup模块

      本文链接:https://www.haomeiwen.com/subject/xrcpqqtx.html