2

python~发布自己的py组件

 1 year ago
source link: https://www.cnblogs.com/lori/p/17445471.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

你使用python写的东西,想让其它人直接使用,可以像java,.net一样,发到包管理平台即可,在python中,你可以使用twine这个工具来实现发布,就像.net里的nuget,java里的mvn deploy一样。

本地自己用

  • 添加一个hello.py的文件,里面有个say方法 ,打印字符
import sys

def say(to):
    print('hello', to)

if __name__ == '__main__':
    say(sys.argv[1])
  • 在当前目录添加setup.py文件,添加包安装的代码,包的名字和包含的模块
import setuptools
setuptools.setup(
    name='hellopkg', # 包的名字,可随意取
    py_modules=['hello'] # 对应hello.py,也是安装了包之后实际import的名字
)
  • 运行命令,安装这个包,以后可以在本机的其它项目里使用
pip install .
  • 其它项目里使用它
import hello; 
hello.say('world')

将组件包发布到远程,在其它电脑上用

组件文件结构

118538-20230531095959626-2031134987.png

twine安装步骤

  1. 注册自己的pypi账号:https://pypi.org/account/register/
  2. python -m pip install --user --upgrade setuptools wheel -i https://mirrors.aliyun.com/pypi/simple/
  3. python -m pip install --user --upgrade twine -i https://mirrors.aliyun.com/pypi/simple/
  4. 创建dist文件:python setup.py sdist
    118538-20230531103249841-1517551303.png
  5. 配置你的pipy的token,免得每次上传都要输入用户名密码
  • 在你的用户根目录,添加文件pypi,将下面内容放上去,注册username是你的真实名字,password是从pypi里申请的token
[pypi]
username = bfyxzls
password = pypi-AgEIcHlwaS5vcmcCJGNkMWM5ODI3LWRjYzUtNGRmZi04MTRkLW
  1. 上传你的包到互联网
twine upload dist/*
118538-20230531104748352-1258508210.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK