自从接触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分发版库的必要的知识。以及详细的一个流程介绍。

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

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

  1. 如何在Pypi上发表自己的Python库

    背景 最近兴趣使然写了几个Python库,也发布到了Pypi上,虽然没什么人下载,但自己在其他机器上用着也会很方便.这里我向大家介绍一下如何在Pypi上发表自己的Python库. 准备 注册账号 很显 ...

  2. 矩池云上编译安装dlib库

    方法一(简单) 矩池云上的k80因为内存问题,请用其他版本的GPU去进行编译,保存环境后再在k80上用. 准备工作 下载dlib的源文件 进入python的官网,点击PyPi选项,搜索dilb,再点击 ...

  3. 造一个轮子然后安装到pypi上

    之前写了一个爬虫的包,主要是根据自己写爬虫的情况总结一下. 因为每次都要重复写一些代码,所以提炼出来,类似一个框架的样子吧. 开始是放在自己的项目里引用,但如果换了一个项目,就得重新拷一遍,很麻烦. ...

  4. pyCharm上解决安装不上pandas库问题

    最近在PyCharm上安装pandas库的时候,总是安装不上,提示好像是pip除了错误.我使用的是python .4版本.最后判断应该是自己pip版本应该太旧了,最后再cmd更新了pip之后就行了.如 ...

  5. 在pypi上发布python包详细教程

    使用Python编程中Python的包安装非常方便,一般都是可以pip来安装搞定:pip install <package name>,我们自己写的python也可以发布在pypi上,很简 ...

  6. Python3: Windows系统上同时安装Python2和Python3

    Python3: Windows系统上同时安装Python2和Python3 为什么要同时安装Python2和Python3环境呢? 因为一些库只支持Python2或者Python3; 在同一台电脑上 ...

  7. Pycharm(五)安装库和虚拟环境

    随便进入一个项目 进入设置    Ctrl + alt + S Project *********  - Project Interpreter     ******是你的项目名字 Project I ...

  8. 实战教程:如何将自己的Python包发布到PyPI上

    1. PyPi的用途 Python中我们经常会用到第三方的包,默认情况下,用到的第三方工具包基本都是从Pypi.org里面下载. 我们举个栗子: 如果你希望用Python实现一个金融量化分析工具,目前 ...

  9. jemalloc在linux上从安装到使用

    jemalloc在linux上从安装到使用 上次在引导大家安装Redis时提到可能会报错:  发现了redis有用到jemalloc. 首先,jemalloc是干什么的? 我们看看作者自己的介绍: j ...

随机推荐

  1. 浅谈linux静态库、动态库。

    动态库又叫动态共享文件(.so,Dynamic Shared Objects)和静态库(.a)都是将一些待重用的公共代码打包成一种特殊的重定位目标文件. 在使用时,连接器会将静态库中所有的代码,编译到 ...

  2. str_replace替换换行符失败原因

    在编程中,需要替换掉字符串的换行符再存进数据库, 语句: $str = str_replace(array('/r/n', '/r', '/n'), $str); 发现语句并没有替换,网上查找后,知道 ...

  3. Caffe的运行mnist手写数字识别

    老规矩,首先附上官方教程:http://caffe.berkeleyvision.org/gathered/examples/mnist.html 1.必要软件 因为Caffe中使用的是Linux才能 ...

  4. PKUWC2018划水记

    PKUWC2018划水记 Day -1 ​ 从福州出发去长沙,原本是预定Day0当天的航班,后来怕来不及提前到了今天. ​ 由于最近长沙下雪,所以听说飞机取消了很多班次,所以早上起来的时候还特地看了一 ...

  5. ●洛谷P3168 [CQOI2015]任务查询系统

    题链: https://www.luogu.org/problemnew/show/P3168题解: 主席树 强制在线? 那就直接对每一个前缀时间建一个线段树(可持久化线段树),线段树维护优先度权值. ...

  6. 【LSGDOJ 1351】关灯

    题目描述 多瑞卡得到了一份有趣而高薪的工作.每天早晨他必须关掉他所在村庄的街灯.所有的街灯都被设置在一条直路的同一侧. 多瑞卡每晚到早晨 5 点钟都在晚会上,然后他开始关灯.开始时,他站在某一盏路灯的 ...

  7. hdu 5120(2014北京—求圆相交)

    题意:求环的相交面积 思路: 通过画图可知,面积= 大圆相交面积 - 大小圆相交面积*2 + 小小圆相交面积  再通过圆相交模板计算即可 #include <iostream> #incl ...

  8. BZOJ3817 Sum(类欧几里得算法)

    设$t=\sqrt r$,原题转化为$\sum_{x=1}^n(4*\lfloor\frac{tx}2\rfloor-2*\lfloor tx\rfloor+1)$考虑如何求$\sum_{x=1}^n ...

  9. 浅谈Java中的equals和==与hashCode

    转载:https://www.cnblogs.com/dolphin0520/p/3592500.html 参考:http://blog.csdn.net/yinzhijiezhan/article/ ...

  10. 在腾讯云的ubuntu服务器上面安装git服务器

    GitHub是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用.搭建Git服 ...