打包python模块成whl 使用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

posted @ 2023-01-30 14:40  雪夜羽  阅读(1987)  评论(0编辑  收藏  举报