关于Python的setup.py文件

虽然Python的模块可以拷贝安装,但是一般情况下推荐制作一个安装包,即写一个setup.py文件来安装。

如何自己写一个Pythonsetup.py文件呢,我通过一个例子来演示一下。

运行环境

  我的运行环境是centos 6.5Python的版本是2.6.6.

2 操作步骤

  我在centos当前目录下创建了两个文件,一个是setup.py,另外一个是say_helloword.py

   setup.py的文件内容:

from distutils.core import setup
setup(name = "say_helloword" ,
      version = "0.10" ,
      py_modules=['say_helloword'], 
    )
say_helloword.py文件的内容:

def say_hello():
    print 'hello,world!'
然后我在centos当前目录下执行 #python setup.py sdist,结果如下图


<img src="https://img-blog.csdn.net/20140713140415034?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2RrdWl3ZWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
<span style="font-size:18px;"><span style="font-family: 微软雅黑, Arial, Helvetica, sans-serif; line-height: 28.7999992370605px;">在当前目录下,会创建</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; word-wrap: break-word; background: rgb(238, 238, 238);">dist</code><span style="font-family: 微软雅黑, Arial, Helvetica, sans-serif; line-height: 28.7999992370605px;">目录,里面有个文件名为</span></span><p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;"><span style="color: rgb(85, 85, 85); font-family: 宋体; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">say_helloword-0.10.tar.gz</span><span style="line-height: 28.7999992370605px;">,这个就是可以分发的包。使用者拿到这个包后,解压,到</span><span style="font-family: 宋体; line-height: 28.7999992370605px;">say_helloword</span><span style="line-height: 28.7999992370605px;">-1.0目录下执行:</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; word-wrap: break-word; background-image: initial; background-attachment: initial; background-color: rgb(238, 238, 238); background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">python setup.py install</code><span style="line-height: 28.7999992370605px;">,那么,</span><span style="font-family: 宋体; line-height: 28.7999992370605px;">say_helloword</span><span style="line-height: 28.7999992370605px;">.py就会被拷贝到python类路径下,可以被导入使用。</span></p><p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">    <img src="https://img-blog.csdn.net/20140713140638484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2RrdWl3ZWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" /></p><p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;"><span style="line-height: 28.7999992370605px;"></span></p><p class="p0" style="margin-bottom:0pt; margin-top:0pt; "><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">对于Windows,可以执行</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">python setup.py bdist_wininst</span><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">生成一个exe文件;若要生成RPM包,执行</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">python setup.py bdist_rpm</span><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">,但系统必须有rpm命令的支持。</span><span style="color: rgb(85, 85, 85); font-size: 9pt; font-family: 宋体; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><o:p></o:p></span></p><p class="p0" style="margin-bottom:0pt; margin-top:0pt; "><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">setup函数还有一些参数:</span><span style="font-size: 12pt; font-family: 微软雅黑; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><o:p></o:p></span></p><span style="white-space:pre">	</span><p class="p0" style="margin-bottom:30.0000pt; margin-top:0pt; text-autospace:ideograph-other; line-height:18.9000pt; "><span style="font-size: 12pt; font-family: 微软雅黑;">1、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">packages</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">告诉Distutils需要处理那些包(包含</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">__init__.py</span><span style="font-size: 12pt; font-family: 微软雅黑;">的文件夹)</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">2、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">package_dir</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">告诉Distutils哪些目录下的文件被映射到哪个源码包。一个例子:</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">package_dir = {'': 'lib'}</span><span style="font-size: 12pt; font-family: 微软雅黑;">,表示“root package”中的模块都在lib目录中。</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">3、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">ext_modules</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">是一个包含Extension实例的列表,Extension的定义也有一些参数。</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">4、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">ext_package</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">定义extension的相对路径</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">5、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">requires</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">定义依赖哪些模块</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">6、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">provides</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">定义可以为哪些模块提供依赖</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">7、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">scripts</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">指定python源码文件,可以从命令行执行。在安装时指定</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">--install-script</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">8、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">package_data</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">通常包含与包实现相关的一些数据文件或类似于readme的文件。如果没有提供模板,会被添加到MANIFEST文件中。</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">9、</span><span style="font-size: 10.5pt; font-family: Courier; background: rgb(238, 238, 238);">data_files</span><span style="font-size: 12pt; font-family: 微软雅黑;">
</span><span style="font-size: 12pt; font-family: 微软雅黑;">指定其他的一些文件(如配置文件)</span></p><p class="p0" style="margin-bottom:30.0000pt; margin-top:0pt; text-autospace:ideograph-other; line-height:18.9000pt; "><span style="font-size: 16px; line-height: 1.8em; background-color: rgb(255, 255, 255);">规定了哪些文件被安装到哪些目录中。如果目录名是相对路径,则是相对于</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; font-size: 14px; word-wrap: break-word; background: rgb(238, 238, 238);">sys.prefix</code><span style="font-size: 16px; line-height: 1.8em; background-color: rgb(255, 255, 255);">或</span><code style="margin: 0px; padding: 0px 5px; border: 1px solid rgb(204, 204, 204); font-family: Courier; line-height: 14px; font-size: 14px; word-wrap: break-word; background: rgb(238, 238, 238);">sys.exec_prefix</code><span style="font-size: 16px; line-height: 1.8em; background-color: rgb(255, 255, 255);">的路径。如果没有提供模板,会被添加到MANIFEST文件中。</span></p>

执行sdist命令时,默认会打包哪些东西呢?

  • 所有由py_modulespackages指定的源码文件
  • 所有由ext_moduleslibraries指定的C源码文件
  • scripts指定的脚本文件
  • 类似于test/test*.py的文件
  • README.txt或README,setup.py,setup.cfg
  • 所有package_datadata_files指定的文件
 



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值