Python模块制作与发布
如果希望自己开发的模块,分享 给其他人,可以按照以下步骤操作
新建包名为hello
,此模块的全部功能由request.py
和response.py
两个文件完成。
目录结构:
.
|-- hello
| |-- __init__.py
| |-- request.py
| `-- response.py
`-- setup.py
request.py
文件
def hello():
print("请求调用我")
response.py
文件
from hello.request import hello
def res():
hello()
print("已经响应了")
def main(): # 测试
res()
if __name__ == '__main__':
main()
__init__.py
文件
from . import request
from . import response
注:这里使用的是
Python3.x
的解释器。
1. 制作发布压缩包步骤
1) 创建 setup.py
setup.py
的文件
from distutils.core import setup
# 多值的字典参数,setup是一个函数
setup(name="Hello", # 包名
version="1.0", # 版本
description="a simple example", # 描述信息
long_description="简单的模块发布例子", # 完整描述信息
author="onefine", # 作者
author_email="188302531@qq.com", # 作者邮箱
url="www.onefine.top", # 主页
py_modules=["hello.request",
"hello.response"]) # 记录包中包中包含的所有模块
有关字典参数的详细信息,可以参阅官方网站:https://docs.python.org/2/distutils/apiref.html
2) 构建模块
构建模块命令如下
$ python3 setup.py build
如果出现下面情况
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ python3 setup.py build
Traceback (most recent call last):
File "setup.py", line 1, in <module>
from distutils.core import setup
ModuleNotFoundError: No module named 'distutils.core'
因为ubuntu-18.10 默认没有安装 pip ,需要安装 python3-pip
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ sudo apt-get install python3-pip
[sudo] onefine 的密码:
输入密码,耐心等待安装之后重新构建模块:
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ python3 setup.py build
running build
running build_py
creating build
creating build/lib
creating build/lib/hello
copying hello/__init__.py -> build/lib/hello
copying hello/request.py -> build/lib/hello
copying hello/response.py -> build/lib/hello
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$
同样,若出现下面情况,请安装ipython3
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ ipython3
Command 'ipython3' not found, but can be installed with:
sudo apt install ipython3
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ sudo apt install ipython3
3) 生成发布压缩包
生成发布压缩包命令如下
$ python3 setup.py sdist
执行如下
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ python3 setup.py sdist
running sdist
running check
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 Hello-1.0
creating Hello-1.0/hello
making hard links in Hello-1.0...
hard linking setup.py -> Hello-1.0
hard linking hello/__init__.py -> Hello-1.0/hello
hard linking hello/request.py -> Hello-1.0/hello
hard linking hello/response.py -> Hello-1.0/hello
creating dist
Creating tar archive
removing 'Hello-1.0' (and everything under it)
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$
注意:要制作哪个版本的模块,就使用哪个版本的解释器执行!这里使用的是Python3.x。
查看一下
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ ls dist -a -l
总用量 12
drwxr-xr-x 2 onefine onefine 4096 1月 14 20:02 .
drwxrwxr-x 7 onefine onefine 4096 1月 14 20:02 ..
-rw-r--r-- 1 onefine onefine 936 1月 14 20:02 Hello-1.0.tar.gz
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$
Hello-1.0.tar.gz
就是发布的模块,自己开发的模块制作完成。
3. 安装模块
解压
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ ls dist/ -l
总用量 8
drwxr-xr-x 3 onefine onefine 4096 1月 14 20:02 Hello-1.0
-rw-r--r-- 1 onefine onefine 936 1月 14 20:02 Hello-1.0.tar.gz
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils$ cd dist/
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$ tar -zxzf Hello-1.0.tar.gz
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$ ls -l
总用量 8
drwxr-xr-x 3 onefine onefine 4096 1月 14 20:02 Hello-1.0
-rw-r--r-- 1 onefine onefine 936 1月 14 20:02 Hello-1.0.tar.gz
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$ tree Hello-1.0
Hello-1.0
|-- PKG-INFO
|-- hello
| |-- __init__.py
| |-- request.py
| `-- response.py
`-- setup.py
1 directory, 5 files
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$
安装
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$ sudo python3 /Hello-1.0/setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/hello
copying hello/__init__.py -> build/lib/hello
copying hello/request.py -> build/lib/hello
copying hello/response.py -> build/lib/hello
running install_lib
creating /usr/local/lib/python3.6/dist-packages/hello
copying build/lib/hello/request.py -> /usr/local/lib/python3.6/dist-packages/hello
copying build/lib/hello/response.py -> /usr/local/lib/python3.6/dist-packages/hello
copying build/lib/hello/__init__.py -> /usr/local/lib/python3.6/dist-packages/hello
byte-compiling /usr/local/lib/python3.6/dist-packages/hello/request.py to request.cpython-36.pyc
byte-compiling /usr/local/lib/python3.6/dist-packages/hello/response.py to response.cpython-36.pyc
byte-compiling /usr/local/lib/python3.6/dist-packages/hello/__init__.py to __init__.cpython-36.pyc
running install_egg_info
Writing /usr/local/lib/python3.6/dist-packages/Hello-1.0.egg-info
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$ tree Hello-1.0
Hello-1.0
|-- PKG-INFO
|-- build [error opening dir]
|-- hello
| |-- __init__.py
| |-- request.py
| `-- response.py
`-- setup.py
2 directories, 5 files
onefine@onefine-virtual-machine:~/PycharmProjects/Distutils/dist$
测试
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ ipython3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import hello
In [2]: hello.response.res()
请求调用我
已经响应了
In [3]: exit
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$
卸载模块
直接从安装目录下,把安装模块的 目录 删除就可以
onefine@onefine-virtual-machine:/$ cd /usr/local/lib/python3.6/dist-packages/
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ ls -a -l
总用量 16
drwxrwsr-x 3 root staff 4096 1月 14 20:53 .
drwxrwsr-x 3 root staff 4096 10月 18 06:23 ..
drwxr-sr-x 3 root staff 4096 1月 14 20:53 hello
-rw-r--r-- 1 root staff 223 1月 14 20:53 Hello-1.0.egg-info
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ sudo rm -r hello*
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ ls -a -l
总用量 12
drwxrwsr-x 2 root staff 4096 1月 14 21:12 .
drwxrwsr-x 3 root staff 4096 10月 18 06:23 ..
-rw-r--r-- 1 root staff 223 1月 14 20:53 Hello-1.0.egg-info
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ cat Hello-1.0.egg-info
Metadata-Version: 1.0
Name: Hello
Version: 1.0
Summary: a simple example
Home-page: www.onefine.top
Author: onefine
Author-email: 188302531@qq.com
License: UNKNOWN
Description: 简单的模块发布例子
Platform: UNKNOWN
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ sudo rm Hello-1.0.egg-info
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ ls -a -l
总用量 8
drwxrwsr-x 2 root staff 4096 1月 14 21:12 .
drwxrwsr-x 3 root staff 4096 10月 18 06:23 ..
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$
删除成功,要不再来测试一下
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$ ipython3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import hello
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-f81fb083bdeb> in <module>()
----> 1 import hello
ModuleNotFoundError: No module named 'hello'
In [2]: exit
onefine@onefine-virtual-machine:/usr/local/lib/python3.6/dist-packages$
至此,模块安装从入门到卸载完成。
4. 使用 pip
安装第三方模块
第三方模块 通常是指由 知名的第三方团队 开发的 并且被 程序员广泛使用 的 Python
包 / 模块,例如 pygame
就是一套非常成熟的 游戏开发模块
pip
是一个现代的,通用的Python
包管理工具- 提供了对
Python
包的查找、下载、安装、卸载等功能
安装和卸载命令如下:
# 将模块安装到 Python 2.x 环境
$ sudo pip install pygame
$ sudo pip uninstall pygame
# 将模块安装到 Python 3.x 环境
$ sudo pip3 install pygame
$ sudo pip3 uninstall pygame
在 Mac
下安装 iPython
$ sudo pip install ipython
在 Linux
下安装 iPython
$ sudo apt install ipython
$ sudo apt install ipython3