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. iptables学习

    droidwall.sh #!/system/bin/sh IPTABLES=iptables BUSYBOX=busybox GREP=grep ECHO=echo # Try to find bu ...

  2. [Linux].deb软件包:wine-qq2013-longeneteam安装与卸载

    --------------------------------------------------------------------------------------------- 首先切换到r ...

  3. week06 08 postman 测试jsonrpc

    用postman来测试rpc需要添加特别的字段 ’ { "jsonrpc":"2.0", "id":"123", &qu ...

  4. RabbitMQ、Memcached、SQLAlchemy

    一.RabbitMQ 1.基础概念 rabbitMQ说白了就是一个消息队列,类似于Queue,也是生产者与消费者模型.只不过做了扩展,所不同的是Queue在内存中的消息队列,而RabbitMQ是部署在 ...

  5. .net上的 jpa

    还没试过,有空试试: NPersistence ORSQL

  6. centos 安装python3.6

    环境准备 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 首先去官网下 ...

  7. hive 安装centos7

    wget mirror.bit.edu.cn/apache/hive/hive-2.3.4/apache-hive-2.3.4-bin.tar.gz 解压到/usr/local/apache-hive ...

  8. jvm中堆和栈的区别

    1.前言.    其实jvm能优化的空间不多,最主要的是使用的共享内存不要超过默认的2g或者自己调的参数.但了解一下还是有点意思的,建议面试时还是要看,别学笔者裸奔. 2.区别.   网上说是有5点区 ...

  9. Process对象的其他属性:

    标签(空格分隔): process join方法: 在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况: 情况一:在主进程的任务与子进程的任务彼 ...

  10. 使用tor网络

    在www.torproject.org/projects/torbrowser.html.en上找到合适的版本下载 下载好tor浏览器之后,解压双击Tor Browser,出现这个错误 这是因为kal ...