python Setuptools

what is setuptools

Setuptools is a fully-featured, actively-maintained, and stable library designed to facilitate packaging Python projects, where packaging includes:

  • Python package and module definitions
  • Distribution package metadata
  • Test hooks
  • Project installation
  • Platform-specific details
  • Python 3 support

install setuptools

examples

i will use a simple example to show how setuptools work

  • Create new directory
mkdir demo 
cd demo
  • Create setup files
from setuptools import setup
package = 'name'
version = '0.1'
setup(name=package,
     version=version,
     description="description",
     url='url')
  • variables
    • name is package name
    • version is package current version
    • description is pacckage description

then run
python setup.py bdist_egg
to build python-eggs for python test, you will find a eggs file in demo/dist directory. if you look into this eggs file you will find out that it is a zip file

cli:~/demo$ file dist/name-0.1-py2.7.egg 
dist/name-0.1-py2.7.egg: Zip archive data, at least v2.0 to extract
cli:~/demo$ unzip -l dist/name-0.1-py2.7.egg 
Archive:  dist/name-0.1-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
        1  2016-10-17 11:17   EGG-INFO/dependency_links.txt
      176  2016-10-17 11:17   EGG-INFO/PKG-INFO
      120  2016-10-17 11:17   EGG-INFO/SOURCES.txt
        1  2016-10-17 11:17   EGG-INFO/zip-safe
        1  2016-10-17 11:17   EGG-INFO/top_level.txt
---------                     -------
      299                     5 files
  • add some funtion into package
mkdir demo 
cd demo
touch __init__.py

put these code into __init__.py

def test():
    print "hello world!"  

if __name__ == '__main__':
    test()

and then run python setup.py bdist_eggs again

cli:~/demo$ unzip -l dist/name-0.1-py2.7.egg 
Archive:  dist/name-0.1-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
        1  2016-10-17 11:17   EGG-INFO/dependency_links.txt
      176  2016-10-17 11:17   EGG-INFO/PKG-INFO
      120  2016-10-17 11:17   EGG-INFO/SOURCES.txt
        1  2016-10-17 11:17   EGG-INFO/zip-safe
        1  2016-10-17 11:17   EGG-INFO/top_level.txt
---------                     -------
      299                     5 files

nothing changed becasue you haven’t added search directory option into setup.py

edit setup.py like below

from setuptools import setup,find_packages
package = 'name'
version = '0.1'
setup(name=package,
      version=version,
      description="description",
      packages=find_packages(),
      url='url')

resetup it again
then you will find something changed

cli:~/demo$ unzip -l dist/name-0.1-py2.7.egg 
Archive:  dist/name-0.1-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
      137  2016-10-17 11:33   EGG-INFO/SOURCES.txt
      176  2016-10-17 11:33   EGG-INFO/PKG-INFO
        1  2016-10-17 11:33   EGG-INFO/dependency_links.txt
        5  2016-10-17 11:33   EGG-INFO/top_level.txt
        1  2016-10-17 11:33   EGG-INFO/zip-safe
       75  2016-10-17 11:30   demo/__init__.py
      341  2016-10-17 11:33   demo/__init__.pyc
---------                     -------
      736                     7 files

we have __init__.py in our package now
now install it using python setup.py install, and import it in python

>>> import demo
>>> demo.test()
hello world
>>> 

great it works now

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值