• Python源码.py文件打包为.whl文件


    1 python源码.py文件打包

     1.1 安装工具包

    python源文件打包需要用到setuptools和wheel工具包:

     1.2建立python项目源文件

      建立一个名称为hello的项目包和setup.py文件

     

     其中hello项目包中有一个hello_world.py文件和一个__init__.py文件

    hello_world.py

    def get_hello():
        return 'hello'

    setup.py中包含了package对应的信息(例如该package的名称、版本、作者)以及该package应当包含的程序文件和数据。

    from setuptools import setup, find_packages
    
    setup(
        name="hello",
        version="0.1",
        author="zz",
        author_email ="928003536@qq.com",
        url="www.cnblogs.com",
        description = "hello world test",
        packages=find_packages(),
        
    )
    • name: 该package的名字,该名字可以由字母、数字、-组成,注意这个名字不能与其它已经上传到pypi.org的项目相同
    • version: 这个就是包的发布版本,可以直接写在这,也可以从其它地方引用
    • author: author可以用来指定该package的作者信息
    • author_email: 这个也是指定该package的作者信息
    • description: 对当前package的较短的总结
    • long_description: 是对当前package的详细说明。这一详细说明将被展示在Python Package Index上当前项目的主页
    • long_description_content_type: 指定了long_description内容的格式。在当前情况下为markdown
    • url: 是当前package的主页链接。大多数情况下这是一个GitHub, GitLab, Bitbucket或者其他代码存储服务的链接
    • packages: 是一系列应当包含在发布软件包文件(distribution package)中的可被import的python包文件。我们可以手动在此处罗列所有文件。
    • 或者如本例中一样使用find_packages()函数自动包含所有的python包文件以及子包文件。
    • python_requires: python依懒版本
    • classifiers: 指定了当前package的其他元信息(metadata)。例如当前package兼容的python版本和操作系统,当前package提供的功能的类型,
    • 当前package的许可证等等。我们应当总是至少包括当前package所支持的python版本,操作系统和许可证。注意此处定义的classifiers关键字
    • 所包含的信息应当符合PyPI的规定。
    • install_requires: 指定了当前package所依赖的其他python类库。这些指定的python类库将会在本package被安装的时候一并被安装。
    • platforms: 程序适用的软件平台列表
    • keywords: 程序的关键字列表
    • include_package_data: 是否自动包含包内所有受版本控制(cvs/svn/git)的数据文件,默认True
    • entry_points: 用来支持自动生成cli命令

    其他示例:

    from setuptools import setup
    
    setup(name='pagtest',
      version='1.0.0',
      description='A print test for PyPI',
      author='winycg',
      author_email='win@163.com',
      url='https://www.python.org/',
      license='MIT',
      keywords='ga nn',
      project_urls={
       'Documentation': 'https://packaging.python.org/tutorials/distributing-packages/',
       'Funding': 'https://donate.pypi.org',
       'Source': 'https://github.com/pypa/sampleproject/',
       'Tracker': 'https://github.com/pypa/sampleproject/issues',
      },
      packages=['pagtest'],
      install_requires=['numpy>=1.14', 'tensorflow>=1.7'],
      python_requires='>=3'
      )
    from setuptools import setup, find_packages
     
    setup(
        name = "driver",
        version = "0.1",
        packages = find_packages(),
        #目标文件
        py_modeles = 'driver_classification.py',
        #excel文件
        data_file = 'driver_model_data.xlsx',
        include_package_data = True,
        #包含所有.xlsx文件
        package_data = {
        '':['*.xlsx'],}

     1.3打包

    检查:

    python setup.py check

    打包:

    python setup.py bdist_wheel
    python setup.py sdist bdist_wheel # 多一个tar.gz包

     运行后会在当前目录多出3个文件夹:build、dist、driver.egg-info

    dist目录下有一个whl文件

    2 安装whl文件

    测试

    # -*- coding: utf-8 -*-
    
    from hello import hello_world
    
    res = hello_world.get_hello()
    print(res)

     

  • 相关阅读:
    git merge branch
    Notes on Large-scale Video Classification with Convolutional Neural Networks
    ubuntu shell编程笔记
    cpu-z for ubuntu 12.04 64bit : cpu-g
    Notation, First Definitions 转 http://brnt.eu/phd/node9.html
    textext for Inkscape
    read later
    Matlab远程调试 转
    Ubuntu中的在文件中查找和替换命令
    Ubuntu 下matlab 查看memory函数
  • 原文地址:https://www.cnblogs.com/xinmomoyan/p/14786040.html
Copyright © 2020-2023  润新知