NiceLeeのBlog 用爱发电 bilibili~

Python 一次package分发记录

2020-01-19
nIceLee

阅读:


感觉还是Python用的人比较多,至少入门简单,使用也方便。
现在,我要发布一个Python应用,包名you-live,console命令you-live
记录一下整个过程。

参考

官方传送门

关于相对引用

因为某些问题,我把顶层的文件夹改了个名,于是引发了一些列的import错误。
此处应有相对引用,方便改动。具体就不说了,知道有这回事就行。

from ..src.folder1 import file1
from .folder1 import file2

另外,想引用同目录所在的包,应该注意以下要素:

import .folder1 	×
from . import folder1	 √

setup.py

要素过多,但一次填写后续项目基本大同小异。
对我而言,主要关心packagesrequiresentry_points

import setuptools
import os
from live_recorder import version

with open("README.md", "r", encoding = 'utf-8') as fh:
    long_description  =  fh.read()

def find_packages(*tops):
    packages = []
    for d in tops:
        for root, dirs, files in os.walk(d, followlinks=True):
            if '__init__.py' in files:
                packages.append(root)
    return packages

REQ  =  ['PyExecJS', 'requests']

setuptools.setup(
    name = "you-live",
    version = version.__version__,
    description = version.__descriptrion__,
    author = "NiceLee",
    author_email = "[email protected]",
    license = "MIT",
    long_description = long_description,
    long_description_content_type = "text/markdown",
    url = "https://github.com/nICEnnnnnnnLee/LiveRecorder",
    requires = REQ,
    install_requires = REQ,
    zip_safe = True,
    packages = find_packages('live_recorder'),
    classifiers = [
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Intended Audience :: End Users/Desktop",
        "Environment :: Console",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3 :: Only",
        "Programming Language :: Python :: 3.0",
        "Programming Language :: Python :: 3.1",
        "Programming Language :: Python :: 3.2",
        "Programming Language :: Python :: 3.3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Topic :: Internet",
        "Topic :: Internet :: WWW/HTTP",
        "Topic :: Multimedia",
        "Topic :: Multimedia :: Sound/Audio",
        "Topic :: Multimedia :: Video"
    ],
    entry_points={
        "console_scripts": ["you-live=live_recorder.__main__:main"]
    },
)

文件生成

  • 确保前置环境OK
    python3 -m pip install --user --upgrade setuptools wheel
    
  • 生成分发文件
    python3 setup.py sdist bdist_wheel
    

测试分发

  • 正式发布前的可选操作,确保拥有Test PyPI账号
  • 确保前置环境OK
    python3 -m pip install --user --upgrade twine
    
  • dist文件夹下的文件发布到TestPy
    python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
    
  • 从TestPy安装包you-live
    python3 -m pip install --upgrade --index-url https://test.pypi.org/simple/ you-live
    
  • (可选)卸载you-live
    python3 -m pip uninstall you-live
    

正式分发

  • 确保拥有PyPI账号
  • (可选)将账号写入配置
    • 编辑~/.pypirc
      [distutils]
      index-servers = pypi
    
      [pypi]
      username:{username}
      password:{password}
    
  • 确保前置环境OK
    python3 -m pip install --user --upgrade twine
    
  • dist文件夹下的文件发布到pypi
    python3 -m twine upload dist/*
    
  • 从pypi安装包you-live
    python3 -m pip install --upgrade you-live
    

内容
隐藏