便宜VPS主机精选
提供服务器主机评测信息

python打包工具distutils简介

distutils 是 python 标准库的一部分,这个库的目的是为开发者提供一种方便的打包方式, 同时为使用者提供方便的安装方式。当我们开发了自己的模块之后,使用distutils的setup.py打包。

hello.py
import time
print("Current time:",time.asctime())
def hello_fun():
    print("This is my function hello")
setup.py
from distutils.core import setup
setup(
     name="hello_module",
     version="1.0",
     author="",
     author_email="",
     py_modules=['hello'],
)

执行打包命令 python setup.py sdist

C:\Users\23798\Desktop\desktop\my_note\python_project\mysetuptool>python setup.py sdist
running sdist
running check
warning: check: missing required meta-data: url
warning: check: missing meta-data: either (author and author_email) or (maintainer and maintainer_email) must be supplied
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
writing manifest file 'MANIFEST'
creating hello_module-1.0
copying files to hello_module-1.0...
copying README -> hello_module-1.0
copying hello.py -> hello_module-1.0
copying setup.py -> hello_module-1.0
creating dist
creating 'dist\hello_module-1.0.zip' and adding 'hello_module-1.0' to it
adding 'hello_module-1.0'
adding 'hello_module-1.0\hello.py'
adding 'hello_module-1.0\PKG-INFO'
adding 'hello_module-1.0\README'
adding 'hello_module-1.0\setup.py'
removing 'hello_module-1.0' (and everything under it)

hello_module-1.0.zip就是生成的python模块, 解压后使用python setup.py install安装该模块。从路径可以看出,该模块安装到标准库的制定路径下。

C:\Users\23798\Desktop\desktop\my_note\python_project\mysetuptool\dist\hello_module-1.0>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
copying hello.py -> build\lib
running install_lib
copying build\lib\hello.py -> C:\Python27\Lib\site-packages
byte-compiling C:\Python27\Lib\site-packages\hello.py to hello.pyc
running install_egg_info
Writing C:\Python27\Lib\site-packages\hello_module-1.0-py2.7.egg-info

现在,我们就可以打开python导入这个包了

>>> import hello
('Current time:', 'Tue Nov 22 21:28:18 2022')
未经允许不得转载:便宜VPS测评 » python打包工具distutils简介