How to create a package with setup.py

本文详细介绍了如何使用Python的setup.py文件来打包和分发自己的模块,包括创建目录结构、编写setup.py文件、使用sdist和bdist命令创建源和构建发行版等步骤。
To pack and distribute your own python modules, you need to create a package with setup.py.
Ensure you have install setuptools package before we start this tutorial.

Here we will take dnsms module as example.

First of all, let's make the layout of the project like below:

  • dnsms_client/
    • dnsms/
    • examples/
    • ChangeLog
    • LICENSE
    • MANIFEST.in
    • README
    • setup.py

 

  • dnsms <directory>:  it contains all the modules we want to pack.

  • examples <directory>: it  contains some sample for our dnsms module, it's not mandatory. 
    Similarly, you could create doc folder to contain design documents.

  • ChangeLog <file>: record the update information.

  • LICENSE <file>: License content, currently we could leave it blank.

  • MANIFEST.in <file>: This is a template file which specifies which files (and file patterns) should be included in the package. Below is an example 

    include README
    include LICENSE
    include ChangeLog
    
    #get example folder involved
    graft examples
    
    # Patterns to exclude from any directory
    global-exclude *~
    global-exclude *.pyo
    global-exclude *.pyc

    The manifest template commands are:

     

    CommandDescription
    include pat1 pat2 ...include all files matching any of the listed patterns
    exclude pat1 pat2 ...exclude all files matching any of the listed patterns
    recursive-include dir pat1 pat2 ...include all files under dir matching any of the listed patterns
    recursive-exclude dir pat1 pat2 ...exclude all files under dir matching any of the listed patterns
    global-include pat1 pat2 ...include all files anywhere in the source tree matching — & any of the listed patterns
    global-exclude pat1 pat2 ...exclude all files anywhere in the source tree matching — & any of the listed patterns
    prune direxclude all files under dir
    graft dirinclude all files under dir
  • README <file>: just like all these readme files, contains description on how to use the software. It's recommended to write this file in reStructuredText, so that the PyPI can use it to generate your project’s PyPI page.

  • setup.py <file>: This file is the most import one for packing a package, it looks like below:

    setup.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    from setuptools import setup, find_packages
     
    setup(
             name = "dnsms" ,
             version = "0.10" ,
             description = "client for dns management service" ,
             long_description = open ( 'README' ).read() + "\n\n" +
                              open ( "ChangeLog" ).read(),
             author = "Zhang Liang" ,
             author_email = "zhang_liang@ctrip.com" ,
             keywords = "dns client service restful" ,
             packages = find_packages(),
             requires = [
                     "requests" ,
             ],
             install_requires = [
                     "requests"
             ],
             classifiers = (
                 "Development Status :: 2 - Pre-Alpha"
                 "Intended Audience :: Developers"
                 "Natural Language :: English"
                 "Programming Language :: Python"
                 "Programming Language :: Python :: 2.7"
                 "Programming Language :: Python :: Implementation :: CPython"
                 "Topic :: Software Development :: Libraries :: Python Modules"
             ),
         )

    Just modify the code to fit your own module, it will works well.

    For the detail of each item in setup.py, you could refer to http://docs.python.org/distutils/setupscript.html & http://peak.telecommunity.com/DevCenter/setuptools#including-data-files

After create the layout and all files, now you can use the sdist command to create a source distribution.

 

$ cd path/to/dnsms_client

$ python setup.py sdist

You can specify as many formats as you like using the --formats option.

$ python setup.py sdist --formats=gztar
FormatDescriptionNotes
zipzip file (.zip)default on Windows
gztargzip’ed tar file (.tar.gz)default on Unix
bztarbzip2’ed tar file (.tar.bz2) 
ztarcompressed tar file (.tar.Z)requires the compress program.
tartar file (.tar) 

Now the folder dnsms_client show like below:

The package <dnsms-0.10.zip> is stored in the dirctory dist.

To create built distributions, you can use bdist command like using sdist.
CommandFormats
bdist_dumbtar, ztar, gztar, zip
bdist_rpmrpm, srpm
bdist_wininstwininst
bdist_msimsi

For detail, please refer to http://docs.python.org/distutils/builtdist.html.

For more commands, you could use help to get more information like below:

$ python setup.py --help-commands

转载于:https://www.cnblogs.com/Alex-Python-Waiter/archive/2012/09/21/2697445.html

(base) PS D:\桌面\PandaGPT-main> pip install deepspeed==0.9.2 Collecting deepspeed==0.9.2 Downloading deepspeed-0.9.2.tar.gz (779 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 779.3/779.3 kB 2.9 MB/s eta 0:00:00 Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [9 lines of output] Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "C:\Users\Lenovo\AppData\Local\Temp\pip-install-zpem72ry\deepspeed_1179651fa32042908e8dca16f3c0cd73\setup.py", line 128, in <module> assert torch_available, "Unable to pre-compile ops without torch installed. Please install torch before attempting to pre-compile ops." AssertionError: Unable to pre-compile ops without torch installed. Please install torch before attempting to pre-compile ops. [WARNING] Unable to import torch, pre-compiling ops will be disabled. Please visit https://pytorch.org/ to see how to properly install torch on your system. [WARNING] unable to import torch, please install it if you want to pre-compile any deepspeed ops. DS_BUILD_OPS=1 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. WARNING: There was an error checking the latest version of pip. 报错的原因
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值