zoukankan      html  css  js  c++  java
  • Python之包管理工具

    安装Python包的过程中,经常涉及到distutils、setuptools、distribute、setup.py、easy_install、easy_install和pip等等。

    distuils

    distutils 是 python 标准库的一部分,这个库的目的是为开发者提供一种方便的打包方式, 同时为使用者提供方便的安装方式。

    经常使用的setup.py就是基于distutils实现的,然后通过setup.py就可以进行打包或者安装了。

    [root@node175 webdemo]# ls -a -l
    总用量 20
    drwxr-xr-x   5 root root  126 1月   5 17:25 .
    drwxr-xr-x. 45 root root 4096 1月   5 14:50 ..
    drwxr-xr-x   8 root root 4096 1月   5 14:53 .git
    -rw-r--r--   1 root root    0 1月   5 14:45 LICENSE
    -rw-r--r--   1 root root    0 1月   5 14:45 README.md
    -rw-r--r--   1 root root  226 1月   5 14:48 requirement.txt
    -rw-r--r--   1 root root  607 1月   5 14:50 setup.cfg
    -rw-r--r--   1 root root  355 1月   5 14:47 setup.py
    drwxr-xr-x   2 root root   24 1月   5 14:46 webdemo
    

    这个是一个最简单的Python项目目录:

    源代码放在子目录Webdemo/下,然后包含了软件包管理的所需的文件:

    • setup.py
    • setup.cfg
    • requirements.txt
    • LICENSE
    • README

    set.py

        #encoding=utf-8
    
    import setuptools
    # In Python < 2.7.4, a lazy loading of package `pbr` will break
    # setuptools if some other modules registered functions in `atexit`.
    # solution from: http://bugs.Python.org/issue15881#msg170215
    try:
        import multiprocessing  # noqa
    except ImportError:
        pass
    
    setuptools.setup(
        setup_requires=['pbr'], pbr=True)
    

    setup.cfg

    [metadata]
    name = webdemo
    version = 0.0.1
    summary = Web Application Demo
    description-file = README.md
    author = author
    author-email = author@example.com
    classifier =
        Environment :: Web Environment
        Intended Audience :: Developers
        Intended Audience :: Education
        License :: OSI Approved :: GNU General Public License v2 (GPLv2)
        Operating System :: POSIX :: Linux
        Programming Language :: Python
        Programming Language :: Python :: 2
        Programming Language :: Python :: 2.7
    
    [global]
    setup-hooks =
        pbr.hooks.setup_hook
    
    [files]
    packages = webdemo
    
    [entry_points]
    console_scripts =
    

    只包含最基本的信息,接下来是requirements.txt文件:

    # The order of packages is significant, because pip processes them in the order
    # of appearance. Changing the order has an impact on the overall integration
    # process, which may cause wedges in the gate later.
    
    pbr<2.0,>=0.11
    

    配合git:

    git init
    git add .
    git commit -m "init project"
    git tag -a 0.0.1 -m "version 0.01"
    

    然后就可以使用Python setup.py sdist命令来生成一个0.0.1版本的源码归档了:

    python setup.py sdist
    

    查看文件:

    [root@node175 webdemo]# tree 
    .
    ├── AUTHORS
    ├── ChangeLog
    ├── dist
    │   └── webdemo-0.0.1.tar.gz   #生成的压缩包
    ├── LICENSE
    ├── README.md
    ├── requirement.txt
    ├── setup.cfg
    ├── setup.py
    ├── webdemo
    │   └── __init__.py
    └── webdemo.egg-info
        ├── dependency_links.txt
        ├── entry_points.txt
        ├── not-zip-safe
        ├── pbr.json
        ├── PKG-INFO
        ├── SOURCES.txt
        └── top_level.txt
    
    3 directories, 16 files
    

    使用者就可以解压缩这个包然后执行 python setup.py install进行安装,然后就可以使用这个模块了;

    setuptools 与 distribute

    setuptools 是对 distutils 的增强,尤其是引入了包依赖管理。我们可以通过ez_setup.py来安装setuptools

    至于distribute,它是setuptools的一个分支版本。分支的原因是有一部分开发者认为 setuptools 开发太慢。但现在,distribute 又合并回了 setuptools 中,所以可以认为它们是同一个东西。

    前面看到setup.py可以创建一个压缩包,而setuptools使用了一种新的文件格式(.egg),可以为Python包创建 egg文件。setuptools 可以识别.egg文件,并解析、安装它;

    当安装好setuptools/distribute之后,我们就可以直接使用easy_install这个工具了:

    • 从PyPI上安装一个包:当使用 easy_install package 命令后,easy_install 可以自动从 PyPI 上下载相关的包,并完成安装,升级
    • 下载一个包安装:通过 easy_install package.tgz 命令可以安装一个已经下载的包
    • 安装egg文件:通过 easy_install package.egg 可以安装一个egg格式的文件

    setuptools/distribute和easy_install之间的关系:

    • setuptools/distribute 都扩展了 distutils,提供了更多的功能
    • easy_install是基于setuptools/distribute的一个工具,方便了包的安装和省级

    pip

    pip install pkg
  • 相关阅读:
    Codeforces467C George and Job
    Codeforces205E Little Elephant and Furik and RubikLittle Elephant and Furik and Rubik
    Codeforce205C Little Elephant and Interval
    51nod1829 函数
    51nod1574 排列转换
    nowcoder35B 小AA的数列
    Codeforce893E Counting Arrays
    gym101612 Consonant Fencity
    CodeForces559C Gerald and Giant Chess
    CodeForces456D A Lot of Games
  • 原文地址:https://www.cnblogs.com/chris-cp/p/8206221.html
Copyright © 2011-2022 走看看