首页 > 编程语言 >打包python模块成whl 使用pip安装

打包python模块成whl 使用pip安装

时间:2023-01-30 14:44:13浏览次数:54  
标签:files string package python setup list whl specifying pip

打包python模块


  目的,将写好的python库文件,打包成wheel,然后使用pip安装到系统,独立成模块。
  

使用工具

  需要提前使用pip安装wheel。
  打包使用setuptools库。

需要步骤

  1. 编写setup.py文件。
  2. 编写MANIFEST.in文件。

结构

dir1
    package_name
        sub_name1
        sub_name2
    setup.py
    MANIFEST.in

此结构表示打包package_name。setup.py,MANIFEST.in 与 package_name 同级目录

打包命令


python setup.py bdist_wheel      打包成whl格式文件

python setup.py bdist    打包成tar.gz格式文件

打包完成后会出现 dist文件夹 里面是whl文件和tar.gz文件
建议先打包tar.gz文件 查看里面的内容是否正确,校验后可打包成wheel文件

setup.py


from setuptools import setup, find_packages

setup(
    name="xxx",
    version="xxx",
    packages=['abc'],
    include_package_data=True,
    install_requires=['ccc']
)

表示
包名为xxx(pip list显示的包名)
版本为xxx(pip list显示的版本)
将abc目录打包
需要安装ccc包(pip 自动安装)


  其中name,version等参数可以在
https://setuptools.pypa.io/en/latest/references/keywords.html 查看。
  中文参考:https://www.cnblogs.com/potato-chip/p/9106225.html 以及 https://cloud.tencent.com/developer/article/2123540 等等。
常见的参数包括

name

A string specifying the name of the package.

version

A string specifying the version number of the package.

description

A string describing the package in a single line.

long_description

A string providing a longer description of the package.

long_description_content_type

A string specifying the content type is used for the long_description (e.g. text/markdown)

author

A string specifying the author of the package.

author_email

A string specifying the email address of the package author.

maintainer

A string specifying the name of the current maintainer, if different from the author. Note that if the maintainer is provided, setuptools will use it as the author in PKG-INFO.

maintainer_email

A string specifying the email address of the current maintainer, if different from the author.

url

A string specifying the URL for the package homepage.

download_url

A string specifying the URL to download the package.

packages

A list of strings specifying the packages that setuptools will manipulate.

py_modules

A list of strings specifying the modules that setuptools will manipulate.

scripts

A list of strings specifying the standalone script files to be built and installed.

ext_package

A string specifying the base package name for the extensions provided by this package.

ext_modules

A list of instances of setuptools.Extension providing the list of Python extensions to be built.

classifiers

A list of strings describing the categories for the package.

distclass

A subclass of Distribution to use.

script_name

A string specifying the name of the setup.py script – defaults to sys.argv[0]

script_args

A list of strings defining the arguments to supply to the setup script.

options

A dictionary providing the default options for the setup script.

license

A string specifying the license of the package.

platforms

A list of strings or comma-separated string.

cmdclass

A dictionary providing a mapping of command names to Command subclasses.

include_package_data

If set to True, this tells setuptools to automatically include any data files it finds inside your package directories that are specified by your MANIFEST.in file. For more information, see the section on Including Data Files.

exclude_package_data

A dictionary mapping package names to lists of glob patterns that should be excluded from your package directories. You can use this to trim back any excess files included by include_package_data. For a complete description and examples, see the section on Including Data Files.

package_data

A dictionary mapping package names to lists of glob patterns. For a complete description and examples, see the section on Including Data Files. You do not need to use this option if you are using include_package_data, unless you need to add e.g. files that are generated by your setup script and build process. (And are therefore not in source control or are files that you don’t want to include in your source distribution.)

zip_safe

A boolean (True or False) flag specifying whether the project can be safely installed and run from a zip file. If this argument is not supplied, the bdist_egg command will have to analyze all of your project’s contents for possible problems each time it builds an egg.

install_requires

A string or list of strings specifying what other distributions need to be installed when this one is. See the section on Declaring required dependency for details and examples of the format of this argument.

entry_points

A dictionary mapping entry point group names to strings or lists of strings defining the entry points. Entry points are used to support dynamic discovery of services or plugins provided by a project. See Advertising Behavior for details and examples of the format of this argument. In addition, this keyword is used to support Automatic Script Creation.

extras_require

A dictionary mapping names of “extras” (optional features of your project) to strings or lists of strings specifying what other distributions must be installed to support those features. See the section on Declaring required dependency for details and examples of the format of this argument.

python_requires

A string corresponding to a version specifier (as defined in PEP 440) for the Python version, used to specify the Requires-Python defined in PEP 345.

eager_resources

A list of strings naming resources that should be extracted together, if any of them is needed, or if any C extensions included in the project are imported. This argument is only useful if the project will be installed as a zipfile, and there is a need to have all of the listed resources be extracted to the filesystem as a unit. Resources listed here should be ‘/’-separated paths, relative to the source root, so to list a resource foo.png in package bar.baz, you would include the string bar/baz/foo.png in this argument.

If you only need to obtain resources one at a time, or you don’t have any C extensions that access other files in the project (such as data files or shared libraries), you probably do NOT need this argument and shouldn’t mess with it. For more details on how this argument works, see the section below on Automatic Resource Extraction.

project_urls

An arbitrary map of URL names to hyperlinks, allowing more extensible documentation of where various resources can be found than the simple url and download_url options provide.

MANIFEST.in 文件

此文件存在目的主要是打包非py格式的文件,如c++动态库so文件,以及其他文件等。

https://packaging.python.org/en/latest/guides/using-manifest-in/

例如

graft xxx/build

则包含 xxx/build 下所有文件一起打包

常见语法

语法 含义
include pat1 pat2 ... Add all files matching any of the listed patterns (Files must be given as paths relative to the root of the project)
exclude pat1 pat2 ... Remove all files matching any of the listed patterns (Files must be given as paths relative to the root of the project)
recursive-include dir-pattern pat1 pat2 ... Add all files under directories matching dir-pattern that match any of the listed patterns
recursive-exclude dir-pattern pat1 pat2 ... Remove all files under directories matching dir-pattern that match any of the listed patterns
global-include pat1 pat2 ... Add all files anywhere in the source tree matching any of the listed patterns
global-exclude pat1 pat2 ... Remove all files anywhere in the source tree matching any of the listed patterns
graft dir-pattern Add all files under directories matching dir-pattern
prune dir-pattern Remove all files under directories matching dir-pattern

中文参考

https://blog.csdn.net/weixin_43590796/article/details/121122850

标签:files,string,package,python,setup,list,whl,specifying,pip
From: https://www.cnblogs.com/XUEYEYU/p/17075889.html

相关文章

  • C++调用Python的API总结
    最近在做C++调Python的work,简单总结下(一) 初始化和关闭Python解释器#include<Python.h>Py_Initialize();…Py_Finalize();所有的Python程序都要在这之间执行(二)......
  • C++调用python脚本
    随着机器学习/深度学习这几年的的火热,python成了当红炸子鸡,使用python训练机器学习模型则成了开发人员们最喜欢的方法,但是由于过往调度系统一般都是用C++来开发的,因此......
  • python获取系统盘符列表
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#需求:#动态捕获指定服务器最新发布接种应用系统路径,重......
  • python接口测试常见问题。
    一.入参问题1.body字段类型1.如果数据是从excel提取的那么中文数据会提示错误。解决方法#media_value['body']=media_value['body'].encode("utf-8").decode("latin1......
  • Python自动批量修改文件名称的方法
      本文介绍基于Python语言,按照一定命名规则批量修改多个文件的文件名的方法。  已知现有一个文件夹,其中包括班级所有同学上交的作业文件,每人一份;所有作业文件命名格式......
  • 【7】Python3函数
    Python函数函数是一个被命名的、独立的、完成特定功能的可重复使用的代码段,其可能给调用它的程序一个返回值。 被命名的:在Python中,大部分函数都是有名函数完成特定功......
  • 【自动化持续集成必备基础】pipeline常用功能
     简介pipeline是一套运行在jenkins上的工作流框架,可以实现复杂流程的编排和可视化运行pipeline支持两种语法:DeclarativePipeline(声明式流水线)和ScriptedPipeline(脚本式......
  • 【KAWAKO】audiotsm-使用python对音频进行变速不变调处理
    目录安装库导入相关库定义reader定义writer定义WSLOA算法,并运行官方手册源码安装库pipinstallaudiotsm导入相关库importaudiotsmimportaudiotsm.io.wavimpo......
  • python语法到底多精妙?八大核心语句,带你深度了解
    Python是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视Python语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现Python语法之精妙的十......
  • 【6】Python3循环语句之while,for循环
    1.循环:有条件地重复地做一件事,每一次做的事情不同但类似在Python中,循环结构分为while和for两种。while判断条件:执行语句……执行语句可以是单个语句或语句块。判断......