制作pypi上的安装库
自从接触Python以来也有几个月了,虽然主要的开发语言还是Java,但对Python也算是情有独钟了。大家也都知道Python为什么会这么火,很大的一个原因就是其有丰富而且强大的库支持,一直以来都是在用别人的库,这次博主也尝试着自己做了一个分发版。
下载地址
制作的是一个命令行下的翻译工具,可以实现英汉中单词,句子的互译。当然了,也是借助了第三方的接口的。
源代码托管在Github上:https://github.com/guoruibiao/MyTranslator
pip安装
pip install mytranslator
在pypi官方平台上,也是可以下载到的。如下:
如何制作分发工具呢?
先来看下面的这个文件目录吧。
这个就是比较基础的了,其中必须的文件是setup.py和mytranslator文件夹 其他的不是必须的选项。
其他更加细节性的不妨参考: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上的安装库的更多相关文章
- 如何在Pypi上发表自己的Python库
背景 最近兴趣使然写了几个Python库,也发布到了Pypi上,虽然没什么人下载,但自己在其他机器上用着也会很方便.这里我向大家介绍一下如何在Pypi上发表自己的Python库. 准备 注册账号 很显 ...
- 矩池云上编译安装dlib库
方法一(简单) 矩池云上的k80因为内存问题,请用其他版本的GPU去进行编译,保存环境后再在k80上用. 准备工作 下载dlib的源文件 进入python的官网,点击PyPi选项,搜索dilb,再点击 ...
- 造一个轮子然后安装到pypi上
之前写了一个爬虫的包,主要是根据自己写爬虫的情况总结一下. 因为每次都要重复写一些代码,所以提炼出来,类似一个框架的样子吧. 开始是放在自己的项目里引用,但如果换了一个项目,就得重新拷一遍,很麻烦. ...
- pyCharm上解决安装不上pandas库问题
最近在PyCharm上安装pandas库的时候,总是安装不上,提示好像是pip除了错误.我使用的是python .4版本.最后判断应该是自己pip版本应该太旧了,最后再cmd更新了pip之后就行了.如 ...
- 在pypi上发布python包详细教程
使用Python编程中Python的包安装非常方便,一般都是可以pip来安装搞定:pip install <package name>,我们自己写的python也可以发布在pypi上,很简 ...
- Python3: Windows系统上同时安装Python2和Python3
Python3: Windows系统上同时安装Python2和Python3 为什么要同时安装Python2和Python3环境呢? 因为一些库只支持Python2或者Python3; 在同一台电脑上 ...
- Pycharm(五)安装库和虚拟环境
随便进入一个项目 进入设置 Ctrl + alt + S Project ********* - Project Interpreter ******是你的项目名字 Project I ...
- 实战教程:如何将自己的Python包发布到PyPI上
1. PyPi的用途 Python中我们经常会用到第三方的包,默认情况下,用到的第三方工具包基本都是从Pypi.org里面下载. 我们举个栗子: 如果你希望用Python实现一个金融量化分析工具,目前 ...
- jemalloc在linux上从安装到使用
jemalloc在linux上从安装到使用 上次在引导大家安装Redis时提到可能会报错: 发现了redis有用到jemalloc. 首先,jemalloc是干什么的? 我们看看作者自己的介绍: j ...
随机推荐
- Windows下Java开发环境安装与配置
1. 前往Oracle网站下载JDK程序并安装. http://www.oracle.com/technetwork/java/javase/downloads/index.html 目前最新的版本为 ...
- CodeForces 912d fishes(优先队列+期望)
While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 机器学习技法:12 Neural Network
Roadmap Motivation Neural Network Hypothesis Neural Network Learning Optimization and Regularization ...
- 实验吧_登陆一下好吗(骚注入)&你真的会PHP吗?(代码审计)
登陆一下好吗 首先看到两个输入框,分别对应账号密码,随手输个admin,admin进去,提交后发现有回显,既然题目说了过滤了一切,那就先看看过滤了些啥 经过一波测试,发现服务器过滤了union,sel ...
- Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)
Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...
- 如何让Mac、Windows可以互相远程
您可以通过Mac来远程Windows桌面:也可通过Windows来远程Mac界面:甚至还可以通过iOS或Android来远程Mac或Windows. Windows的操作方法,以Windows XP ...
- STM8操作LCD5110总结
附上一小段代码: void LCD_init(void) { // 产生一个让LCD复位的低电平脉冲 //LCD_RST = 0; GPIO_WriteLow(LCD_PORTG, LCD_RST); ...
- Lua和C#调用探秘
转载请标明出处:http://www.cnblogs.com/zblade/ 在实际的项目中,大部分业务逻辑 程序员只需要负责lua层编写逻辑即可,或者在c#层添加一些静态函数,供lua层调用.那么对 ...
- PostgreSQL 查看单表大小
1. 在数据库中查单个表 select pg_size_pretty(pg_relation_size('table_name')); 2. 查出并按大小排序 SELECT table_schema ...