大家好,本文是解决如何将自己的python代码建成一个python库,可以让任何人都能pip install <库名> 使用,亲测有效!
前期准备:
1.创建PyPI用户
PyPI官网:https://pypi.org/
2.创建阿里云用户
阿里云官网:https://code.aliyun.com/
3.下载git
当然,python3,anaconda3这些环境也是必备的。
第一步:
登录阿里云,创建一个空项目jlwang825(项目名自己取),可见等级为Public 。
然后,将自己建的项目拉到本地,如下图:
git clone https://code.aliyun.com/wangjieling1124/jlwang825.git
上面git clone 后面的网址就是你自己建的项目的地址,复制到后面即可。
第二步:
在电脑上找到自己拉下来的项目文件jlwang825,在文件jlwang825下再新建一个文件夹jlwang(文件名自己取),jlwang里面新建两个.py文件
add_num.py
# -*- coding:utf-8 -*-
def add_num(a,b):
return a+b
__init__.py
# -*- coding:utf-8 -*-
from . import add_num
第三步:
在项目文件jlwang825里建两个和jlwang同级的文件,如下图
setup.py
from distutils.core import setup
import setuptools
packages = ['jlwang']# 唯一的包名,自己取名
setup(name='jlwang',
version='1.0',
author='wjl',
packages=packages,
package_dir={'requests': 'requests'},)
license.txt(此文件里的内容来源于https://blog.csdn.net/weixin_44201449/article/details/115753474)
Copyright © 2021 xiaoxiong 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.
第四步:
在项目文件jlwang825路径下进入cmd,执行下列三次操作。
python setup.py build
python setup.py sdist
python setup.py sdist bdist_wheel
然后,生成下面这些文件
第五步:
在项目文件jlwang825路径下进入cmd,执行下列操作,将包上传到PyPI。
twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
注意!输入的username和password是你自己创建PyPI账号时设置的。如上图,输入密码时,屏幕上不会显示,所以直接输入自己的密码后enter下一步即可。
然后,登录自己的PyPI账号,会发现库已经上传,可以 pip install 使用了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)