栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

模块——模块打包与发布

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模块——模块打包与发布

文章目录
打包步骤:

1、创建模块包 sanle,一定要有__init__.py文件

[root@lier modpack]# mkdir sanle
[root@lier modpack]# ls
sanle
[root@lier modpack]# cd sanle
[root@lier sanle]# ls
[root@lier sanle]# vim __init__.py
[root@lier sanle]# ls
__init__.py
[root@lier sanle]# vim sanchuang.py
[root@lier sanle]# pwd
/python-test/import_test/modpack/sanle
[root@lier sanle]# cat sanchuang.py 
print("this is sanchuang")

def func1():
	print("this is func1")

2、在sanle同级目录下创建打包配置文件setup.py

[root@lier sanle]# cd ..
[root@lier modpack]# ls
sanle
[root@lier modpack]# vim setup.py
from setuptools import setup, find_packages

setup(
	# 包名
	name = "sc",
	# 官网
	url = "http://www.sanchuangedu.cn",
	# 版本号
	version = "0.0.1",
	# 指定要打包的模块和包
	packages = find_packages(),
	# 作者
	author = "lier",
	# 邮箱
	author_email = "2025838198@qq.com",
	# 依赖
	install_requires = ['xlrd>=1.1.0'],
	# 描述信息
	description = "this is test package"
)

find_packages()会自动寻找同级目录下有__init__.py的包

3、运行python3 setup.py check进行语法检查,返回running check表示语法正确

[root@lier modpack]# python3 setup.py check
running check

4、运行python3 setup.py sdist 会生成一个tar.gz压缩包,会在同级目录下生成一个dist目录

[root@lier modpack]# python3 setup.py sdist
[root@lier modpack]# ls
dist  sanle  sc.egg-info  setup.py
[root@lier modpack]# cd dist
[root@lier dist]# ls
sc-0.0.1.tar.gz

5、安装

进入dist目录使用pip3 install sc-0.0.1.tar.gz 安装刚才发布的包

[root@lier dist]# pip3 install sc-0.0.1.tar.gz 

Installing collected packages: xlrd, sc
  Running setup.py install for sc ... done
Successfully installed sc-0.0.1 xlrd-2.0.1

6.测试

可以在任意一个目录下导入包测试

[root@lier dist]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sanle import sanchuang
this is sanchuang
>>> sanchuang.func1()
this is func1

说明制作成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号