python3 模块构建与发布

时间:2021-11-28 18:35:52

Based on Ubuntu:

准备发布自己的模块

自己的模块内容

root@ubuntu:~/python/nester# cat nester.py 
def print_lol(the_list):
#This is the example from the book
    for var in the_list:
        if isinstance(var,list):
            print_lol(var)
        else:
            print(var)

在同级目录下编辑一个setup.py:

root@ubuntu:~/python/nester# cat setup.py 
from distutils.core import setup

setup(
    name        ='nester',
    version     ='1.0.0',
    py_modules  =['nester'],
    author      ='testtester',
    author_email    ='test@test.com',
    url     ='http://www.headfirstlabs.com',
    description ='A simple printer of nested lists',
)

浏览在nester目录下已经创建的内容:

root@ubuntu:~/python/nester# ls
nester.py  setup.py

构建一个发布文件

在目录nester下:

root@ubuntu:~/python/nester# 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 nester-1.0.0
making hard links in nester-1.0.0...
hard linking nester.py -> nester-1.0.0
hard linking setup.py -> nester-1.0.0
creating dist
Creating tar archive
removing 'nester-1.0.0' (and everything under it)

将发布安装到python本地副本中:

root@ubuntu:~/python/nester# python3 setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying nester.py -> build/lib
running install_lib
copying build/lib/nester.py -> /usr/local/lib/python3.5/dist-packages
byte-compiling /usr/local/lib/python3.5/dist-packages/nester.py to nester.cpython-35.pyc
running install_egg_info
Removing /usr/local/lib/python3.5/dist-packages/nester-1.0.0.egg-info
Writing /usr/local/lib/python3.5/dist-packages/nester-1.0.0.egg-info

发布预览

root@ubuntu:~/python/nester# tree -af
.
├── ./build
│   └── ./build/lib
│       └── ./build/lib/nester.py
├── ./dist
│   ├── ./dist/nester-1.0.0
│   │   ├── ./dist/nester-1.0.0/nester.py
│   │   ├── ./dist/nester-1.0.0/PKG-INFO
│   │   └── ./dist/nester-1.0.0/setup.py
│   └── ./dist/nester-1.0.0.tar
├── ./MANIFEST
├── ./nester.py
└── ./setup.py

Note:run “apt install tree” first.

导入模块并使用

root@ubuntu:~/python# python3
>>> import nester
>>> m=["A",["BVD","C"],"DDD"]
>>> nester.print_lol(m)
A
BVD
C
DDD

For windows:
For windows下:
C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32\nester> C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32\ python.exe setup.py sdist
C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36-32\ python.exe setup.py install