Python Package 包与 Module 模块

Package是通过文件夹定义的,目的是将所有是需要用到的模块打包到一起。

包的识别是通过__init__.py来区分的,如果包内有__main__.py,则可以通过python -m来执行。

$ tree
.
├── blfems
│   ├── __init__.py
│   ├── __main__.py
│   ├── cloud.py
│   ├── db
│   │   ├── __init__.py
│   │   └── qmysql.py
│   └── iothub
│       ├── __init__.py
│       └── qmqtt.py
└── runas_service.py

3 directories, 8 files
$ 
$ python3 -m ems
- run ems as iothub cloud service since 2018-04-17 14:54:01
$ cat ems/__init__.py

$ cat ems/__main__.py
#!/usr/bin/env python3
# by Qige <[email protected]> since 20180417

from ems.cloud import run_iot_service

run_iot_service()

$

打包 Python Package

Package的同级目录下,创建setup.py文件,内容如下:

#!/usr/bin/env python3
# by Qige <[email protected]> since 20180418
# v1.0.20180418

from setuptools import setup, find_packages

version = '1.0.20180418a'

extras_require = {}

setup(
    name = 'ems',
    version = version,
    url = 'https://gitlab.corp.com/qige.zhao/ems.git',
    description = 'A Corp Environmental Monitoring System (MQTT V3.1.1)',
    #long_description = open('README.txt').read(),
    author = 'Qige',
    maintainer = 'Qige Zhao',
    maintainer_email = '[email protected]',
    license = 'MIT',
    packages = find_packages(),
    include_package_data = True,
    zip_safe = False,
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        #'Development Status :: 3 - Alpha',
        #'Development Status :: 4 - Beta',
        #'Development Status :: 5 - Production/Stable',
        #'Development Status :: 6 - Mature',
        #'Development Status :: 7 - Inactive',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development :: Libraries :: Application Frameworks',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
    #python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
    python_requires='!=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
    install_requires = [
        'pymysql>=0.8.0', # '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
        'paho-mqtt>=1.3.1' # '>= 3.4, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
    ],
    extras_require = extras_require
)

之后执行python3 setup.py install即可安装该模块至当前系统。

可以随时执行python3 -m ems来执行了

包卸载

$ python3 setup.py install --config files.txt
$ cat files.txt | xargs rm -rf

results matching ""

    No results matching ""