如何发布自己的 python 库

  1. 首先去 pypi 网站注册自己的账号
  2. 在 github 上新建一个代码仓库 https://github.com/YifeiLiu-1997/guto
  3. 将代码拉取到本地,将文件夹内建成如下的样子
    1
    2
    3
    4
    5
    6
    7
    |-- LICENSE
    |-- README.md
    |-- setup.py
    |-- guto // 库目录,实际上 import guto 后在此调用其中的 API
    | |-- frame.py
    | |-- position.py
    | |-- __init__.py
  4. 将 setup.py 内写成如下结构
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import setuptools

    setuptools.setup(
    name='guto', # 指定包名
    version='0.0.1.21',
    author='YifeiLiu-1997',
    author_email='lyfwork_mail@163.com',
    description='easy gui-auto tools',
    url='https://github.com/YifeiLiu-1997/guto', # 指定 github 库的 url
    packages=setuptools.find_packages(),
    classifiers=[
    'Programming Language :: Python :: 3',
    'License :: OSI Approved :: MIT License',
    'Operating System :: OS Independent'
    ],
    install_requires=['pyautogui>=0.9.53'] # 指定依赖的包以及版本要求
    )
  5. LICENSE 具有很多中写法,具体可以去百度
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Copyright © 2022 YifeiLiu Authors. All Rights Reserve.

    Licensed under the Apache License, Version 2.0 (the “License”);
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an “AS IS” BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  6. 安装 setuptools 和 twine 库
  7. 在文件夹下打包写好的源代码
    1
    python setup.py sdist bdist_wheel
    打包之后,文件目录成为如下结构
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    |-- guto
    |-- LICENSE
    |-- README.md
    |-- setup.py
    |-- build
    | |-- bdist.win-amd64
    | |-- lib
    | |-- guto
    | |-- frame.py
    | |-- position.py
    | |-- __init__.py
    |-- dist
    | |-- guto-0.0.1.21-py3-none-any.whl
    | |-- guto-0.0.1.21.tar.gz
    |-- guto
    | |-- frame.py
    | |-- position.py
    | |-- __init__.py
    | |-- __pycache__
    | |-- position.cpython-39.pyc
    | |-- __init__.cpython-39.pyc
    |-- guto.egg-info
    | |-- dependency_links.txt
    | |-- PKG-INFO
    | |-- requires.txt
    | |-- SOURCES.txt
    | |-- top_level.txt
  8. 将源代码上传至 pypi,以供直接安装
    1
    twine upload dist/*
    输入 pypi 注册的账号名以及密码,成功上传至远程库,在任意联网电脑输入
    1
    pip install guto==0.0.1.2
    即可安装自己发布的 python 库