python 自定义安装包

版权声明:本文为博主原创文章,欢迎转载,但请注明原文出处。 https://blog.csdn.net/GiveMeFive_Y/article/details/79488008

1.编写setup.py文件

from distutils.core import setup

setup(name = 'mytest',
      version = '1.1',
      py_modules = ['mytest'],
      install_requires=['arrow>=0.10.0'],
     )

setup.py各参数介绍:

–name 包名称
–version (-V) 包版本
–author 程序的作者
–author_email 程序的作者的邮箱地址
–maintainer 维护者
–maintainer_email 维护者的邮箱地址
–url 程序的官网地址
–license 程序的授权信息
–description 程序的简单描述
–long_description 程序的详细描述
–platforms 程序适用的软件平台列表
–classifiers 程序的所属分类列表
–keywords 程序的关键字列表
–packages 需要处理的包目录(包含init.py的文件夹)
–py_modules 需要打包的python文件列表
–download_url 程序的下载地址
–cmdclass
–data_files 打包时需要打包的数据文件,如图片,配置文件等
–scripts 安装时需要执行的脚步列表
–package_dir 告诉setuptools哪些目录下的文件被映射到哪个源码包。一个例子:package_dir = {”: ‘lib’},表示“root package”中的模块都在lib 目录中。
–requires 定义依赖哪些模块
–provides定义可以为哪些模块提供依赖
–find_packages() 对于简单工程来说,手动增加packages参数很容易,刚刚我们用到了这个函数,它默认在和setup.py同一目录下搜索各个含有 init.py的包。

2.编写源文件mytest.py

# coding: utf-8
#!/usr/bin/python

import arrow

class Student():
    def __init__(self, birthday, sex):
        self.birthday = birthday
        self.sex = sex

    @property
    def age(self):
        return  (arrow.now() - arrow.get(self.birthday)).days//365

    @staticmethod
    def say(words):
        print(words)

    @classmethod
    def upper(cls, birthday, sex):
        return cls(birthday, sex.upper())

    def __str__(self):
        return str(id(self))

if __name__ == '__main__':
    k = Student.upper("1999.9.9", "man")

3. 生成安装包

$python setup.py sdist

可以看到当前目录结构:
├── dist
│   └── mytest-1.1.tar.gz
├── MANIFEST
├── mytest.py
├── pycache
│   └── mytest.cpython-35.pyc
└── setup.py

4. 安装模块

$pip3 install mytest-1.1.tar.gz
或者:
$tar -xvf mytest-1.1.tar.gz
$cd mytest-1.1
$python setup.py install

5.测试

(test) [root@dev dist]#python 
Python 3.5.3 (default, Jan 17 2017, 14:36:19) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from mytest import Student
>>> k = Student("2000.1.1", "Man")
>>> k.age
18
>>> k
<mytest.Student object at 0x7effe0ebebe0>
>>> print(k)
139637455317984
>>> k.sex
'Man'
>>> k1 = Student.upper("2000.1.1", "Man")
>>> k1.sex
'MAN'
>>> k.say("finished!")
finished!

猜你喜欢

转载自blog.csdn.net/GiveMeFive_Y/article/details/79488008