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
相关文章
|
4天前
|
Java Serverless Python
探索Python中的并发编程与`concurrent.futures`模块
探索Python中的并发编程与`concurrent.futures`模块
11 4
|
16天前
|
API Python
python ratelimit模块
python ratelimit模块
|
16天前
|
Python
像导入Python模块一样导入ipynb文件
像导入Python模块一样导入ipynb文件
|
16天前
|
算法 Python
python tarfile模块
python tarfile模块
|
16天前
|
Python
如何在 Python 中导入模块
【8月更文挑战第29天】
19 1
|
16天前
|
Python
|
16天前
|
数据采集 JSON 算法框架/工具
我常用的几个经典Python模块
我常用的几个经典Python模块
|
16天前
|
开发者 Python
什么是 python 模块?
【8月更文挑战第29天】
9 0
|
16天前
|
API Python
python中copy模块的使用,深拷贝和浅拷贝
python中copy模块的使用,深拷贝和浅拷贝
12 0