Python库打包到PyPI

标签: PyPI 打包 Python pip
413793人阅读 评论(0) 收藏 举报
分类:

打开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:tianshl
password:******

前提

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.py

1 directory, 2 files

创建README.rst和setup.py文件

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

1 directory, 4 files

编辑setup.py内容

# -*- coding: utf-8 -*-

from setuptools import setup, find_packages
from codecs import open
from os import path

here = 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 check
running check

注: running check 表示没问题, 其他情况对照提示修改即可

打包 (python setup.py sdist)

tianshl@tianshl wechat $ python3 setup.py sdist
running sdist
running egg_info
creating wxReply.egg-info
writing wxReply.egg-info/PKG-INFO
writing dependency_links to wxReply.egg-info/dependency_links.txt
writing requirements to wxReply.egg-info/requires.txt
writing top-level names to wxReply.egg-info/top_level.txt
writing manifest file 'wxReply.egg-info/SOURCES.txt'
reading manifest file 'wxReply.egg-info/SOURCES.txt'
writing manifest file 'wxReply.egg-info/SOURCES.txt'
running check
creating wxReply-1.0.6
creating wxReply-1.0.6/wxReply
creating wxReply-1.0.6/wxReply.egg-info
copying files to wxReply-1.0.6...
copying README.rst -> wxReply-1.0.6
copying setup.py -> wxReply-1.0.6
copying wxReply/__init__.py -> wxReply-1.0.6/wxReply
copying wxReply/wxReply.py -> wxReply-1.0.6/wxReply
copying wxReply.egg-info/PKG-INFO -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/SOURCES.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/dependency_links.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/requires.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/top_level.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/zip-safe -> wxReply-1.0.6/wxReply.egg-info
Writing wxReply-1.0.6/setup.cfg
creating dist
Creating tar archive
removing '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-safe

3 directories, 11 files

上传

tianshl@tianshl wxReply $ python3 setup.py sdist register upload
running sdist
running egg_info
writing wxReply.egg-info/PKG-INFO
writing dependency_links to wxReply.egg-info/dependency_links.txt
writing requirements to wxReply.egg-info/requires.txt
writing top-level names to wxReply.egg-info/top_level.txt
reading manifest file 'wxReply.egg-info/SOURCES.txt'
writing manifest file 'wxReply.egg-info/SOURCES.txt'
running check
creating wxReply-1.0.6
creating wxReply-1.0.6/wxReply
creating wxReply-1.0.6/wxReply.egg-info
copying files to wxReply-1.0.6...
copying README.rst -> wxReply-1.0.6
copying setup.py -> wxReply-1.0.6
copying wxReply/__init__.py -> wxReply-1.0.6/wxReply
copying wxReply/wxReply.py -> wxReply-1.0.6/wxReply
copying wxReply.egg-info/PKG-INFO -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/SOURCES.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/dependency_links.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/requires.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/top_level.txt -> wxReply-1.0.6/wxReply.egg-info
copying wxReply.egg-info/zip-safe -> wxReply-1.0.6/wxReply.egg-info
Writing wxReply-1.0.6/setup.cfg
Creating tar archive
removing 'wxReply-1.0.6' (and everything under it)
running register
Registering 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 upload
Submitting dist/wxReply-1.0.6.tar.gz to https://upload.pypi.org/legacy/
Server response (200): OK

测试

测试是否上传成功
tianshl@tianshl wechat $ pip3 search wxReply
wxReply (1.0.6)  - wxReply
测试能否安装成功
tianshl@tianshl ~ $ pip3 install wxReply
Collecting wxReply
  Downloading wxReply-1.0.6.tar.gz
Requirement 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/883fd73919e7c2cce794b0acc212216441ced601d6062e2941
Successfully built wxReply
Installing collected packages: wxReply
Successfully installed wxReply-1.0.6
测试能否正常引入
tianshl@tianshl ~ $ python3
Python 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 darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wxReply
>>> 
查看评论

把自己的python脚本打包上传到PYPI

去年9月的时候,写了几个python脚本,用来从一家基金网站爬数据,然后进行分析,给出值得投资的若干只基金。分析的方法很简单,就是取前1个月,3个月,6个月,1年,2年和3年这所有种类的前100名或5...
  • nirendao
  • nirendao
  • 2015-09-22 23:29:42
  • 3801

Python3 上传Python包到PyPi

1.首先,先安装工具twine在cmd输入:pip install twine 2.在Python的包目录下创建setup.py文件例如:from setuptools import setupse...
  • huplion
  • huplion
  • 2017-04-02 21:24:37
  • 631

上传代码到PyPI

我们可以把自己的代码,尤其是期待分享的得意代码,上传分享到第三方Python模块的“集中营” —— PyPI,如果没有账号,可以注册个先。1 准备发布像在本地发布安装一样,先新建一个文件夹,然后将自...
  • kikitaMoon
  • kikitaMoon
  • 2015-07-09 14:26:14
  • 2657

将自己写的Python代码打包放到PyPI上

如果是开源的Python代码,为了能够让大家更方便的使用,放到PyPI上也许是个非常不错的主意(PyPI:Python Package Index)。刚开始我以为要将代码打包放到PyPI上是一件非常复...
  • chenlei_525
  • chenlei_525
  • 2016-07-06 16:50:33
  • 2712

【解决方法】ECall 方法必须打包到系统模块中

今天在做Unity毕业设计,是一个有客户端和服务器端的RPG游戏,可是开启服务器是报错“ECall 方法必须打包到系统模块中” 问题重现: 之前的代码是客户端直接连接数据库的,后来改为So...
  • Q594076608
  • Q594076608
  • 2015-04-18 16:35:41
  • 3786

把自己的独立python模块上传到pypi上去

参考文章  http://blog.csdn.net/crisschan/article/details/51840552 http://stackoverflow.com/questions/156...
  • mengtianwxs
  • mengtianwxs
  • 2017-02-24 16:11:05
  • 494

maven 打包到本地仓库

mvn install:install-file -DgroupId=com.qfang.agent -DartifactId=qfang-agent-facade -Dversion=0.0.1-S...
  • a714115852
  • a714115852
  • 2016-02-29 11:15:43
  • 1652

把图标打包成dll文件

  • 2009年02月24日 22:36
  • 196KB
  • 下载

Unity3D坦克大战

因为要得找工作了,不得不写些文章让HR更多的了解到我,同时能帮到别的同学也是好的。因为接触Unity时间不是特别久,所以这里先帖出早期开发的C#游戏,然后再帖Unity游戏。 另外提醒,我的所有...
  • Q594076608
  • Q594076608
  • 2014-10-19 15:26:26
  • 2077

如何将自己的Python代码打包发布到pypi上

什么是pypi 简单的说pypi是一个python包的仓库,里面有很多别人写好的python库,你可以通过easy_install或者pip进行安装,方便用户更方面的使用你的代码模块。 将代码打...
  • wdxin1322
  • wdxin1322
  • 2017-02-23 23:10:44
  • 4545
    个人资料
    持之以恒
    等级:
    访问量: 602万+
    积分: 2406
    排名: 1万+
    最新评论