Python模块制作及安装
一、module目录结构体如下:
.
├── setup.py
├── suba
│ ├── aa.py
│ ├── bb.py
│ └── __init__.py
└── subb
├── cc.py
├── dd.py
└── __init__.py
二、编辑setup.py文件
py_modules需指明所需包含的py文件
[root@operationinception test-module]# cat setup.py
from distutils.core import setup
setup(name="testModule", version="1.0", description="test module", author="test", py_modules=['suba.aa', 'suba.bb', 'subb.cc', 'subb.dd'])
三、构建模块
python setup.py build
构建后目录结构
[root@operationinception test-module]# python3 setup.py build
running build
running build_py
creating build
creating build/lib
creating build/lib/suba
copying suba/__init__.py -> build/lib/suba
copying suba/aa.py -> build/lib/suba
copying suba/bb.py -> build/lib/suba
creating build/lib/subb
copying subb/__init__.py -> build/lib/subb
copying subb/cc.py -> build/lib/subb
copying subb/dd.py -> build/lib/subb
[root@operationinception test-module]# tree
.
├── build
│ └── lib
│ ├── suba
│ │ ├── aa.py
│ │ ├── bb.py
│ │ └── __init__.py
│ └── subb
│ ├── cc.py
│ ├── dd.py
│ └── __init__.py
├── setup.py
├── suba
│ ├── aa.py
│ ├── bb.py
│ └── __init__.py
└── subb
├── cc.py
├── dd.py
└── __init__.py
6 directories, 13 files
四、生成发布压缩包
python setup.py sdist
打包后,生成最终发布压缩包testModule-1.0.tar.gz , 目录结构
[root@operationinception test-module]# python3 setup.py sdist
running sdist
running check
warning: check: missing required meta-data: url
warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating testModule-1.0
creating testModule-1.0/suba
creating testModule-1.0/subb
making hard links in testModule-1.0...
hard linking setup.py -> testModule-1.0
hard linking suba/__init__.py -> testModule-1.0/suba
hard linking suba/aa.py -> testModule-1.0/suba
hard linking suba/bb.py -> testModule-1.0/suba
hard linking subb/__init__.py -> testModule-1.0/subb
hard linking subb/cc.py -> testModule-1.0/subb
hard linking subb/dd.py -> testModule-1.0/subb
creating dist
Creating tar archive
removing 'testModule-1.0' (and everything under it)
[root@operationinception test-module]# tree
.
├── build
│ └── lib
│ ├── suba
│ │ ├── aa.py
│ │ ├── bb.py
│ │ └── __init__.py
│ └── subb
│ ├── cc.py
│ ├── dd.py
│ └── __init__.py
├── dist
│ └── testModule-1.0.tar.gz
├── MANIFEST
├── setup.py
├── suba
│ ├── aa.py
│ ├── bb.py
│ └── __init__.py
└── subb
├── cc.py
├── dd.py
└── __init__.py
7 directories, 15 files
五、安装模块(可将tar包拷贝到任意机器上)
[root@operationinception test-module]# cp dist/testModule-1.0.tar.gz /root/
[root@operationinception test-module]# cd /root/
[root@operationinception ~]# tar xf testModule-1.0.tar.gz
[root@operationinception ~]# cd testModule-1.0/
[root@operationinception testModule-1.0]# python3 setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/suba
copying suba/__init__.py -> build/lib/suba
copying suba/aa.py -> build/lib/suba
copying suba/bb.py -> build/lib/suba
creating build/lib/subb
copying subb/__init__.py -> build/lib/subb
copying subb/cc.py -> build/lib/subb
copying subb/dd.py -> build/lib/subb
running install_lib
creating /usr/local/python3/lib/python3.6/site-packages/suba
copying build/lib/suba/__init__.py -> /usr/local/python3/lib/python3.6/site-packages/suba
copying build/lib/suba/aa.py -> /usr/local/python3/lib/python3.6/site-packages/suba
copying build/lib/suba/bb.py -> /usr/local/python3/lib/python3.6/site-packages/suba
creating /usr/local/python3/lib/python3.6/site-packages/subb
copying build/lib/subb/__init__.py -> /usr/local/python3/lib/python3.6/site-packages/subb
copying build/lib/subb/cc.py -> /usr/local/python3/lib/python3.6/site-packages/subb
copying build/lib/subb/dd.py -> /usr/local/python3/lib/python3.6/site-packages/subb
byte-compiling /usr/local/python3/lib/python3.6/site-packages/suba/__init__.py to __init__.cpython-36.pyc
byte-compiling /usr/local/python3/lib/python3.6/site-packages/suba/aa.py to aa.cpython-36.pyc
byte-compiling /usr/local/python3/lib/python3.6/site-packages/suba/bb.py to bb.cpython-36.pyc
byte-compiling /usr/local/python3/lib/python3.6/site-packages/subb/__init__.py to __init__.cpython-36.pyc
byte-compiling /usr/local/python3/lib/python3.6/site-packages/subb/cc.py to cc.cpython-36.pyc
byte-compiling /usr/local/python3/lib/python3.6/site-packages/subb/dd.py to dd.cpython-36.pyc
running install_egg_info
Writing /usr/local/python3/lib/python3.6/site-packages/testModule-1.0-py3.6.egg-info
[root@operationinception testModule-1.0]# python3
Python 3.6.5 (default, Apr 13 2020, 17:54:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import suba
>>> import suba.aa
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具