医疗手机网站模板,广东seo站外推广折扣,网站建设费属于无形资产吗,白佛网站建设一、setup.py文件的书写
这个资料有很多#xff0c;不多赘述#xff0c;setup 函数常用的参数如下#xff1a;
基础描述信息#xff1a; name 包名称#xff08;起一个响亮的名字#xff09;version (-V) 包版本author 程序的作者author_email 程序的作者的邮箱地址mai…一、setup.py文件的书写
这个资料有很多不多赘述setup 函数常用的参数如下
基础描述信息 name 包名称起一个响亮的名字version (-V) 包版本author 程序的作者author_email 程序的作者的邮箱地址maintainer 维护者maintainer_email 维护者的邮箱地址url 程序的官网地址license 程序的授权信息description 程序的简单描述long_description 程序的详细描述platforms 程序适用的软件平台列表keywords 程序的关键字列表classifiers 程序的所属分类列表包的进阶信息 packages 需要处理的包目录包含__init__.py的文件夹这里通常使用 find_packages()它默认在和setup.py同一目录下搜索各个含有 __init__.py的包。install_requires [requests] 需要安装的依赖包。我们可以首先生成 requirements.txt 文件接着使用生成的文件生成需要的参数。关于生成 requirements.txt 文件可以参考Python自动生成requirements.txt文件。include_package_data引入非 Python 文件。默认情况下只会对 Python 源码进行打包但是如果我们想要将其他静态文件例如 css 文件或是 qt 的一些 ui 文件打包进去就需要使用这个参数。后面会有详细的介绍。制作命令行工具 entry_points 动态发现服务和插件可以用来制作命令行工具。也就是我们可以通过一些简单的命令来运行 Python 项目中的指定文件或是函数。下面会详细进行介绍。
更多参数可见https://setuptools.readthedocs.io/en/latest/setuptools.html
二、setup.py 打包命令 setup.py打包命令各参数详解 python setup.py --help-commands --python setup.py build # 仅编译不安装 --python setup.py install #安装到python安装目录的lib下 --python setup.py sdist #生成压缩包(zip/tar.gz) --python setup.py bdist #生成平台安装包 --python setup.py bdist_wininst #生成NT平台安装包(.exe) --python setup.py bdist_rpm #生成rpm包 --python setup.py bdist_wheel 生成wheel包 --python setup.py egg_info # 单独生成egg_info信息
接下来以bandit 1.7.6为例进行讲解 egg-info目录是用于存储与Python包相关的元数据信息的目录。它通常位于包的顶级目录中以”.egg-info”为后缀。egg-info目录中包含了一些文本文件其中存储了包的名称、版本、作者、依赖项等重要信息。这些信息对于包的安装、发布和管理都是必要的。参考Python Python包和egg-info目录|极客教程
目录: D:\MyPython\my_setup\bandit-1.7.6\bandit.egg-infoMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023/12/9 10:53 1 dependency_links.txt
-a---- 2023/12/9 10:53 3822 entry_points.txt
-a---- 2023/12/9 10:53 1 not-zip-safe
-a---- 2023/12/9 10:53 47 pbr.json
-a---- 2023/12/9 10:53 4885 PKG-INFO
-a---- 2023/12/9 10:53 359 requires.txt
-a---- 2023/12/9 10:53 8935 SOURCES.txt
-a---- 2023/12/9 10:53 7 top_level.txt
其中PKG-INFO文件存储了关于包的详细信息如名称、版本、作者、许可证等。top_level.txt文件列出了包中的顶级模块。SOURCES.txt文件记录了包中包含的所有源代码文件。dependency_links.txt文件存储了与包相关的依赖项的URL链接。
pbr 是一个管理 python setuptools 的工具库为了方便使用setuptoolspbr 模块读入 setup.cfg 文件的信息并且给 setuptools 中的 setup hook 函数填写默认参数提供更加有意义的行为。pbr只需要最小化的setup.py 文件。参考python包管理和pbr — W.Young
import setuptoolssetuptools.setup(python_requires3.8, setup_requires[pbr2.0.0], pbrTrue
) package名称在setup.cfg中定义
[metadata]
name bandit
entry_points参数用来支持自动生成脚本其值应该为是一个字典从 entry_point 组名映射到一个表示 entry_point 的字符串或字符串列表
[entry_points]
console_scripts bandit bandit.cli.main:mainbandit-config-generator bandit.cli.config_generator:mainbandit-baseline bandit.cli.baseline:main
...
2.1 python setup.py build
build相当于把需要打包的文件先收集起来如下
PS D:\MyPython\my_setup\bandit-1.7.6 python setup.py build
running build
running build_py
creating build
creating build\lib
creating build\lib\bandit
creating build\lib\bandit\blacklists
copying bandit\blacklists\calls.py - build\lib\bandit\blacklists
copying bandit\blacklists\imports.py - build\lib\bandit\blacklists
copying bandit\blacklists\utils.py - build\lib\bandit\blacklists
copying bandit\blacklists\__init__.py - build\lib\bandit\blacklists
creating build\lib\bandit\core
copying bandit\core\blacklisting.py - build\lib\bandit\core
copying bandit\core\config.py - build\lib\bandit\core
copying bandit\core\constants.py - build\lib\bandit\core
copying bandit\core\context.py - build\lib\bandit\core
copying bandit\core\docs_utils.py - build\lib\bandit\core
copying bandit\core\extension_loader.py - build\lib\bandit\core
copying bandit\core\issue.py - build\lib\bandit\core
copying bandit\core\manager.py - build\lib\bandit\core
copying bandit\core\meta_ast.py - build\lib\bandit\core
copying bandit\core\metrics.py - build\lib\bandit\core
copying bandit\core\node_visitor.py - build\lib\bandit\core
copying bandit\core\tester.py - build\lib\bandit\core
copying bandit\core\test_properties.py - build\lib\bandit\core
copying bandit\core\test_set.py - build\lib\bandit\core
copying bandit\core\utils.py - build\lib\bandit\core
copying bandit\core\__init__.py - build\lib\bandit\core
creating build\lib\bandit\cli
copying bandit\cli\baseline.py - build\lib\bandit\cli
copying bandit\cli\config_generator.py - build\lib\bandit\cli
copying bandit\cli\main.py - build\lib\bandit\cli
copying bandit\cli\__init__.py - build\lib\bandit\cli
creating build\lib\bandit\plugins
copying bandit\plugins\app_debug.py - build\lib\bandit\plugins
copying bandit\plugins\asserts.py - build\lib\bandit\plugins
copying bandit\plugins\crypto_request_no_cert_validation.py - build\lib\bandit\plugins
copying bandit\plugins\django_sql_injection.py - build\lib\bandit\plugins
copying bandit\plugins\django_xss.py - build\lib\bandit\plugins
copying bandit\plugins\exec.py - build\lib\bandit\plugins
copying bandit\plugins\general_bad_file_permissions.py - build\lib\bandit\plugins
copying bandit\plugins\general_bind_all_interfaces.py - build\lib\bandit\plugins
copying bandit\plugins\general_hardcoded_password.py - build\lib\bandit\plugins
copying bandit\plugins\general_hardcoded_tmp.py - build\lib\bandit\plugins
copying bandit\plugins\hashlib_insecure_functions.py - build\lib\bandit\plugins
copying bandit\plugins\injection_paramiko.py - build\lib\bandit\plugins
copying bandit\plugins\injection_shell.py - build\lib\bandit\plugins
copying bandit\plugins\injection_sql.py - build\lib\bandit\plugins
copying bandit\plugins\injection_wildcard.py - build\lib\bandit\plugins
copying bandit\plugins\insecure_ssl_tls.py - build\lib\bandit\plugins
copying bandit\plugins\jinja2_templates.py - build\lib\bandit\plugins
copying bandit\plugins\logging_config_insecure_listen.py - build\lib\bandit\plugins
copying bandit\plugins\mako_templates.py - build\lib\bandit\plugins
copying bandit\plugins\request_without_timeout.py - build\lib\bandit\plugins
copying bandit\plugins\snmp_security_check.py - build\lib\bandit\plugins
copying bandit\plugins\ssh_no_host_key_verification.py - build\lib\bandit\plugins
copying bandit\plugins\tarfile_unsafe_members.py - build\lib\bandit\plugins
copying bandit\plugins\try_except_continue.py - build\lib\bandit\plugins
copying bandit\plugins\try_except_pass.py - build\lib\bandit\plugins
copying bandit\plugins\weak_cryptographic_key.py - build\lib\bandit\plugins
copying bandit\plugins\yaml_load.py - build\lib\bandit\plugins
copying bandit\plugins\__init__.py - build\lib\bandit\plugins
copying bandit\__init__.py - build\lib\bandit
copying bandit\__main__.py - build\lib\bandit
creating build\lib\bandit\formatters
copying bandit\formatters\csv.py - build\lib\bandit\formatters
copying bandit\formatters\custom.py - build\lib\bandit\formatters
copying bandit\formatters\html.py - build\lib\bandit\formatters
copying bandit\formatters\json.py - build\lib\bandit\formatters
copying bandit\formatters\screen.py - build\lib\bandit\formatters
copying bandit\formatters\text.py - build\lib\bandit\formatters
copying bandit\formatters\utils.py - build\lib\bandit\formatters
copying bandit\formatters\xml.py - build\lib\bandit\formatters
copying bandit\formatters\yaml.py - build\lib\bandit\formatters
copying bandit\formatters\__init__.py - build\lib\bandit\formatters
running egg_info
writing bandit.egg-info\PKG-INFO
writing dependency_links to bandit.egg-info\dependency_links.txt
writing entry points to bandit.egg-info\entry_points.txt
writing requirements to bandit.egg-info\requires.txt
writing top-level names to bandit.egg-info\top_level.txt
[pbr] Reusing existing SOURCES.txt
我们看到在同级目录下有个build目录生成bandit-1.7.6\build\lib\bandit下就是源码文件。
目录: D:\MyPython\my_setup\bandit-1.7.6\build\lib\banditMode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/1/20 21:08 blacklists
d----- 2024/1/20 21:08 cli
d----- 2024/1/20 21:08 core
d----- 2024/1/20 21:08 formatters
d----- 2024/1/20 21:08 plugins
-a---- 2023/12/9 10:53 632 __init__.py
-a---- 2023/12/9 10:53 571 __main__.py 执行如下命令查看所有命令; python setup.py build --help Options for build command: --build-base (-b) 构建根目录 --build-purelib 中间发行版的构建目录 --build-platlib 特定发行版的构建目录 --build-lib 所有发行版的构建目录默认为构建purelib或构建platlib --build-scripts 脚本的构建目录 --build-temp (-t) 临时构建目录 --plat-name (-p) 要生成的支持平台名称默认值win-amd64 --compiler (-c) 指定编译器类型 --parallel (-j) 并行任务数 --debug (-g) 使用调试信息编译扩展和库 --force (-f) 强制构建所有内容忽略文件时间戳 --executable (-e) 指定最终目标解释器路径build.py --help-compiler 列出可用的编译器 --compilerbcpp Borland C Compiler --compilercygwin Cygwin port of GNU C Compiler for Win32--compilermingw32 Mingw32 port of GNU C Compiler for Win32 --compilermsvc Microsoft Visual C--compilerunix standard UNIX-style compiler
1、指定构建根目录 --build-base
当前目录下生成了my_build的目录里面是bandit的源码。
python setup.py build --build-base my_build./my_build目录: D:\MyPython\my_setup\bandit-1.7.6\output\lib\banditMode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/1/20 23:07 blacklists
d----- 2024/1/20 23:07 cli
d----- 2024/1/20 23:07 core
d----- 2024/1/20 23:07 formatters
d----- 2024/1/20 23:07 plugins
-a---- 2023/12/9 10:53 632 __init__.py
-a---- 2023/12/9 10:53 571 __main__.py
2、发行版的构建目录--build-lib
注意多一层 lib目录看着有点不舒服可以通过--build-lib实现
python setup.py build --build-lib my_build目录: D:\MyPython\my_setup\bandit-1.7.6\my_buildMode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/1/20 23:15 bandit
2.2 python setup.py install
install会将build/lib下的文件以及egg-info进行打包并进行二进制化成.egg文件该文件在当前文件夹下的dist文件夹下并且会把这个egg文件复制到对应环境的site-packages包下面此时可以直接import 这个egg中的内容但是无法跳转另外还会生成egg-info文件夹里面就是一些文件 执行如下命令查看所有命令 python setup.py install --help Options for LocalInstall command: --prefix installation prefix --exec-prefix (Unix only) prefix for platform-specific files --home (Unix only) home directory to install under --install-base base installation directory (instead of --prefix or --home) --install-platbase base installation directory for platform-specific files (instead of --exec-prefix or --home) --root install everything relative to this alternate root directory --install-purelib installation directory for pure Python module distributions --install-platlib installation directory for non-pure module distributions --install-lib installation directory for all module distributions (overrides --install-purelib and --install-platlib) --install-headers installation directory for C/C headers --install-scripts installation directory for Python scripts --install-data installation directory for data files --compile (-c) compile .py to .pyc [default] --no-compile dont compile .py files --optimize (-O) also compile with optimization: -O1 for python -O, -O2 for python -OO, and -O0 to disable [default: -O0] --force (-f) force installation (overwrite any existing files) --skip-build skip rebuilding everything (for testing/debugging) --record filename in which to record list of installed files --user install in user site-package C:\Users\rnanprince\AppData\Roaming\Python\Python38\site-packages --old-and-unmanageable Try not to use this! --single-version-externally-managed used by system package builders to create flat eggs
2.3 python setup.py sdist
python setup.py sdist不会生成build文件夹只会生成dist和egg-info文件夹但此时dist下面的不是egg文件而是tar.gz文件是一个打包文件也是打包的相同的东西
D:\MyPython\my_setup\bandit-1.7.6 ls .\dist\目录: D:\MyPython\my_setup\bandit-1.7.6\distMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/1/20 23:38 98707 bandit-1.7.6.tar.gz其余的格式
python setup.py sdist --help-formatsList of available source distribution formats:--formatsbztar bzip2ed tar-file--formatsgztar gziped tar-file--formatstar uncompressed tar file--formatsxztar xzed tar-file--formatszip ZIP file--formatsztar compressed tar file执行如下命令编译zip包 python setup.py sdist --formatszip 结果如下
目录: D:\MyPython\my_setup\bandit-1.7.6\distMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/1/20 23:45 139518 bandit-1.7.6.zip
2.4 python setup.py bdist
网上说sdist和bdist的区别一个是源码包一个是二进制包这里看到bdist会同时生成build,distegg-info文件夹且dist下面也会生成tar.gz文件但我们可以看到sdist和bdist生成的两个tar.gz文件是不一样的。
1、python setup.py bdist
生成NT平台安装包(.zip)
python setup.py bdist目录: D:\MyPython\my_setup\bandit-1.7.6\distMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/1/20 23:38 98707 bandit-1.7.6.tar.gz
-a---- 2024/1/20 23:50 388251 bandit-1.7.6.win-amd64.zip
-a---- 2024/1/20 23:45 139518 bandit-1.7.6.zip具体参数
List of available distribution formats:--formatsrpm RPM distribution--formatsgztar gziped tar file--formatsbztar bzip2ed tar file--formatsxztar xzed tar file--formatsztar compressed tar file--formatstar tar file--formatswininst Windows executable installer--formatszip ZIP file--formatsmsi Microsoft Installer--formatsegg Python .egg file 2、python setup.py bdist --formatsgztar
生成NT平台安装包(tar.gz)
目录: D:\MyPython\my_setup\bandit-1.7.6\distMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/1/20 23:38 98707 bandit-1.7.6.tar.gz
-a---- 2024/1/20 23:57 295291 bandit-1.7.6.win-amd64.tar.gz
-a---- 2024/1/20 23:50 388251 bandit-1.7.6.win-amd64.zip
-a---- 2024/1/20 23:45 139518 bandit-1.7.6.zip
3、python setup.py bdist_wininst
生成NT平台安装包(.exe)
python setup.py bdist_wininst目录: D:\MyPython\my_setup\bandit-1.7.6\distMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/1/20 23:38 98707 bandit-1.7.6.tar.gz
-a---- 2024/1/20 23:49 827272 bandit-1.7.6.win-amd64.exe
-a---- 2024/1/20 23:57 295291 bandit-1.7.6.win-amd64.tar.gz
-a---- 2024/1/20 23:50 388251 bandit-1.7.6.win-amd64.zip
-a---- 2024/1/20 23:45 139518 bandit-1.7.6.zip4、python setup.py bdist_rpm
生成rpm包
python setup.py bdist_rpmrunning bdist_rpm
error: dont know how to create RPM distributions on platform nt
出错了再议再议。。。哈哈
5、python setup.py bdist_wheel
生成wheel包
python setup.py bdist_wheel目录: D:\MyPython\my_setup\bandit-1.7.6\distMode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/1/21 0:12 123459 bandit-1.7.6-py3-none-any.whl
-a---- 2024/1/20 23:38 98707 bandit-1.7.6.tar.gz
-a---- 2024/1/20 23:49 827272 bandit-1.7.6.win-amd64.exe
-a---- 2024/1/20 23:57 295291 bandit-1.7.6.win-amd64.tar.gz
-a---- 2024/1/20 23:50 388251 bandit-1.7.6.win-amd64.zip
-a---- 2024/1/20 23:45 139518 bandit-1.7.6.zip2.5 python setup.py egg_info
单独生成egg_info信息 python setup.py egg_info 生成的目录还是原来的目录如果想自定义目录呢
Options for LocalEggInfo command:--egg-base (-e) directory containing .egg-info directories (default: top of the source tree)--tag-date (-d) Add date stamp (e.g. 20050528) to version number--tag-build (-b) Specify explicit tag to add to version number--no-date (-D) Dont include date stamp [default]1、python setup.py egg_info --egg-base ./my_egg_info
目录: D:\MyPython\my_setup\bandit-1.7.6\my_egg_infoMode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/1/21 0:11 bandit.egg-info
2、 标记信息
--tag-buildNAME-b NAME
将NAME附加到项目的版本字符串中。由于setuptools处理以字母“a”到“e”如“alpha”、“beta”和“candidate”开头的“预发布”版本后缀的方式您通常希望使用“.build”或“.dev”这样的标记因为这会导致版本号被认为低于项目的默认版本。如果您想使版本号高于默认版本您可以始终取消标记构建然后使用以下一个或两个选项。
如果在setup.cfg中设置了默认的构建标记则可以在命令行中使用-b“”或--tag build“”作为egg_info命令的参数来抑制它。
--tag-date-d
在项目的版本号中添加一个格式为“-YYYYMMDD”例如“-2005 0528”的日期戳。
--no-date-D
不要在版本号中包含日期戳。此选项包括在内因此您可以覆盖setup.cfg中的默认设置。 参考
python setup.py 浅析-腾讯云开发者社区-腾讯云Python 库打包分发(setup.py 编写)简易指南Python: Directory not found or invalid file type prevents data_files from copying in Pythons setup.pypython包管理和pbr — W.YoungPython深入setuptools进阶 - 我用python写Bug - 博客园Python编程entry_points将Python模块转变为命令行工具_python entry_points console_scripts-CSDN博客Python打包分发工具setuptools简介 | 思诚之道如何使用setuptools和setup.py从包中排除单个文件-腾讯云开发者社区-腾讯云使用setuptools对Python进行打包分发 | 旅途札记Python包管理工具setuptools之setup函数参数详解Python Python包和egg-info目录|极客教程Running setuptools commands - setuptools 69.0.3.post20240114 documentationPython的setuptools详解【3】打包wheel并提交给pypi_python setup.py sdist bdist_wheel-CSDN博客https://www.cnblogs.com/skying555/p/5191503.htmlpython用spec打包 python setup打包_mob64ca14089531的技术博客_51CTO博客