时间:2021-01-05 python教程 查看: 844
本文实例讲述了Python模块的制作方法。分享给大家供大家参考,具体如下:
1 目的
利用setup.py将框架安装到python环境中,作为第三方模块来调用,
2 第一步:完成setup.py的编写
以下代码相当于一个模板,只用更改name字段出,改为对应的需要安装的模块名称就可以,比如这里是:py_plus
将setup.py文件放到py_plus的同级目录下
from os.path import dirname, join
# from pip.req import parse_requirements
from setuptools import (
find_packages,
setup,
)
def parse_requirements(filename):
""" load requirements from a pip requirements file """
lineiter = (line.strip() for line in open(filename))
return [line for line in lineiter if line and not line.startswith("#")]
with open(join(dirname(__file__), './VERSION.txt'), 'rb') as f:
version = f.read().decode('ascii').strip()
setup(
name='py-plus', # 模块名称
version=version,
description='A mini web framework', # 描述
packages=find_packages(exclude=[]),
author='xx',
author_email='your@email.com',
license='Apache License v2',
package_data={'': ['*.*']},
url='#',
install_requires=parse_requirements("requirements.txt"), # 所需的运行环境
zip_safe=False,
classifiers=[
'Programming Language :: Python',
'Operating System :: Microsoft :: Windows',
'Operating System :: Unix',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
注意: 上面代码中可能会报错需要额外安装packaging模块,更新setuptools
pip.req可能不存在,对应的可以:
def parse_requirements(filename):
""" load requirements from a pip requirements file """
lineiter = (line.strip() for line in open(filename))
return [line for line in lineiter if line and not line.startswith("#")]
3 第二步:完成requirements.txt的编写
功能:
使用:
requests>=2.18.4
six>=1.11.0
4 第三步:完成VERSION.txt的编写
功能:
使用:
1.0
5 第四步:执行安装命令
步骤:
python setup.py install
希望本文所述对大家Python程序设计有所帮助。