模块——模块打包与发布

文章目录


打包步骤:

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

说明制作成功

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值