Amaze UI Logo

码动指尖



python开源之上传代码到PyPI仓库

一直以来就想要把自己的东西进行开源,但是由于各种原因一直耽搁,一直就没有试过上传,仅仅是放在github进行开源。

自己使用也比较麻烦,经常需要进行 git clone xxxxx.git.

在18年五月的时候,由于实习,各种翻以前造的的工具,自己都要翻晕了,于是毅然进行上传到仓库。

废话不多说,开始肝:


1. 上传工具

上传肯定是要用到工具的。

执行以下命令


python -m pip install --user --upgrade setuptools wheel

python -m pip install --user --upgrade twine

PS:

setuotools 和 wheel 用来构建你的项目,一般都会随 Python 安装,但是还是检查一下

twine 用来上传你的包到 PyPi 


2.目录结构

然后看目录结构,我的项目名叫 ueditor-flask

.
├── LICENSE
├── Pipfile
├── Pipfile.lock
├── README.md
├── __init__.py
├── flask_ueditor
├── main.py
├── requirements.txt
├── setup.py
└── test


可以看到根目录下有许多文件,其中与上传相关的文件是 setup.py 和 flask_ueditor文件夹下面的 __about__.py 文件。接下来看里面的内容:


2.1 setup.py


"""
Flask-Ueditor
-------------

Flask-Ueditor is used to accept ueditor's request from the front end.

Links
`````

* `development version <https://github.com/guaidashu/ueditor-flask>`_
"""
import os
import sys

from setuptools import setup, find_packages

about = {}

with open('flask_ueditor/__about__.py') as f:
exec(f.read(), about)

if sys.argv[-1] == 'test':
status = os.system('make check')
status >>= 8
sys.exit(status)

setup(
name=about['__title__'],
version=about['__version__'],
author=about['__author__'],
author_email=about['__author_email__'],
description=about['__description__'],
long_description=__doc__,
long_description_content_type="text/markdown",
license=about['__license__'],
url=about['__url__'],
packages=find_packages(),
install_requires=about['__install_requires__'],
classifiers=[
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
]
)


2.2 flask_ueditor/__about__.py


__title__ = 'Flask-Ueditor'
__description__ = 'Flask ueditor Backstage, designed by yy'
__url__ = 'https://github.com/guaidashu/ueditor-flask'
__version_info__ = ('0', '1', '0')
__version__ = '.'.join(__version_info__)
__author__ = 'guaidashu'
__author_email__ = 'song42960@gmail.com'
__maintainer__ = 'YY blog'
__license__ = 'MIT'
__copyright__ = '(c) 2019 by guaidashu'
__install_requires__ = [
"qiniu >= 7.2.6",
"requests >= 2.21.0",
"Flask >= 1.0.2"
]

PS:

name: 这个字段很关键,pip install的时候就是根据这个名字来进行安装

version: 每次上传必须要修改这个东西,对应你的发布版本

description: 项目描述

author: 作者

url: 是项目的地址。我这里填的是 github 地址。

install_requires: 此项目的依赖

classifiers: 分类。根据 PyPi Classifiers 填写,至少要包含所用的 Python 版本。

其他的杂七杂八的都无所谓了


3. License获取和填写

上传到 PyPi 的每个包都要包含许可证。

选择许可证:Choose License

选择许可证后,打开 LICENSE 并输入许可证文本。选择了 MIT 许可证:


MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


4.注册账号

点此注册:Register Pypi


5.构建项目并上传

最后,依次执行以下命令进行构建检查和上传


5.1 构建

# 检查
python setup.py check

# 构建
python setup.py sdist bdist_wheel


5.2 上传


twine upload --repository-url https://upload.pypi.org/legacy/ dist/*


执行此命令之后会要求你输入PyPI的用户名和密码,输入你注册好的账户信息即可上传。中途可能会报错邮箱未验证,请先验证邮箱噢。



 Python

作者  :  奕弈

喵喵喵,你在心上



评论


About ME

about me

奕弈

为了最初的心,努力奋斗,从不懈怠的学习。

我不想成为一个庸俗的人。十年百年后,当我们死去,质疑我们的人同样死去,后人看到的是裹足不前、原地打转的你,还是一直奔跑、走到远方的我?

Contact ME