Python: Command Line Scripts
许多python包包括命令行工具。这对于分发和包相关联的支持工具是很有的。
为了“funniest”,我们添加一个“funniest-joke”命令行工具。
setuptools.setup()提供了两种机制去实现:
- 通过scripts关键字
- 通过entry_points的console_scripts
用scripts关键字
这种方法是在一个单独的文件里写你的脚本。例如
funniest/funniest/__init__.py...setup.pybin/funniest-joke...
这funniest-joke脚本如下
#!/usr/bin/env pythonimport funniest
print funniest.joke()
定义在setup()如下
setup(...scripts=['bin/funniest-joke'],...
)
当我们安装包,setuptools会将脚本拷贝到PATH, 可我们可以直接如下调用
$ funniest-joke
这个好处是工具脚本可以是python也可以不是python。
This has advantage of being generalizeable to non-python scripts, as well: funniest-joke
could be a shell script, or something completely different.
console_scripts 入口
第二个方法就是使用‘entry point’。Setuptools允许模块去注册entrypoints,也提供了自己的例如console_scripts entry point.
其允许Python函数直接注册为命令行可执行工具。在这种情况下,我们需要添加一个新文件和功能去支持它。
如下:
funniest/funniest/__init__.pycommand_line.py...setup.py...
In this case, we’ll add a new file and function to support the command line tool:
command_line.py内容如下:
import funniestdef main():print funniest.joke()
我们可以直接测试这个脚本,如下:
$ python
>>> import funniest.command_line
>>> funniest.command_line.main()
...
我们可以在setup.py中注册main()函数,如下:
setup(...entry_points = {'console_scripts': ['funniest-joke=funniest.command_line:main'],}...
)
当这个包被安装后,我们可以在命令行直接运行‘funniest-joke’。Setuptools会直接生成一个独立的脚本,导入了你的模块和注册的函数。
这种方法的优势是非常方便进行测试。不用在shell端去触发执行脚本,我们就可以有如下测试代码:
from unittest import TestCase
from funniest.command_line import mainclass TestConsole(TestCase):def test_basic(self):main()
为了让其更有用,我们会需要一个捕捉标准输出的context manager, 但其不在该文讨论范围。
Python: Command Line Scripts相关推荐
- python command line debug_【已解决】Mac中PyCharm中去加断点实时调试scrapy的项目
折腾: 后,在之前已经用过PyCharm去加上断点实时调试别的python程序的前提下. 下载希望可以用PyCharm也可以加断点,实时调试scrapy的项目. 不过能想到的是,scrapy这种项目, ...
- python xmxl 无法启动_auto-pypi-一个Python命令行工具,用于自动设置包并将其上载到PyPi。-Sen LEI Use As A Command Line Tool...
作者:Sen LEI ### 作者邮箱:sen.lei@outlook.com ### 首页:https://github.com/Listen180/auto_pypi ### 文档:None ## ...
- Calling JNI Functions with Java Object Arguments from the Command Line
When analyzing malware or penetration testing an app which uses a native library, it's helpful to is ...
- ERROR: Unrecognized command line argument: #39;use#39;
Unrecognized command line argument: 'use' gvm- -GoLang语言多版本管理工具 基础环境 centos6.5 报错内容 gvm在命令行以外的任何地方调用 ...
- Command line is too long. Shorten command line for Application---微服务升级_SpringCloud Alibaba工作笔记0067
今天打开idea项目,启动的时候,无法启动报错: 15:19 Error running 'Application': Command line is too long. Shorten com ...
- suse12中pip install xxx报错gcc: error: unrecognized command line option ‘-fstack-clash-protection’
我们在Linux中安装某些python的第三方包时,有时候需要gcc来进行协助编译安装(例如psutil.pycrypto等) 1.很不幸,最近在suse12中用pip install psutil时 ...
- MySQL 5.6 警告信息 command line interface can be insec
MySQL 5.6 警告信息 command line interface can be insecure 修复 在命令行输入密码,就会提示这些安全警告信息. Warning: Using a pas ...
- [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure. 来自TMySQL用户,包括很多开发和G ...
- 【The Linux Command Line】学习笔记
以下内容参考于书籍<The Linux Command Line>,中文版本翻译项目:快乐的 Linux 命令行 终端 提示符:$ 表示普通用户,# 表示超级用户 鼠标与光标:使用光标选择 ...
最新文章
- Python---图像读写操作(scipy)
- 打造属于自己的图文符号库
- 大型数据中心蓄电池规划与应用中的痛点及展望
- Servlet中获取请求体的数据
- codeforces Restore Cube(暴力枚举)
- 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration
- Linux学习总结(33)——Linux Java启动关闭bash脚本
- mysql 内置存储过程_数据库:mysql内置功能-存储过程
- Summernote个性化定制使用帮助(二)
- java怎么定义scanner_Java Scanner类的常用方法及用法(很详细)
- Linux中fcntl函数介绍
- 数学基础知识总结 —— 4. 常见函数图像
- java计算机毕业设计进出货管理系统MyBatis+系统+LW文档+源码+调试部署
- 我看了java_我看java没有那么难(转载)
- python apply函数_Python中apply函数的用法实例教程
- 郑捷《机器学习算法原理与编程实践》学习笔记(第三章 决策树的发展)(二)_C4.5...
- JavaSE学习笔记
- C# 文本框定位到文本末尾
- python-共现矩阵(共词矩阵)计算
- js用map筛选指定元素(ES6的map方法)
热门文章
- Xmanager Enterprise 5破解版
- TI-RTOS之初体验(1)
- 怎么可以修改pr基本图形中的文字_10、Pr中基本图形安装使用,点点就可以应用高级的字幕...
- mysql按 结束不了语句_每一条 mysql 命令必须以( )结束,否则系统判断语句尚未结束,暂不执行_大学计算机A1重修答案_学小易找答案...
- eGPU 能否一战?- Tensorflow GPU 基准测试
- VUE打印 每页顶部添加一个二维码
- Elasticsearch 中的骚操作你确定不看看?
- 第十四届蓝桥杯模拟赛(Python)
- 极简Python语法(5)
- IPv4/IPv6地址范围与网络地址/子网掩码的转换,点分十进制与数字掩码的转换