python 写一个工具, 上传到 pypi

起因

  1. 把自己的图片工具, 整理为一个库, package, 然后发布出去!
  2. 就像是一个人喜欢搜集贝壳,现在整理一下,用一根线把好看的贝壳串起来,做成一个项链。
  3. 整理记录是很有必要的。 比如写一个 python package. 我之前明明做过(2022年)。

项目代码:

假设项目代码已经写好了,下面是打包发布的过程。

1. 修改下面 setup.py 中的内容,把各种名称改为自己的名字。

# Always prefer setuptools over distutils
from setuptools import setup, find_packages

# To use a consistent encoding
from codecs import open
from os import path

# The directory containing this file
HERE = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(HERE, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

# This call to setup() does all the work
setup(
    name="ImgToolsGG",  # todo fix this name
    version="0.1.23",
    description="test how to make a package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/buxuele/ImgToolsGG",
    author="MeJustAFanOfGSW",
    author_email="[email protected]", # todo fix this
    license="MIT",
    classifiers=[
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Operating System :: OS Independent"
    ],
    packages=["src"],  # todo: main folder
    include_package_data=True,
    install_requires=["numpy"]
)

2. 安装2个工具

pip install wheel twine

3. 构建 build

python setup.py sdist bdist_wheel

4. 检查一下

twine check dist/*

5. 创建 pypi 账号

!!! 这里是最麻烦的地方是: pypi 的双重身份验证
手机下载一个 FreeOTP, 然后扫描二维码就行了

打开: https://pypi.org/manage/account/token/
申请一个 api token. 全账户通用的。
这个token 会比较长!

token: 类似这样,pypi-AgEIcHl*****5xVLj0er0q50fiFoIJQ

6. 测试一下, 能否上传到 test.pypi.org

twine upload --repository-url https://test.pypi.org/legacy/ dist/*
这里可以不用输入token. 毕竟只是测试。 其实这一步可以省略。

7. 上传真正的代码:

twine upload dist/*

这里有个问题,pycharm 终端, 无法复制粘贴文本!

所以需要打开一个系统终端。
右键直接粘贴上面的 token.

8. 其他的, travis-ci 以后再说

你可能感兴趣的:(python,开发语言)