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
相关文章
|
3月前
|
开发者 Python
如何在Python中管理模块和包的依赖关系?
在实际开发中,通常会结合多种方法来管理模块和包的依赖关系,以确保项目的顺利进行和可维护性。同时,要及时更新和解决依赖冲突等问题,以保证代码的稳定性和可靠性
163 62
|
12天前
|
JSON Shell 数据格式
使用 pipx 安装并执行 Python 应用程序 (1)
使用 pipx 安装并执行 Python 应用程序 (1)
58 17
|
1月前
|
IDE 测试技术 项目管理
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
PyCharm是由JetBrains开发的Python集成开发环境(IDE),专为Python开发者设计,支持Web开发、调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试和版本控制等功能。它有专业版、教育版和社区版三个版本,其中社区版免费且适合个人和小型团队使用,包含基本的Python开发功能。安装PyCharm前需先安装Python解释器,并配置环境变量。通过简单的步骤即可在PyCharm中创建并运行Python项目,如输出“Hello World”。
284 13
【新手必看】PyCharm2025 免费下载安装配置教程+Python环境搭建、图文并茂全副武装学起来才嗖嗖的快,绝对最详细!
|
2月前
|
Python
Python Internet 模块
Python Internet 模块。
137 74
|
1月前
|
人工智能 Java Python
python安装、vscode安装、conda安装:一文搞定Python的开发环境(史上最全)
尼恩架构团队推出了一系列《LLM大模型学习圣经》PDF,旨在帮助读者深入理解并掌握大型语言模型(LLM)及其相关技术。该系列包括Python基础、Transformer架构、LangChain框架、RAG架构及LLM智能体等内容,覆盖从理论到实践的各个方面。此外,尼恩还提供了配套视频教程,计划于2025年5月前发布,助力更多人成为大模型应用架构师,冲击年薪百万目标。
|
3月前
|
算法 数据安全/隐私保护 开发者
马特赛特旋转算法:Python的随机模块背后的力量
马特赛特旋转算法是Python `random`模块的核心,由松本真和西村拓士于1997年提出。它基于线性反馈移位寄存器,具有超长周期和高维均匀性,适用于模拟、密码学等领域。Python中通过设置种子值初始化状态数组,经状态更新和输出提取生成随机数,代码简单高效。
144 63
|
3月前
|
数据可视化 Python
如何在Python中解决模块和包的依赖冲突?
解决模块和包的依赖冲突需要综合运用多种方法,并且需要团队成员的共同努力和协作。通过合理的管理和解决冲突,可以提高项目的稳定性和可扩展性
|
3月前
|
测试技术 Python
手动解决Python模块和包依赖冲突的具体步骤是什么?
需要注意的是,手动解决依赖冲突可能需要一定的时间和经验,并且需要谨慎操作,避免引入新的问题。在实际操作中,还可以结合使用其他方法,如虚拟环境等,来更好地管理和解决依赖冲突😉。
|
1月前
|
Shell Linux iOS开发
使用 pipx 安装并执行 Python 应用程序 (1)
使用 pipx 安装并执行 Python 应用程序 (1)
70 0
使用 pipx 安装并执行 Python 应用程序 (1)
|
3月前
|
持续交付 Python
如何在Python中自动解决模块和包的依赖冲突?
完全自动解决所有依赖冲突可能并不总是可行,特别是在复杂的项目中。有时候仍然需要人工干预和判断。自动解决的方法主要是提供辅助和便捷,但不能完全替代人工的分析和决策😉。

热门文章

最新文章