Python库打包到PyPI

来源:互联网 发布:专业美工软件 编辑:程序博客网 时间:2024/06/05 06:09

打开pypi官网, 并注册账号

https://pypi.python.org/

创建并编辑.pypirc (注: 家目录下创建)

tianshl@tianshl ~ $ vim .pypirc[pypirc]index-servers =    pypi    pypitest[pypi]repository=https://pypi.python.org/pypi[pypitest]repository=https://testpypi.python.org/pypi[server-login]username:tianshlpassword:******

前提

1. 要打包的代码必须是一个包(package)2. 代码是开源的

包(package)

创建包
右键 / New / Python Package / 输入包名 / OK
创建成功后,查看目录结构
tianshl@tianshl wechat $ tree.└── wxReply    └── __init__.py实际上就是文件夹中包含__init__.py文件

编写要打包的源代码

tianshl@tianshl wechat $ tree.└── wxReply    ├── __init__.py    └── wxReply.py1 directory, 2 files

创建README.rst和setup.py文件

README.rst为说明文档setup.py为安装脚本 (核心)
此时wechat包的目录结构
tianshl@tianshl wechat $ tree.├── README.rst├── setup.py└── wxReply    ├── __init__.py    └── wxReply.py1 directory, 4 files

编辑setup.py内容

# -*- coding: utf-8 -*-from setuptools import setup, find_packagesfrom codecs import openfrom os import pathhere = path.abspath(path.dirname(__file__))with open(path.join(here, 'README.rst'), encoding='utf-8') as f:    long_description = f.read()__author__ = 'tianshl'__date__ = '2017/01/26'setup(    name='wxReply',                                 # 名称    version='1.0.6',                                # 版本号    description='wxReply',                          # 简单描述    long_description=long_description,              # 详细描述    classifiers=[        'License :: OSI Approved :: MIT License',        'Programming Language :: Python',        'Intended Audience :: Developers',        'Operating System :: OS Independent',    ],    keywords='wechat robot weixin wxReply',         # 关键字    author='tianshl',                               # 作者    author_email='xiyuan91@126.com',                # 邮箱    url='https://my.oschina.net/tianshl/blog',      # 包含包的项目地址    license='MIT',                                  # 授权方式    packages=find_packages(),                       # 包列表    install_requires=['requests', 'itchat'],    include_package_data=True,    zip_safe=True,)

校验 (python setup.py check)

tianshl@tianshl wxReply $ python3 setup.py checkrunning check注: running check 表示没问题, 其他情况对照提示修改即可

打包 (python setup.py sdist)

tianshl@tianshl wechat $ python3 setup.py sdistrunning sdistrunning egg_infocreating wxReply.egg-infowriting wxReply.egg-info/PKG-INFOwriting dependency_links to wxReply.egg-info/dependency_links.txtwriting requirements to wxReply.egg-info/requires.txtwriting top-level names to wxReply.egg-info/top_level.txtwriting manifest file 'wxReply.egg-info/SOURCES.txt'reading manifest file 'wxReply.egg-info/SOURCES.txt'writing manifest file 'wxReply.egg-info/SOURCES.txt'running checkcreating wxReply-1.0.6creating wxReply-1.0.6/wxReplycreating wxReply-1.0.6/wxReply.egg-infocopying files to wxReply-1.0.6...copying README.rst -> wxReply-1.0.6copying setup.py -> wxReply-1.0.6copying wxReply/__init__.py -> wxReply-1.0.6/wxReplycopying wxReply/wxReply.py -> wxReply-1.0.6/wxReplycopying wxReply.egg-info/PKG-INFO -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/SOURCES.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/dependency_links.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/requires.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/top_level.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/zip-safe -> wxReply-1.0.6/wxReply.egg-infoWriting wxReply-1.0.6/setup.cfgcreating distCreating tar archiveremoving 'wxReply-1.0.6' (and everything under it)
此时wechat包的目录结构
tianshl@tianshl wechat $ tree.├── README.rst├── dist│   └── wxReply-1.0.6.tar.gz├── setup.py├── wxReply│   ├── __init__.py│   └── wxReply.py└── wxReply.egg-info    ├── PKG-INFO    ├── SOURCES.txt    ├── dependency_links.txt    ├── requires.txt    ├── top_level.txt    └── zip-safe3 directories, 11 files

上传

tianshl@tianshl wxReply $ python3 setup.py sdist register uploadrunning sdistrunning egg_infowriting wxReply.egg-info/PKG-INFOwriting dependency_links to wxReply.egg-info/dependency_links.txtwriting requirements to wxReply.egg-info/requires.txtwriting top-level names to wxReply.egg-info/top_level.txtreading manifest file 'wxReply.egg-info/SOURCES.txt'writing manifest file 'wxReply.egg-info/SOURCES.txt'running checkcreating wxReply-1.0.6creating wxReply-1.0.6/wxReplycreating wxReply-1.0.6/wxReply.egg-infocopying files to wxReply-1.0.6...copying README.rst -> wxReply-1.0.6copying setup.py -> wxReply-1.0.6copying wxReply/__init__.py -> wxReply-1.0.6/wxReplycopying wxReply/wxReply.py -> wxReply-1.0.6/wxReplycopying wxReply.egg-info/PKG-INFO -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/SOURCES.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/dependency_links.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/requires.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/top_level.txt -> wxReply-1.0.6/wxReply.egg-infocopying wxReply.egg-info/zip-safe -> wxReply-1.0.6/wxReply.egg-infoWriting wxReply-1.0.6/setup.cfgCreating tar archiveremoving 'wxReply-1.0.6' (and everything under it)running registerRegistering wxReply to https://upload.pypi.org/legacy/Server response (410): Project pre-registration is no longer required or supported, so continue directly to uploading files.running uploadSubmitting dist/wxReply-1.0.6.tar.gz to https://upload.pypi.org/legacy/Server response (200): OK

测试

测试是否上传成功
tianshl@tianshl wechat $ pip3 search wxReplywxReply (1.0.6)  - wxReply
测试能否安装成功
tianshl@tianshl ~ $ pip3 install wxReplyCollecting wxReply  Downloading wxReply-1.0.6.tar.gzRequirement already satisfied: requests in /usr/local/lib/python3.6/site-packages (from wxReply)Requirement already satisfied: itchat in /usr/local/lib/python3.6/site-packages (from wxReply)Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/site-packages (from requests->wxReply)Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python3.6/site-packages (from requests->wxReply)Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/site-packages (from requests->wxReply)Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python3.6/site-packages (from requests->wxReply)Requirement already satisfied: pyqrcode in /usr/local/lib/python3.6/site-packages (from itchat->wxReply)Requirement already satisfied: pypng in /usr/local/lib/python3.6/site-packages (from itchat->wxReply)Building wheels for collected packages: wxReply  Running setup.py bdist_wheel for wxReply ... done  Stored in directory: /Users/tianshl/Library/Caches/pip/wheels/49/26/9e/883fd73919e7c2cce794b0acc212216441ced601d6062e2941Successfully built wxReplyInstalling collected packages: wxReplySuccessfully installed wxReply-1.0.6
测试能否正常引入
tianshl@tianshl ~ $ python3Python 3.6.2 (default, Jul 17 2017, 16:44:45) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import wxReply>>> 
原创粉丝点击