『Python』setup.py简介

setup.py应用场合

网上见到其他人这样介绍:

假如我在本机开发一个程序,需要用到python的redis、mysql模块以及自己编写的redis_run.py模块。我怎么实现在服务器上去发布该系统,如何实现依赖模块和自己编写的模块redis_run.py一起打包,实现一键安装呢?同时将自己编写的redis_run.py模块以exe文件格式安装到python的全局执行路径C:\Python27\Scripts下呢?在这种应用场景下,pip工具似乎派不上了用场,只能使用python的构建工具setup.py了,使用此构建工具可以实现上述应用场景需求,只需在 setup.py 文件中写明依赖的库和版本,然后到目标机器上使用python setup.py install安装。

可以看到其目的就是方便我们安装代码:可以一体化的安装依赖包,并记录生成包相关信息,方便大规模部署,故对于开发者而言,编写python的第三方库,最重要的事情之一就是编写setup.py了。

使用setup例子

我们建立一个名为demo的空白文件夹,在里面创建setup.py文件,录入如下代码:

from setuptools import setup, find_packages
setup(
	name = "demo",
	version = "0.1",
	packages = find_packages(),
	)

 运行命令:

python setup.py bdist_egg

 此时查看目录树:

│  setup.py

├─build
│  └─bdist.win-amd64
├─demo.egg-info
│      dependency_links.txt
│      PKG-INFO
│      SOURCES.txt
│      top_level.txt

└─dist
        demo-0.1-py3.6.egg

其中dist目录下即为打包好的egg文件,可以将尾缀改为zip并解压,内部是一系列自动生成的文件,可以自行查阅功能,

│  demo-0.1-py3.6.zip

└─EGG-INFO
        dependency_links.txt
        PKG-INFO
        SOURCES.txt
        top_level.txt
        zip-safe

如果我们的生成命令使用的是:

python setup.py sdist

生成的将会是tar.gz文件,解压后直接包含setup.py文件,我们使用python setup.py install运行该文件可以直接将包拷贝到python工作目录下,方便直接调用。

在项目Mask_RCNN中,我们最终可以在anaconda目录中找到如下文件,

其对应的安装选项如下,可以一窥setup的效用,

setup(
    name='mask-rcnn',
    version='2.1',
    url='https://github.com/matterport/Mask_RCNN',
    author='Matterport',
    author_email='waleed.abdulla@gmail.com',
    license='MIT',
    description='Mask R-CNN for object detection and instance segmentation',
    packages=["mrcnn"],  # setup.py同级目录下文件夹mrcnn中含有__init__.py文件
    install_requires=install_reqs,
    include_package_data=True,
    python_requires='>=3.4',
    long_description="""This is an implementation of Mask R-CNN on Python 3, Keras, and TensorFlow. 
The model generates bounding boxes and segmentation masks for each instance of an object in the image. 
It's based on Feature Pyramid Network (FPN) and a ResNet101 backbone.""",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "Intended Audience :: Information Technology",
        "Intended Audience :: Education",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: MIT License",
        "Natural Language :: English",
        "Operating System :: OS Independent",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "Topic :: Scientific/Engineering :: Image Recognition",
        "Topic :: Scientific/Engineering :: Visualization",
        "Topic :: Scientific/Engineering :: Image Segmentation",
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
    ],
    keywords=["image instance segmentation object detection mask rcnn r-cnn tensorflow keras", ]
)

其他资料

官方文档:Writing the Setup Script

Python 库打包分发(setup.py 编写)简易指南

关于python中的setup.py

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值