我刚刚测试过stdeb
(see https://pypi.python.org/pypi/stdeb https://pypi.python.org/pypi/stdeb),一个 Python 包,用于将任何其他 Python 包转换为 Debian 包。
首先我安装了 stdeb:
apt-get install python-stdeb
然后我做了一个简单的脚本叫做myscript.py
包含以下内容:
def main():
print "Hello world, says myscript!"
# wait for input from the user
raw_input()
if __name__ == '__main__':
main()
重要的是,您的目录结构应该是:
somewhere/myscript/
setup.py
myscript/
__init__.py
myscript.py
在 setup.py 文件中,您可以执行以下操作:
import os
from setuptools import setup
from nvpy import nvpy
setup(
name = "myscript",
version = "1.0",
author = "Charl P. Botha",
author_email = "[email protected] /cdn-cgi/l/email-protection",
description = "Demo of packaging a Python script as DEB",
license = "BSD",
url = "https://github.com/cpbotha/nvpy",
packages=['myscript'],
entry_points = {
'console_scripts' : ['myscript = myscript.myscript:main']
},
data_files = [
('share/applications/', ['vxlabs-myscript.desktop'])
],
classifiers=[
"License :: OSI Approved :: BSD License",
],
)
The console_scripts
指令很重要,它将创建一个名为的可执行脚本my_script
,在安装生成的 DEB 后,它将在系统范围内可用。如果你的脚本使用类似的东西tkinter
or wxpython
并且有图形用户界面,您应该使用gui_scripts
代替console_scripts
.
The data_files
指令将安装一个合适的桌面文件到/usr/share/applications
,这样您也可以开始myscript
从您的桌面环境。vxlabs-myscript.desktop
看起来像这样:
[Desktop Entry]
Version=1.0
Type=Application
Name=myscript
Comment=Minimal stdeb example
# myscript should wait for user input at the end, else the terminal
# window will disappear immediately.
Exec=myscript
Icon=/usr/share/icons/gnome/48x48/apps/file-manager.png
Categories=Utility;
# desktop should run this in a terminal application
Terminal=true
StartupNotify=true
StartupWMClass=myscript
要构建 DEB,您需要在顶层执行以下操作myscript
:
python setup.py --command-packages=stdeb.command bdist_deb
这将在 deb_dist 目录中创建一个 .deb。
安装了我这样创建的 DEB 后,我可以运行myscript
从命令行,我也可以从桌面环境调用它。
这是一个 GitHub 存储库,其中包含上面的示例代码:https://github.com/cpbotha/stdeb-minimal-example https://github.com/cpbotha/stdeb-minimal-example