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
复制代码

 

posted @   百衲本  阅读(508)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码
点击右上角即可分享
微信分享提示