作者:虚坏叔叔
早餐店不会开到晚上,想吃的人早就来了!😄
python如何将代码制作成可以pip的库,将自己的python代码打包成库,让别人pip安装调用?
大家好,本文是解决如何将自己的python代码建成一个python库,可以让任何人都能pip install <库名> 使用,亲测有效!
前期准备:
1.创建PyPI用户:PyPI官网:https://pypi.org/
2.安装Python:python3,anaconda3这些环境也是必备的。
一、创建项目
在电脑上找到自己拉下来的项目文件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
二、创建pip库所需要的文件
在项目文件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
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
然后,生成下面这些文件
四、上传到PyPI
在项目文件jlwang825
路径下进入cmd,执行下列操作,将包上传到PyPI。
twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
注意!输入的username
和password
是你自己创建PyPI
账号时设置的。如上图,输入密码时,屏幕上不会显示,所以直接输入自己的密码后enter
下一步即可。
然后,登录自己的PyPI
账号,会发现库已经上传,可以 pip install 使用了。
五、测试pip下载并使用jlwang包
六、总结
- 本文介绍了python如何将代码制作成可以pip的库,将自己的python代码打包成库,让别人pip安装调用? 。