如何将python源码打包上传到代码仓库pypi


目的

将项目封装为python模块;

需要注意的地方,比如对其他包的依赖,内部结构(子包),版本控制,目标用户和包的形式(源文件还是二进制文件)

示例

代码

根目录包含了各种各样的配置文件(setup.py 是一个必须的并且最重要的配置文件),代码本身通常在一个子目录下,目录名就是包的名称。当然最好还有一个测试文件目录

├── LICENSE ├── MANIFEST.in ├── README.md ├── benyo_zbx ├── __init__.py └── lib ├── __init__.py ├── alert.py ├── base.py ├── event.py ├── host.py ├── item.py ├── service.py ├── history.py └── trigger.py ├── docs ├── test ├── documents.txt └── releasing.rst ├── tests ├── manuals.rst ├── changelog.txt └── changelog.txt ├── requirements.txt ├── setup.cfg ├── setup.py ├── test-requirements.txt └── requirements.txt 

它从setuptools 包中导入了setup()函数;

setup.py 是一个常规的 Python文件; 可以在文件里面做任何你想做的事情,主要工作是用适当的参数去调用 setup() 函数。因为在安装包的时候,setup() 函数将会被各种各样的工具以标准的方法调用。

配置文件

setup.py
- name:包的名称(以及如何在 PYPI 上呈现)
- version:这对于保持适当的依赖关系至关重要
- url:包的链接,通常为 Github 上的链接,或者是 readthedocs 链接
- packages:需要包含的子包列表,find_packages()将帮助我们查找
- setup_requires:指定依赖项
- test_suite:测试时运行的工具

  

详细代码如下

import codecs
import os
import sys

try:
    from setuptools import setup
except:
    from distutils.core import setup


def read(fname):
    return codecs.open(os.path.join(os.path.dirname(__file__), fname)).read()

long_des = read("README.rst")
platforms = ['linux/Windows']
classifiers = [ 'Development Status :: 3', 'Topic :: Text Processing', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.7', ] install_requires = [ #'numpy>=1.11.1', #'pandas>=0.19.0' 'requests' ] setup(name='benyo_zbx', version='0.0.1', description='A test module for DataXujing', long_description=long_des, py_modules=['benyo_zbx'], author = "Benyo yuan", author_email = "yuyuhupo@outlook.com" , url = "https://benyo.github.io" , license="Apache License, Version 2.0", platforms=platforms, classifiers=classifiers, install_requires=install_requires )

 

MANIFEST.in

此文件中包含有不属于内部包目录,但你仍想纳入进来的文件。这些文件通常是 readme 文件,license 文件以及一些类似的文件。其中,比较重要的一个是 requirements.txt。 pip 使用该文件安装其他必须的包。

include README.rst
include LICENSE
include requirements.txt
recursive-include benyo_zbx *
recursive-include docs *
recursive-include tests *

 

recursive-includ表明包含子目录。别急!!!还有一件事要做,就是在setup.py中将include_package_data参数设为True。

setup(name='benyo_zbx',
      version='1.0.0',
      description='A zabbix api for pypi',
      long_description=long_des,
      author="Benyo yuan",
      packages=['benyo_zbx'],
      author_email="yuyuhupo@outlook.com",
      url="https://benyo.github.io",
      license="Apache License, Version 2.0",
      platforms=platforms,
      classifiers=classifiers,
      install_requires=install_requires,
      include_package_data=True
      )
 packages 就是这个参数没有加,导致在安装python包的时候,不能正常使用;
 long_description=__doc__, 从代码中获取文档注释
 install_requires=[ # 依赖列表 'Flask>=0.10', 'Flask-SQLAlchemy>=1.5,<=2.1' ],
 dependency_links=[ # 依赖包下载路径 'http://example.com/dependency.tar.gz' ]
依赖项

您可以在 setup.py 文件的 install_requires 部分和 requirements.txt 文件中指定依赖项。Pip 将会自动安装 install_requires 中列出的依赖项,而不是 requirements.txt 文件。要安装后者中指定的依赖项,在运行 pip 时必须明确指定:pip install -r requirements.txt。

install_requires 选项旨在指定所要求模块的最低主版本号等较抽象的要求。而在 requirements.txt 文件的要求更加具体,通常细致到次版本号。

发布

两种发布形式:一是源代码发布;二是二进制版本发布。

 

源发布版

使用python setup.py sdist build命令创建发布文件。 下面是创建时的输出

running sdist
running egg_info
creating benyo-zbx.egg-info
writing requirements to benyo-zbx.egg-info\requires.txt
writing benyo-zbx.egg-info\PKG-INFO
writing top-level names to benyo-zbx.egg-info\top_level.txt
writing dependency_links to benyo-zbx.egg-info\dependency_links.txt
writing manifest file 'benyo-zbx.egg-info\SOURCES.txt'
file benyo-zbx.py (for module Pyzabbix) not found
reading manifest file 'benyo-zbx.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'benyo-zbx'
warning: no files found matching 'docs'
warning: no files found matching 'tests'
writing manifest file 'benyo-zbx.egg-info\SOURCES.txt'
running check
creating benyo-zbx-0.6.1
creating benyo-zbx-0.6.1\benyo-zbx.egg-info
copying files to benyo-zbx-0.6.1...
copying LICENSE -> benyo-zbx-0.6.1
copying MANIFEST.in -> benyo-zbx-0.6.1
copying README.rst -> benyo-zbx-0.6.1
copying setup.py -> benyo-zbx-0.6.1
copying benyo-zbx.egg-info\PKG-INFO -> benyo-zbx-0.6.1\benyo-zbx.egg-info
copying benyo-zbx.egg-info\SOURCES.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info
copying benyo-zbx.egg-info\dependency_links.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info
copying benyo-zbx.egg-info\requires.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info
copying benyo-zbx.egg-info\top_level.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info
Writing benyo-zbx-0.6.1\setup.cfg
creating dist
Creating tar archive
removing 'benyo-zbx-0.6.1' (and everything under it)

  

打包过程中会创建一些文件(夹)

├── build └── bdist.win-amd64 ├── dist ├── Pyzabbix-0.6.1.tar.gz └── Pyzabbix-0.6.1-py2.7.egg └── Pyzabbix.egg-info ├── dependency_link.txt ├── PKG_INFO ├── requires.txt ├── SOURCES.txt └── top_level.txt 

测试安装(两种方式)

# 当前路径下 1、python setup.py install # 使用pip安装 2、pip install dist/Pyzabbix-0.6.1.tar.gz
 Processing d:\pypi ???\pyzabbix\dist\benyo-zbx-0.6.1.tar.gz
 Requirement already satisfied: numpy>=1.11.1 in c:\python27\lib\site-packages (from benyo-zbx==0.6.1)
 Requirement already satisfied: pandas>=0.19.0requests in c:\python27\lib\site-packages (from benyo-zbx==0.6.1)
 Requirement already satisfied: pytz>=2011k in c:\python27\lib\site-packages (from pandas>=0.19.0requests->benyo-zbx==0.6.1)
 Requirement already satisfied: python-dateutil>=2.5.0 in c:\python27\lib\site-packages (from pandas>=0.19.0requests->benyo-zbx==0.6.1)
 Requirement already satisfied: six>=1.5 in c:\python27\lib\site-packages (from python-dateutil>=2.5.0->pandas>=0.19.0requests->benyo-zbx==0.6.1)
 Installing collected packages: benyo-zbx
 Running setup.py install for benyo-zbx... done
 Successfully installed benyo-zbx-0.6.1

 

导入包,测试功能是否正常

# python
>>> from benyo_zbx import ZabbixFace
>>>

 

测试成功 ! @_@

 

 Wheel(二进制)

Wheels 是打包 Python 代码和扩展 C 语言的一个相对新的方法;替换了 egg 格式。

创建纯 Python 或者平台 Wheels 的命令是:"python setup.py bdist_wheel --universal"

running bdist_wheel
running build
installing to build\bdist.win-amd64\wheel
running install
running install_egg_info
running egg_info
writing requirements to Pyzabbix.egg-info\requires.txt
writing benyo-zbx.egg-info\PKG-INFO
writing top-level names to benyo-zbx.egg-info\top_level.txt
writing dependency_links to benyo-zbx.egg-info\dependency_links.txt
reading manifest file 'benyo-zbx.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'benyo-zbx.egg-info\SOURCES.txt'
Copying Pyzabbix.egg-info to build\bdist.win-amd64\wheel\.\benyo-zbx-0.0.1-py2.7.egg-info
running install_scripts
adding license file "LICENSE" (matched pattern "LICEN[CS]E*")
creating build\bdist.win-amd64\wheel\benyo-zbx-0.0.1.dist-info\WHEEL
creating 'dist\benyo-zbx-0.0.1-py2.py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'benyo-zbx-0.0.1.dist-info/LICENSE'
adding 'benyo-zbx-0.0.1.dist-info/METADATA'
adding 'benyo-zbx-0.0.1.dist-info/WHEEL'
adding 'benyo-zbx-0.0.1.dist-info/top_level.txt'
adding 'benyo-zbx-0.0.1.dist-info/RECORD'
removing build\bdist.win-amd64\wheel

  


配置 pypi

(1)为了发布包,必须在 pypi.org 注册一个用户,注册邮箱需要验证。

 PyPi 注册

Username: benyo
Fullname: benyo yuan
密码: **********
邮箱: yuyuhupo@outlook.com

  

(2)配置 $HOME”.pypirc” 文件

[distutils]
index-servers=pypi

[pypi]
repository = https://upload.pypi.org/legacy/
username = benyo
password = **********

  

# 若不配置".pypirc"文件,也可直接上传,上传过程中输入用户和密码验证,即可。

 

上传到pypi

使用setuptools上传

# 上传source 包
python setup.py sdist upload
     
# 上传pre-compiled包
python setup.py bdist_wheel upload

使用twine上传

# 使用twine上传,先安装twine
sudo pip install twine
# 上传 twine upload dist/*

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM