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. Django添加ckeditor富文本编辑器

    源码 https://github.com/django-ckeditor/django-ckeditor 通过pip安装. pip3 install django-ckeditor pip3 ins ...

  2. openwrt手工配置pptpd

    官方wiki:http://wiki.openwrt.org/doc/howto/vpn.server.pptpd#prerequisites 20190220更新:PPTP VPN协议已经被 IOS ...

  3. python 验证码test

    灰度化 #coding:utf8 import re import urllib import urllib2 import requests from PIL import Image import ...

  4. (转)MySQL 获得当前日期时间 函数

    select *from High_valwhere SerialDate >= curdate() and SerialDate < date_add(curdate(), interv ...

  5. ceph部署手册

    CentOS7.2部署Luminous版Ceph-12.2.0 在CentOS7.2上安装部署Luminous版Ceph-12.2.0.由于ceph的Luminous版本默认使用bluestore作为 ...

  6. jsonp封装成promise

    首先将jsonp通过npm 安装引入js文件中,代码如下 import originJsonp from 'jsonp' export default function jsonp(url, data ...

  7. Java的学习04

    今天依旧记录一下,学习的东西. import java.io.File; import java.io.IOException; import java.util.Date; /** * 测试File ...

  8. Ambient Light

    [Ambient Light] Ambient light is light that is present all around the scene and doesn’t come from an ...

  9. php使用redis扩展以及安装redis(linux下)

    一,安装redis 1,下载redis包:wget http://download.redis.io/releases/redis-2.8.9.tar.gz 2,解压redis包后,进入redis-2 ...

  10. python找包的路径(找不到自定义包的问题解决)

    问题:工程下自定义的包,python在执行时经常找不到包   python找包的路径:python安装路径下的lib包和PYTHONPATH下的包     可以使用[sys.path]打印出pytho ...