python中setup 打包安装包
来源:互联网 发布:淘宝字画 编辑:程序博客网 时间:2024/06/01 20:04
python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算、图片处理、web应用、GUI开发等。当然也可以将自己写的模块进行打包或发布。一简单的方法是将你的类包直接copy到python的lib目录,但此方式不便于管理与维护,存在多个python版本时会非常混乱。现介绍如何编写setup.py来对一个简单的python模块进行打包。
一、编写模块
进入项目目录
#cd /home/pysetup
#vi foo.py
class MyClass(): def __init__(self): self.blog = "http://blog.liuts.com" def printblog(self): print self.blog def printBblog(self): print self.blog.swapcase()
二、编写setup.py
#vi setup.py
from distutils.core import setup setup(name='Myblog', version='1.0', description='My Blog Distribution Utilities', author='Liu tiansi', author_email='liutiansi@gmail.com', url='http://blog.liuts.com', py_modules=['foo'],
)
更多参数说明见表:
三、setup.py参数说明
#python setup.py install #安装
#python setup.py sdist #生成压缩包(zip/tar.gz)
#python setup.py bdist_wininst #生成NT平台安装包(.exe)
#python setup.py bdist_rpm #生成rpm包
或者直接"bdist 包格式",格式如下:
#python setup.py bdist --help-formats
--formats=rpm RPM distribution
--formats=gztar gzip'ed tar file
--formats=bztar bzip2'ed tar file
--formats=ztar compressed tar file
--formats=tar tar file
--formats=wininst Windows executable installer
--formats=zip ZIP file
四、打包
#python setup.py sdist
running sdist
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating Myblog-1.0
making hard links in Myblog-1.0...
hard linking foo.py -> Myblog-1.0
hard linking setup.py -> Myblog-1.0
creating dist
tar -cf dist/Myblog-1.0.tar Myblog-1.0
gzip -f9 dist/Myblog-1.0.tar
removing 'Myblog-1.0' (and everything under it)
提示两条warning可以忽略,不影响打包,当然一个完善的项目必须有README及MANIFEST.in(项目文件清单)文件。
#ls dist
Myblog-1.0.tar.gz
五、安装
#tar -zxvf Myblog-1.0.tar.gz
#cd Myblog-1.0.tar.gz
#python setup.py install (此命令大家再熟悉不过了)
running install
running build
running build_py
creating build/lib.linux-x86_64-2.6
copying foo.py -> build/lib.linux-x86_64-2.6
running install_lib
copying build/lib.linux-x86_64-2.6/foo.py -> /usr/local/lib/python2.6/dist-packages
byte-compiling /usr/local/lib/python2.6/dist-packages/foo.py to foo.pyc
running install_egg_info
Writing /usr/local/lib/python2.6/dist-packages/Myblog-1.0.egg-info
六、测试
>>> from foo import MyClass
>>> app=MyClass()
>>> app.print printblog()
>>> app.printblog()
http://blog.liuts.com
>>> app.printBblog()
HTTP://BLOG.LIUTS.COM
>>>
如何制作Python安装模块
Python模块的安装方法:
1. 单文件模块
直接把文件拷贝到$python_dir/lib
2. 多文件模块,带setup.py
python setup.py install
3. egg文件,类似Java的jar
1) 下载ez_setup.py,运行python ez_setup
2) easy_install *.egg
虽然Python的模块可以拷贝安装,但是一般情况下推荐制作一个安装包,即写一个setup.py文件来安装。
setup.py文件的使用:
% python setup.py install #安装
% python setup.py sdist #制作分发包
% python setup.py bdist_wininst #制作windows下的分发包
% python setup.py bdist_rpm
setup.py文件的编写
简单的例子:
setup.py文件
setup(
name="mytest",
version="0.10",
description="My test module",
author="Robin Hood",
url="http://www.csdn.net",
license="LGPL",
packages= find_packages(),
scripts=["scripts/test.py"],
)
mytest.py
def get():
return sys.path
scripts/test.py
print os.environ.keys()
setup中的scripts表示将该文件放到 Python的Scripts目录下,可以直接用。
OK,简单的安装成功,可以运行所列举的命令生成安装包,或者安装该python包。
本机测试成功(win32-python25)!
- python中setup 打包安装包
- python中setup安装包
- 如何制作python安装包 setup.py
- Python中, 使用setup.py和console_scripts参数创建安装包和shell命令
- 卸载python setup.py install安装的包
- 卸载使用“python setup.py install”安装的包
- 对于python setup.py install安装的包如何卸载
- python:用setup.py安装第三方包packages
- 对于python setup.py install安装的包如何卸载
- 如何卸载命令python setup.py install 安装的包?
- python 自定义 包 模块 打包 安装
- installshield打包成只有setup.exe的安装包时获取setup.exe所在路径的方法
- python 安装 pip setup
- 使用setup.py安装python包和卸载python包的方法
- 02-QT5程序打包成可执行文件.exe和安装包.setup
- Inno Setup制作安装包
- INNO setup 制作安装包
- Windows安装包,Inno Setup
- JAVA集合框架面试题(三)
- 字典树(trie)模板
- 晕晕的php正则表达式
- linux-vi编辑命令
- Android getDecorView用途——屏幕截图
- python中setup 打包安装包
- 最强大最好用的Web调试工具Fiddler,有了这个burp suit就可以del了
- 下午sping+hibernate配置遇到的小问题总结下
- 黑马程序员_java静态代码块、构造代码块和构造函数的执行时间分析
- cdc
- HTML_01 常用基本标签
- setup.py vs requirements.txt
- iOS 为导航栏自定义按钮图案Button Image 运行出来的颜色与原本颜色不一样 -解决方案
- 杭电1872 稳定排序