Python -- 模块库-自定义工具module
需求:常用的几个类,来回复制很麻烦,代码也不够整洁
方案:自己制作一个utils的module,可以通过pip 本地安装
步骤:
- 创建工程文件
projectname/
setup.py
README.txt
Doc/
documentation.txt
projectname/
__init__.py
foo.py
bar.py
utils/
__init__.py
spam.py
grok.py
examples/
helloworld.py
...
- setup.py
注意package的内容;
# setup.py from distutils.core import setup setup(name='projectname', version='1.0', author='Your Name', author_email='you@youraddress.com', url='http://www.you.com/projectname', packages=['projectname', 'projectname.utils'], )
- MANIFEST.in
包中需要包含进来的非源码文件
# MANIFEST.in include *.txt recursive-include examples * recursive-include Doc *
__init__.py
可以为空,也可以from . import file1
安装+卸载
安装:
在setup.py的目录下:
pip install .
卸载:pip uninstall projectname
参考:https://python3-cookbook.readthedocs.io/zh_CN/latest/c10/p15_distributing_packages.html