打包步骤:
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
说明制作成功



