安装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

Python之包管理工具的更多相关文章

  1. Python的包管理工具Pip (zz )

    Python的包管理工具Pip 接触了Ruby,发现它有个包管理工具RubyGem很好用,并且有很完备的文档系统http://rdoc.info 发现Python下也有同样的工具,包括easy_ins ...

  2. Python的包管理工具

    Python的包管理工具 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么使用包管理 Python的模块或者源文件直接可以复制到目标项目目录中,就可以导入使用了. 但是为了 ...

  3. 【转载】Python的包管理工具Pip

    接触了Ruby,发现它有个包管理工具RubyGem很好用,并且有很完备的文档系统http://rdoc.info 发现Python下也有同样的工具,包括easy_install和Pip.不过,我没有细 ...

  4. Python的包管理工具Pip

    接触了Ruby,发现它有个包管理工具RubyGem非常好用,而且有非常完备的文档系统http://rdoc.info 发现Python下也有相同的工具,包含easy_install和Pip.只是,我没 ...

  5. Python之包管理工具:distutils、setuptools、distribute、setup.py、easy_install、easy_install、pip

    在安装Python包的过程中,经常涉及到distutils.setuptools.distribute.setup.py.easy_install.easy_install和pip等等. 介绍:htt ...

  6. 5、Python之包管理工具pip

    pip提供我们各色各样的软件(第三方库),而这些第三方库又可以给我们实现各种各样不同的功能,科学计算.画图.操作文件.聊天-- 我们可以通过Cmd终端.Pycharm.Jupyter三种平台使用pip ...

  7. Python 的包管理工具 distribute, setuptools, easy_install命令与 pip命令

    Setuptools 是 Python Enterprise Application Kit (PEAK)的一个副项目,它是 Python 的disutils工具的增强工具,可以让程序员更方便地创建和 ...

  8. Python的包管理工具--PIP安装使用

    最新安装方式 # wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py  // 使用该方式安装已经不再要求提前安装setuptoo ...

  9. 【跟我一起学Python吧】Python的包管理工具

    刚开始学习Python时,在看文档和别人的blog介绍安装包有的用easy_install, setuptools, 有的使用pip,distribute,那麽这几个工具有什么关系呢,看一下下面这个图 ...

  10. Python的包管理工具easy_install, setuptools, pip,distribute介绍

    1.相互之间的关联 easy_install, setuptools, pip,distribute,这几个工具的联系,如下图: 可以看到distribute是setuptools的取代,pip是ea ...

随机推荐

  1. 四轴自适应控制算法的一些尝试开源我的山猫飞控和梯度在线辨识自适应等算法—(转)

    本文的最主要目的在于抛砖引玉,阿莫论坛真的是非常好的一个论坛,没有这个论坛,没有那么多这个论坛上的前人无私的奉献和热烈的讨论,我想我是怎么也无法入门四轴的控制的.只是论坛上已经很多年都没有看到过新东西 ...

  2. Windows server 2008 R2如何预览图片而不是显示图标?

      Previews of media files are disabled by default in Windows Server 2008. In this article we will en ...

  3. java 上传图片压缩图片

    package com.bitspace.flame.util; import java.io.File; import java.awt.Image;import java.awt.image.Bu ...

  4. 关于 redis 报错 :JsonParseException: Unrecognized token 'xxx': was expecting ('true', 'false' or 'null')

    在使用java  读取redis存储的数据时出现 JsonParseException: Unrecognized token 'xiaoqiang': was expecting ('true', ...

  5. Tensorflow高速入门2--实现手写数字识别

    Tensorflow高速入门2–实现手写数字识别 环境: 虚拟机ubuntun16.0.4 Tensorflow 版本号:0.12.0(仅使用cpu下) Tensorflow安装见: http://b ...

  6. Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】

    原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...

  7. SpringBoot 启动错误搜集

    Spring Boot:The Bean Validation API is on the classpath but no implementation could be found https:/ ...

  8. ngx_http_upstream_keepalive

    链接:http://wiki.nginx.org/HttpUpstreamKeepaliveModule 今天看了一些代码: upstream b_memc2 { server ; keepalive ...

  9. angularjs中templateUrl的路径问题

    angularjs中templateUrl的路径问题 templateUrl的相对路径是现对于app主页面的,因为template使用js来加载的.

  10. Customize Web Sessions List

    To customize Fiddler's Web Sessions List, add rules using FiddlerScript to the OnBeforeRequest funct ...