http://python.jobbole.com/80912/

在我开始之前,我想先说清楚我将要解释的是些“窍门”。他们不是“最好的做法”,至少在一种情况下是不可取的。

说到不可取的做法,我会适时写一个“setup.py陷阱”的博文,这都是我相信你不会在setup.py模块做出的事情。

窍门

这些窍门让我使用python做打包管理变得更简单。在你完善他们之前,我建议你至少有些关于创建新包的基本经验。学python打包的两种方法是New Library Sprint (初级)和 Python Packaging User Guide (高级些)。

‘python setup.py publish’

一切都是从这里开始的。一天我在看汤姆的代码时发现python setup.py publish命令在Django Rest Framework里的 setup.py模块里面。它像这样:

 
 
 
 
 

Python

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# setup.py
import os
import sys
 
# I'll discuss version tricks in a future blog post.
version = "42.0.0"
 
if sys.argv[-1] == 'publish':
    os.system("python setup.py sdist upload")
    os.system("python setup.py bdist_wheel upload")
    print("You probably want to also tag the version now:")
    print("  git tag -a %s -m 'version %s'" % (version, version))
    print("  git push --tags")
    sys.exit()
 
# Below this point is the rest of the setup() function

用这种方法太赞了,我不需要去查找那有些晦涩的python setup.py sdist upload命令,或是真的很让人困惑的python setup.py bdist_wheel upload命令了。取而代之的是,当要把包发布在PyPI上时,我只需要打下:

 
 
 
 
 

Python

 
1
$ python setup.py publish

好记多了!

‘python setup.py tag’

汤姆的python setup.py publish指令的问题在于,他强迫我去打出git tag命令。好吧,诚实些,他让我复制/粘贴我屏幕上的输出。因此,全靠我自己,我“发明”了python setup.py tag 指令:

 
 
 
 
 

Python

 
1
2
3
4
5
6
# setup.py
 
if sys.argv[-1] == 'tag':
    os.system("git tag -a %s -m 'version %s'" % (version, version))
    os.system("git push --tags")
    sys.exit()

很漂亮,哈?现在我不需要去记住那么多模糊的git命令。我就得到了短版python setup.py publish命令:

 
 
 
 
 

Python

 
1
2
3
4
if sys.argv[-1] == 'publish':
    os.system("python setup.py sdist upload")
    os.system("python setup.py bdist_wheel upload")
    sys.exit()

当我需要做一个版本时,我用我的代码,然后打出:

 
 
 
 
 

Python

 
1
2
$ python setup.py publish
$ python setup.py tag

我为什么不合并那些代码?嗯,你不可以把“RC1”或“-alpha”用作你PyPI的版本名称。分离这些命令,我可以对我的包的发布有更精细的掌控。我被鼓励用alpha、beta,还有在git tag发布参与者,而不是正式的PyPI发布。

‘python setup.py test’

我很确定我的一些读者在这个窍门中会遇到很严重的问题。事实上,依据管理python包的基础建设的人的回应,这会在我接下来的“陷阱”博文中讲。

那么然后……

我喜欢py.test。我曾写过关于使用py.test的博客。我试着在各处用它。然而,我真的不是必须用python setup.py test的狂热分子。我感觉到用py.test不舒服的那一刻是它让我在setup.py中添加特殊类时。

不幸的是,有另一种方式:

 
 
 
 
 
 

Python

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if sys.argv[-1] == 'test':
    test_requirements = [
        'pytest',
        'flake8',
        'coverage'
    ]
    try:
        modules = map(__import__, test_requirements)
    except ImportError as e:
        err_msg = e.message.replace("No module named ", "")
        msg = "%s is not installed. Install your test requirments." % err_msg
        raise ImportError(msg)
    os.system('py.test')
    sys.exit()

只意味着我要添加一个简单的代码来用py.test和python setup.py test:

 
 
 
 
 

Python

 
1
$ python setup.py test

理论上,可以运行pip install命令安装缺少依赖包,或者从requirements文件中调用。但是,由于这是“窍门”,我想让它保持简洁好用。如果我用这个得到了足够的好结果,我会更新这个包括缺少要求的pip调用的例子。

注意:这不是说我不用tox。实际上,我用tox来调用我那一版本的python setup.py test。

subprocess模块怎么样?

有些人会问,“你为什么不利用子进程库来用这些shell命令呢?”

我的答案是,“因为,如果我杀鸡还需要用宰牛刀的话,未免太过了。”对于这些简单的窍门,os.system()函数就很够用了。

为什么不直接用Makefile?

我一开始在Mac OSX和Linux上编程,我的很多开源包使用Windows。感谢AppVeyor,我正不断测试在那环境中的代码。实际上,我会给Windows使用者改进我的“窍门”。

陷阱!

2015年初会发布“陷阱”博客,敬请期待

Updates

  • 2014/12/21 – Added a note about using tox.
  • 2014/12/21 – Added a note about Makefile and Windows

Python中setup.py一些不为人知的技巧的更多相关文章

  1. Python中__init__.py文件的作用详解

    转自http://www.jb51.net/article/92863.htm Python中__init__.py文件的作用详解 http://www.jb51.net/article/86580. ...

  2. python中__init__.py文件的作用

    问题 在执行models.py时,报ImportError:No module named transwarp.db的错误,但明明transwarp下就有db.py文件,路径也没有错误.真是想不通.后 ...

  3. python 利用 setup.py 手动安装第三方类库

    python 利用 setup.py 手动安装第三方类库 由于我在mac使用时,装了python3,默认有python2的环境,使用 pip 安装第三方类库时,老是安装到 python2的环境上: 在 ...

  4. 转载:【学习之家】Python中__init__.py文件的作用

    Python中__init__.py文件的作用详解 Python中__init__.py文件的作用详解 来源:学习之家 作者:xuexi110 人气:357 发布时间:2016-09-29 摘要:__ ...

  5. python 利用 setup.py 手动安装django_chartit

    手动安装django_chartit库 1 下载压缩包 2 解压到python安装目录下,文件夹名为django_chartit,并检查文件夹下是否有setup.py文件 3 在cmd中进入djang ...

  6. 『Python』setup.py简介

    setup.py应用场合 网上见到其他人这样介绍: 假如我在本机开发一个程序,需要用到python的redis.mysql模块以及自己编写的redis_run.py模块.我怎么实现在服务器上去发布该系 ...

  7. python的setup.py文件

    最近工作需要,用Cython写了*.pyx扩展,并将其编译成C文件,最后转换为so扩展,供python引用使用 distutils 编译,建立一个setup.py 的脚本from distutils. ...

  8. python的setup.py文件及其常用命令

    编写setup.py文件,获取帮助:python setup.py --help-commands [python]  Standard commands:    build             ...

  9. python 使用 setup.py 方式安装及包的卸载

     安装:         可通过 --home 或 --prefix 指定安装目录 --prefix=xx/xxx    选择安装目录 --record files.txt   记录所有安装文件的路径 ...

随机推荐

  1. 【原】Win7 host 与 virtualbox ubuntu guest 相同ping通

    环境:Win7 host 与 virtualbox ubuntu guest 在 “设置”-->"网络" 中选择网卡1,修改连接方式为 “桥接网卡” 后, 主机和虚拟机可以相 ...

  2. Celery + RabbitMq 示意图

    一直搞不清楚消息队列和任务队列是如何结合的,直到碰到了 :http://www.cnblogs.com/jijizhazha/p/8086119.html 中的图,恍然大悟,凭借自己的理解,画了这幅组 ...

  3. Spring3.0学习1.2(使用annotation)

    使用annotation 首先 xml文件更改  新加xslt <?xml version="1.0" encoding="UTF-8"?> < ...

  4. 吴裕雄 python 机器学习-KNN(2)

    import matplotlib import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import ...

  5. Centos7安装Oracle 11gR2

    ======================================== - 环境:VM12+centos7 x86_64 minimal - 最小化安装的Centos7 - 虚拟机配置- 5 ...

  6. java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    hibernet 报错 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 处理方法  数据表字段为char ...

  7. matlab stereo_gui立体标定

    http://www.vision.caltech.edu/bouguetj/calib_doc/index.html#examples 文档中举了几个例子,有关双目的是第5个, 这个例子展示了如何使 ...

  8. Tween animation

    [Tween animation] An animation defined in XML that performs transitions such as rotating, fading, mo ...

  9. EasyUI值的清空与获取

    清空: 一般值 $("#searchx").val(""); 时间选择框 $('#starttime').datetimebox('setValue', '') ...

  10. macaca自动化测试以及配置环境问题

    macaca 测试和环境问题 标签(空格分隔): macaca自动化配置环境问题 macaca环境变量配置 基本环境需要准备的东西: JDK的安装及环境配置:(1.8) Node.js的安装及环境配置 ...