zoukankan      html  css  js  c++  java
  • (转)python 模块安装包 制作

    转自: http://testerhome.com/topics/539

    用过python的同学对于python setup.py install肯定不会陌生。那么我们自己如果封装了很多的方法怎么很好的保存或者开源呢?模块安装包的制作是必不可少的。

    假设我们的模块叫做monkey。那么首先我们创建一个monkey.py。添加如下代码:

    # -*- coding: utf-8 -*-  
    class MyClass():
        def __init__(self):
            self.url = "http://www.testerhome.com"
        def printurl(self):
            print self.url

    然后创建一个setup.py,添加如下代码:

    # -*- coding: utf-8 -*-  
    from distutils.core import setup
    setup(name='test',
        version='1.0',
        description='MonkeyTest make first setup moudle',
        author='MonkeyTest',
        author_email='snowangelsiomn@gmail.com',
        url='http://www.testerhome.com',
        py_modules=['monkey'],
        )

    更多参数参考:

    将两个python文件放入一个文件夹,执行python setup.py sdist,然后会出现一个test-1.0.tar.gz的压缩包,显示如下log

    running sdist
    running check
    warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
    
    warning: sdist: standard file not found: should have one of README, README.txt
    
    writing manifest file 'MANIFEST'
    creating test-1.0
    making hard links in test-1.0...
    hard linking monkey.py -> test-1.0
    hard linking setup.py -> test-1.0
    Creating tar archive
    removing 'test-1.0' (and everything under it)

    解压缩之后可以看到文件夹中有一个setup.py,这个就是我们可以install的py文件。安装之后我们可以尝试

    >>> from monkey import MyClass
    >>> app=MyClass()
    >>> app.printurl()
    http://www.testerhome.com
  • 相关阅读:
    React: React的组件状态机制
    React: React的复合组件
    JavaScript:ES6的新特性
    React: 研究React的组件化
    React: 认识React
    CSS:CSS弹性盒子布局 Flexible Box
    iOS:应用程序扩展开发之Today扩展(Today Extesnsion)
    《逆向工程核心原理》
    《左手数据,右手图表》
    《设计模式之禅(第2版)》
  • 原文地址:https://www.cnblogs.com/zhang-pengcheng/p/4336203.html
Copyright © 2011-2022 走看看