制作pypi上的安装库

    • 下载地址
    • 如何制作分发工具呢
      • setuppy
      • 源码包
      • 其他文件
      • 制作过程
        • 首先上场的肯定是setuppy了如下
        • 然后是LICENCE
        • 注册
    • 测试
    • 总结

自从接触Python以来也有几个月了,虽然主要的开发语言还是Java,但对Python也算是情有独钟了。大家也都知道Python为什么会这么火,很大的一个原因就是其有丰富而且强大的库支持,一直以来都是在用别人的库,这次博主也尝试着自己做了一个分发版。


下载地址

制作的是一个命令行下的翻译工具,可以实现英汉中单词,句子的互译。当然了,也是借助了第三方的接口的。

如何制作分发工具呢?

先来看下面的这个文件目录吧。

这个就是比较基础的了,其中必须的文件是setup.pymytranslator文件夹 其他的不是必须的选项。
其他更加细节性的不妨参考:https://packaging.python.org/distributing/#uploading-your-project-to-pypi
https://wiki.python.org/moin/TestPyPI

由于全是英文,所以需要耐下心来慢慢的品读咯。

setup.py

我们打包,以及安装Python源码的时候依赖的就是这个文件,里面配置好了安装和使用一个Python库的全部的信息。比较重要,因为官方的解释也比较的详细,就看看人家的官方配置吧,然后咱们按需进行调整即可。

"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='sample',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='1.2.0',

    description='A sample Python project',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/pypa/sampleproject',

    # Author details
    author='The Python Packaging Authority',
    author_email='pypa-dev@googlegroups.com',

    # Choose your license
    license='MIT',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: MIT License',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],

    # What does your project relate to?
    keywords='sample setuptools development',

    # You can just specify the packages manually here if your project is
    # simple. Or you can use find_packages().
    packages=find_packages(exclude=['contrib', 'docs', 'tests']),

    # Alternatively, if you want to distribute just a my_module.py, uncomment
    # this:
    #   py_modules=["my_module"],

    # List run-time dependencies here.  These will be installed by pip when
    # your project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/requirements.html
    install_requires=['peppercorn'],

    # List additional groups of dependencies here (e.g. development
    # dependencies). You can install these using the following syntax,
    # for example:
    # $ pip install -e .[dev,test]
    extras_require={
        'dev': ['check-manifest'],
        'test': ['coverage'],
    },

    # If there are data files included in your packages that need to be
    # installed, specify them here.  If using Python 2.6 or less, then these
    # have to be included in MANIFEST.in as well.
    package_data={
        'sample': ['package_data.dat'],
    },

    # Although 'package_data' is the preferred approach, in some case you may
    # need to place data files outside of your packages. See:
    # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa
    # In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
    data_files=[('my_data', ['data/data_file'])],

    # To provide executable scripts, use entry points in preference to the
    # "scripts" keyword. Entry points provide cross-platform support and allow
    # pip to create the appropriate form of executable for the target platform.
    entry_points={
        'console_scripts': [
            'sample=sample:main',
        ],
    },
)

源码包

这里关系到了包的概念了,通俗的来讲就是包含了一个__init__.py文件的文件夹,大家姑且可以这样理解!然后这个文件夹包含了能够运行我们代码的最小组件,函数库。这样就可以称之为一个包了。

博主这里的小工具比较的简单,如下:


 E:\Code\Python\DataStructor\translate\mytranslator 的目录

2016/10/08  15:52    <DIR>          .
2016/10/08  15:52    <DIR>          ..
2016/09/29  09:12                50 mytranslate.bat
2016/10/08  15:50             1,221 translate.py
2016/10/08  15:51               151 __init__.py
               3 个文件          1,422 字节
               2 个目录 87,527,870,464 可用字节

其他文件

其他文件是为了让你的用户对这个项目有更加深刻的了解而存在的,跟打包程序关联不大,但是这并不是说不重要。尤其是README.md文件和LICENCE文件,这都是非常的有价值的。LICENCE的概念大家可能比较的陌生,有兴趣的可以参考博主之前转载的一篇文章。http://blog.csdn.net/marksinoberg/article/details/52337214

各种开源协议可以归纳如下:

(图片转载至阮一峰博客)

制作过程

下面谈谈博主制作这个小工具的过程吧。

首先上场的肯定是setup.py了。如下:

# coding:utf-8
from setuptools import setup, find_packages

setup(
    name='mytranslator',
    description='Simple translator in English and Chinese, easier to use',
    url='https://github.com/guoruibiao/MyTranslator',
    version='1.0.1',
    license='MIT',
    packages=find_packages(),
    entry_points = {
        "console_points": ['trans = mytranslator.translate:main']
    },
)

然后是LICENCE

MIT License

Copyright (c) 2016

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

这样基本上就可以了。

注册

注册之前,最好是在官网上注册一个账号,因为这对今后的上传自己的库会很有帮助。

官网如下: https://pypi.python.org/pypi/

需要注意的就是用户名为ASCII支持的,密码为字母加数字即可。完成之后就可以进行接下来的工作了。



在项目的根目录下输入:

python setup.py register sdist upload

然后会有一个选项,默认选择为数字1,咱们可以按自己的情况来输入。博主由于注册了一个账号于是选择1.

最后出现如下字样,就说明您的库已经上传到了pypi上面了。

running upload
Submitting dist\mytranslator-1.0.1.zip to https://pypi.python.org/pypi
Server response (200): OK

这个时候你会发现自己项目的文件夹下面多了一点东西。基本上如下图所示。

测试

接下来,Python2.7的小伙伴们就可以自己下载博主的这个库了。其实它到底有什么用呢?

其实就是个可以在命令行下面实现英语汉语单词,句子的翻译。而且对于Window尤其优化了编码的支持。详细的可以参考博主之前的文章http://blog.csdn.net/marksinoberg/article/details/52701650

  • 英文转汉语

  • 汉语转英文

好了,废话不多说。下面介绍一下怎么安装这个库吧。

安装的时候,出现下面的字样,那么恭喜您,成功地安装了博主的库了。

(temp) E:\Code\Python\temp\Scripts>pip install mytranslator
Collecting mytranslator
  Downloading mytranslator-1.0.1.zip
Building wheels for collected packages: mytranslator
  Running setup.py bdist_wheel for mytranslator ... done
  Stored in directory: C:\Users\Administrator\AppData\Local\pip\Cache\wheels\ec\29\22\f37da8b8f8fef3
a08472f50c0a084995236ba10cf8af52bb20
Successfully built mytranslator
Installing collected packages: mytranslator
Successfully installed mytranslator-1.0.1

总结

照例,最后一点都是回顾一下这按文章的主要内容。

制作自己的Python分发版库的必要的知识。以及详细的一个流程介绍。

知识点不难,重在尝试,不妨现在就动手,做出你的分发版吧。

时间: 2024-09-20 00:06:27

制作pypi上的安装库的相关文章

Python中pip安装非PyPI官网第三方库的方法

  这篇文章主要介绍了Python中pip安装非PyPI官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非PyPI的URL,本文就给出两种解决方法,需要的朋友可以参考下 在python中安装非自带python模块,有三种方式: 1.easy_install 2.pip 3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令 本文主要针对pip安装时可能会碰到的一种情况,

Symfony2使用第三方库Upload制作图片上传实例详解_php实例

本文实例分析了Symfony2使用第三方库Upload制作图片上传的方法.分享给大家供大家参考,具体如下: 我们在应用程序或者网站的个人资料里一般都有设置头像的功能,这一章我们在Symfony2里用第三方的一个比较有名Upload库来制作上传图片的功能. 一.安装第三方库 1.在composer.json文件中的"require"中加入 "codeguy/upload": "*" 2.运行指令安装 composer update 二.编码 1.编

link环境下,codefirst制作《网盘软件》,“云服务器”上如何安装自己的软件?使用安装器么?

问题描述 link环境下,codefirst制作<网盘软件>,"云服务器"上如何安装自己的软件?使用安装器么? link环境下,codefirst制作<网盘软件>,"云服务器"上如何安装自己的软件?使用安装器么? 解决方案 一般是两种,亿个是服务器支持远程桌面,那么和普通服务器没什么两样.一个是有自己的控制面板,那么可以通过它配置,也有ssh telnet ps之类的命令行.

link环境下制作一款《订餐软件》,如何把本电脑的库合并到服务器上的大库中?会不会冲突?

问题描述 link环境下制作一款<订餐软件>,如何把本电脑的库合并到服务器上的大库中?会不会冲突? link环境下制作一款<订餐软件>,如何把本电脑的库合并到服务器上的大库中?会不会冲突? 解决方案 每个独立的库有一个标识,产生一个流水号.这样放在一个大数据库中,因为前缀的标识不同,就不会重复啦. 解决方案二: 建议你在每个表中都增加一个本地库标识,主键为这个本地库标识以及一个物理主键,这样的话导入到服务器上的大库中就不会冲突,因为本地库标识是不同的

制作软盘上运行的FreeBSD系统

测试平台:FreeBSD 4.5 Release (i386) 大家可能见到过很多在软盘上运行的Linux系统,可在软盘上运行的FreeBSD反而比较少,虽然有PICOBSD,然而很多时候PICOBSD并不能满足我们的需要,那么可不可以自己制作一个在软盘上运行的FreeBSD系统呢?答案是肯定的.我在维护着一个Floppy Firewall的Project,它是一个基于FreeBSD和IPFilter的运行在软盘上的防火墙系统,很多网友在使用了Floppy Firewall之后发邮件来询问如何使

拇指玩」制作的「谷歌安装器」app

作者:匿名用户链接:https://www.zhihu.com/question/57468448/answer/153000587来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 先说Google 服务框架Google 服务框架全称 Google Mobile Service,其中包括了应用包,也就是我们希望用到的各种 Google 应用.Android 虽然开源且自由,但 Google 的应用程序并不包含其中,它们都属于 Google 的知识产权.所以厂家们预装

win7-有关libsvm在Python2.7上的安装问题

问题描述 有关libsvm在Python2.7上的安装问题 大家好,我想安装libsvm,Python2.7版本(Win7) 我的Python是32位的,我也添加了DLL文件到环境变量中,但是引用svm库的时候会出现"不是有效的Win32"错误. 我在网上查了一下,发现这样的问题基本是因为python版本与libsvm不匹配造成的. 但是,我的python是32位,与下载的libsvm相同,所以,我不是很确定原因是什么. 找了好久都没有找到原因,请大家帮帮我,谢谢啦! 解决方案 还需要

ubuntu上编译libvnc库,报 openssl 错误

问题描述 ubuntu上编译libvnc库,报 openssl 错误 错误信息 和 libssl .libcrypto 两个库有关系. 而libssl 依赖于libcrypto,那么可以确认 是libssl的问题 . 但是 我又不敢确定一定是libssl . 系统自带的openssl ,以及 我自己安装的ssl 和libssl-dev ,我把三个 卸了,重新安装.可以依然编译报错 . 解决方案 在ubuntu上编译和交叉编译OpenSSL库在windows下编译openSSL库openssl编译

【云端起舞】Oracle云上一键安装数据库补丁集

编辑手记:为数据库升级打补丁是一项常规的任务,在通常情况下 ,打补丁是一件繁琐的事情,需要考虑的细节比较多.但在云上,可以通过按钮一键式应用 相关补丁集,高效便捷.我们今天一起来学习 Oracle Cloud Database Patching Then patch like a king with single click Database As A Service (DbaaS)  系列文章回顾: 1.Configure and Practice Backup and Recovery in