Python模块制作及安装

简介: 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
相关文章
|
1月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
274 7
|
1月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
215 0
|
2月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
378 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
1月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
304 4
|
1月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
226 0
|
1月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
164 0
|
2月前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
116 4
|
2月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
276 0
|
4月前
|
人工智能 数据挖掘 Linux
Centos安装Python3.7(亲测可用)
本指南详细介绍了在基于Linux(以CentOS系统为例,使用yum包管理器)的系统上安装Python 3.7版本的完整流程。Python是一种广泛使用的高级编程语言,在各种领域如软件开发、数据分析、人工智能和区块链开发等都有着重要的应用。
473 2
|
3月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
316 0

推荐镜像

更多
下一篇
oss云网关配置