Python中setup.py一些不为人知的技巧
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一些不为人知的技巧的更多相关文章
- Python中__init__.py文件的作用详解
转自http://www.jb51.net/article/92863.htm Python中__init__.py文件的作用详解 http://www.jb51.net/article/86580. ...
- python中__init__.py文件的作用
问题 在执行models.py时,报ImportError:No module named transwarp.db的错误,但明明transwarp下就有db.py文件,路径也没有错误.真是想不通.后 ...
- python 利用 setup.py 手动安装第三方类库
python 利用 setup.py 手动安装第三方类库 由于我在mac使用时,装了python3,默认有python2的环境,使用 pip 安装第三方类库时,老是安装到 python2的环境上: 在 ...
- 转载:【学习之家】Python中__init__.py文件的作用
Python中__init__.py文件的作用详解 Python中__init__.py文件的作用详解 来源:学习之家 作者:xuexi110 人气:357 发布时间:2016-09-29 摘要:__ ...
- python 利用 setup.py 手动安装django_chartit
手动安装django_chartit库 1 下载压缩包 2 解压到python安装目录下,文件夹名为django_chartit,并检查文件夹下是否有setup.py文件 3 在cmd中进入djang ...
- 『Python』setup.py简介
setup.py应用场合 网上见到其他人这样介绍: 假如我在本机开发一个程序,需要用到python的redis.mysql模块以及自己编写的redis_run.py模块.我怎么实现在服务器上去发布该系 ...
- python的setup.py文件
最近工作需要,用Cython写了*.pyx扩展,并将其编译成C文件,最后转换为so扩展,供python引用使用 distutils 编译,建立一个setup.py 的脚本from distutils. ...
- python的setup.py文件及其常用命令
编写setup.py文件,获取帮助:python setup.py --help-commands [python] Standard commands: build ...
- python 使用 setup.py 方式安装及包的卸载
安装: 可通过 --home 或 --prefix 指定安装目录 --prefix=xx/xxx 选择安装目录 --record files.txt 记录所有安装文件的路径 ...
随机推荐
- 29.Junit测试框架.md
目录 作用 使用 单个对象的测试 有步骤的测试 注意 作用 用于简化测试,可以对方法,类,包等范围测试 使用 单个对象的测试 在需要测试的方法上加注解@Test,选中方法,运行里选择junit执行 同 ...
- JAVAWEB 一一框架整合(SSI : Spring+SpringMVC+ ibtis)
web.xml applicationContext.xml springmvc-servlet.xml UserController package com.ssi.controller; impo ...
- Visual studio 2019 preview & C# 8 initial experience
Visual studio 2019 preview & C# 8 initial experience using System; using static System.Con ...
- PCA 降维
http://f.dataguru.cn/spark-751832-1-1.html 我们可以利用PCA算法将向量的维数降低,从而实现特征转化.具体原理在<机器学习>课程中有详细的讲述.故 ...
- @Component单例与并发(未解决)
今天用websocket记录连接的个数: 模拟少量请求到服务器端的websocket,@Component默认是单例的,让其注解到MyWebSocket类上: 每次请求过来都是相同的MyWebSock ...
- orthodb
1.数据库 orthodb数据: odb10v0_levels.tab.gz: NCBI taxonomy nodes where Ortho DB orthologous groups (OGs) ...
- @JsonInclude(JsonInclude.Include.NON_NULL) 加在对象上
@JsonInclude(JsonInclude.Include.NON_NULL) public class ViewWorkermessage implements Serializable { ...
- NDK环境搭建方法2
1.新建项目NDKDemo3 2.新建com.example.shixm.ndkdemo3.MyNdk.java 3.右键main文件夹,New->Folder->JNI Folder 4 ...
- PHP 用正则获取URL的根域名
function GetUrlRoot($url){ preg_match('/[\w][\w-]*\.(?:com\.cn|com|cn|co|net|org|gov|cc|biz|info)(\/ ...
- pta l2-18(多项式A除以B)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984 题意:给定两个多项式,求出其做除法 ...