Python 官方文档:入门教程 => 点击学习
python项目打包发布汇总 【Python】Python项目打包发布(一)(基于Pyinstaller打包多目录项目) 【Python】Python项目打包发布(二)(基于Pyinstaller打包
【Python】Python项目打包发布(一)(基于Pyinstaller打包多目录项目)
【Python】Python项目打包发布(二)(基于Pyinstaller打包PyWebIO项目)
【Python】Python项目打包发布(三)(基于Aardio打包多目录项目)
【Python】Python项目打包发布(四)(基于Nuitka打包PySide6项目)
【Python】Python项目打包发布(五)(制作Windows安装包)
nuitka是一个可以将Python代码转换为c++代码并编译为可执行文件或扩展模块的工具。
Nuitka官网
Nuitka官网中文
Nuitka中文
python venv是一个用于创建和管理虚拟环境的模块。虚拟环境是一种可以在系统中隔离安装Python包的方法,避免与其他项目或系统级别的包发生冲突。
使用python venv的基本步骤如下:
python -m venv
命令,在指定的目录下创建一个虚拟环境,该目录会包含一个Python解释器和一些支持文件。通常,可以将虚拟环境命名为 .venv ,这样可以在终端中隐藏它,并且表明它的用途。source /bin/activate
命令(linux或MacOS)或call \Scripts\activate.bat
命令(windows)来激活虚拟环境。这样,就可以在虚拟环境中使用 pip 安装或卸载所需的包了。deactivate
命令来退出虚拟环境。这样,就可以恢复到系统级别的Python解释器和包了。激活虚拟环境后,可以创建或使用requirements文件
pip freeze > requirements.txt
自动生成requirement.txt,执行成功后,会自动生成requirement.txt文件。pip install -r requirements.txt
python -m pip install -U nuitka
安装“ordered-set’”PyPI包以获得最佳的Python编译性能。
python -m pip install -U ordered-set
创建名为 hello.py 的 Python 文件
def talk(message): return "Talk " + messagedef main(): print(talk("Hello World"))if __name__ == "__main__": main()
采用python -m nuitka hello.py
方式构建,第一次使用会提示下载一个 C 语言缓存工具GCC(以加速重复编译生成的 C 代码)和一个基于 MinGW64 的 C 语言编译器。选择yes,会自动下载,也可以自己下载。
MinGW64 10.0.0 64位下载
MinGW环境配置,请自行百度
nuitka参数有很多,可以使用nuitka --help
命令查看
可以分为以下几类:
一般选项:用于指定输入文件、输出文件、日志级别、帮助信息等。
编译选项:用于指定编译器、优化级别、调试信息等。
控制结果中包含的模块和包:用于指定要包含或排除的模块和包,以及是否使用标准库等。
控制结果中包含的数据文件:用于指定要包含或排除的数据文件,以及是否使用资源压缩等。
插件选项:用于启用或禁用一些特定功能的插件,例如Qt、Tkinter、multiprocessing等。
其他选项:用于指定一些其他功能,例如图形界面、生成依赖关系图、检查更新等。
您可以使用 nuitka --help 命令查看所有参数的详细说明。
以下是之前版本参数的参考,最新请自行官网学习
--mingw64 #默认为已经安装的vs2017去编译,否则就按指定的比如mingw(官方建议)--standalone 独立环境,这是必须的(否则拷给别人无法使用)--windows-disable-console 没有CMD控制窗口--output-dir=out 生成exe到out文件夹下面去--show-progress 显示编译的进度,很直观--show-memory 显示内存的占用--include-qt-plugins=sensible,styles 打包后PyQt的样式就不会变了--plugin-enable=qt-plugins 需要加载的PyQt插件--plugin-enable=tk-inter 打包tkinter模块的刚需--plugin-enable=numpy 打包numpy,pandas,matplotlib模块的刚需--plugin-enable=torch 打包PyTorch的刚需--plugin-enable=Tensorflow 打包tensorflow的刚需--windows-icon-from-ico=你的.ico 软件的图标--windows-company-name=Windows下软件公司信息--windows-product-name=Windows下软件名称--windows-file-version=Windows下软件的信息--windows-product-version=Windows下软件的产品信息--windows-file-description=Windows下软件的作用描述--windows-uac-admin=Windows下用户可以使用管理员权限来安装--linux-onefile-icon=Linux下的图标位置--onefile 像pyinstaller一样打包成单个exe文件--include-package=复制比如numpy,PyQt5 这些带文件夹的叫包或者轮子--include-module=复制比如when.py 这些以.py结尾的叫模块
项目requirements.txt
beautifulsoup4==4.11.2bs4==0.0.1certifi==2022.12.7charset-nORMalizer==3.0.1idna==3.4Nuitka==1.4.8numpy==1.24.2ordered-set==4.1.0PySide6==6.4.2PySide6-Addons==6.4.2PySide6-Essentials==6.4.2requests==2.28.2shiboken6==6.4.2soupsieve==2.4urllib3==1.26.14
项目结构
set pythonpath=venv\Lib\site-packages
执行命令
nuitka --mingw64 --standalone --windows-disable-console --show-memory --show-progress --plugin-enable=pyside6 --nofollow-import-to=tkinter,mpl_toolkits --plugin-enable=tk-inter --windows-icon-from-ico=loGo.ico --output-dir=o main.py
nuitka.utils.Execution.NuitkaCalledProcessError: Command ‘[‘venv\Scripts\python.exe’, ‘-c’, ‘\\nfrom future import print_function\nfrom future import absolute_import\n\ntry:\n import os, sys\n library_path = os.path.join(“Library”, “bin”) if os.name == “nt” else “lib”\n library_prefix = “mkl_” if os.name == “nt” else “libmkl_”\nexcept ImportError:\n import sys\n sys.exit(38)\nprint(repr([os.path.join(sys.prefix, library_path, filename)\n for filename in os.listdir(os.path.join(sys.prefix, library_path))\n if filename.startswith(library_prefix)]\n))\nprint(“-” * 27)\n’]’ returned non-zero exit status 1. Error was b’Traceback (most recent call last):\r\n File “”, line 13, in \r\nFileNotFoundError: [WinError 3] \xcf\xb5\xcd\xb3\xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc2\xb7\xbe\xb6\xa1\xa3: ‘zssw\\venv\\Library\\bin’'.
根据您提供的错误信息,问题似乎出在 FileNotFoundError: [WinError 3]。这意味着程序在尝试访问一个不存在的文件或目录。在这种情况下,它似乎是在寻找 \venv\Library\bin 这个目录,但没有找到。
解决方法:
将conda目录下的libary复制到venv下
可以在dist文件夹生成之后,将icon文件复制进去,使用EVB封包
–help
显⽰此帮助信息并退出
–version
显⽰版本信息和提交 bug 报告所需的重要细节,然后退出。默认关闭。
–module
创建⼀个扩展模块可执⾏⽂件,⽽不是⼀个程序。默认关闭。
–standalone
启⽤独⽴模式输出。这允许您将创建的⼆进制⽂件转移到其他机器上,⽽不需要现有的 Pyth
on 安装。这也意味着它会变⼤。它隐含了这些选项: - follow-imports 和 - python-fla
g=no_site 。默认关闭。
–onefile
在独⽴模式的基础上,启⽤ onefile 模式。这意味着创建并使⽤的不是⼀个⽂件夹,⽽是
⼀个压缩的可执⾏⽂件。默认关闭。
–python-debug
是否使⽤ debug 版本。默认使⽤您⽤来运⾏ Nuitka 的版本,很可能是⾮ debug 版本。
–python-flag=FLAG
要使⽤的 Python 标志。默认是您⽤来运⾏ Nuitka 的标志,这强制执⾏⼀个特定的模式。这些
选 项 也 存 在 于 标 准 Python 可 执 ⾏ ⽂ 件 中 。 当 前 ⽀ 持 的 有 : “-S” ( alias
“no_site”),“static_hashes”(不使⽤散列随机化),“no_warnings”(不给出 Python 运⾏时警
告),“-O” (alias “no_asserts” ),“no_docstrings” (不使⽤⽂档字符串),“-u” (alias
“unbuffered”)和"-m"。默认为空。
–python-for-scons=PATH
如果使⽤ python3.3 或 Python3.4,提供⼀个 Python ⼆进制⽂件的路径供 Scons 使⽤。否则
Nuitka 可以使⽤您⽤来运⾏ Nuitka 的⼆进制⽂件或从 Windows 注册表中获取⼀个 Python 安
装。在 Windows 上需要 Python 3.5或更⾼版本。在⾮ Windows 上,Python 2.6 或 2.7 也可以。
–include-package=PACKAGE
包含⼀个完整的包。提供为⼀个 Python 命名空间,例如 " some_package.sub_packag
e “,然后 Nuitka 会找到它,并将其及其下⾯找到的所有模块包含在它创建的⼆进制⽂件或扩
展模块中,并使代码可以导⼊它。为了避免不需要的⼦包,例如测试,您可以这样做” - nofoll
ow-import-to=*.tests "。默认为空。
使⽤这个选项后,所有在 PACKAGE 中被列出来的包,例如 - include-package=av,PySi
de6,faster_whisper,transformers,ctranslate2 ,所有这些包都 将会被完整编译,这将
增加编译时间,同时可能解决⼀部分编译后出现模块或者⼦模块⽆法找到的错误,例如: Model
FoundError 、 importError 、 No model Named XXXX 。
–include-module=MODULE
包含⼀个单⼀的模块。提供为⼀个 Python 命名空间,例如 " some_package.some_modul
e ",然后 Nuitka 会找到它,并将其包含在它创建的⼆进制⽂件或扩展模块中,并使代码可以导
⼊它。默认为空。
–include-plugin-directory=MODULE/PACKAGE
在该⽬录中找到的代码也将被包含,就像它们每个都作为主⽂件给出⼀样。覆盖所有其他包
含选项。您应该更喜欢按名称⽽不是⽂件名的其他包含选项,因为它们通过在“sys.path”中来查
找东西。这个选项仅⽤于⾮常特殊的使⽤案例。可以多次给定。默认为空。
–include-plugin-files=PATTERN
包含匹配 PATTERN 的⽂件。覆盖所有其他 follow 选项。可以多次给出。默认为空。
–prefer-source-code
对于已经编译的扩展模块,如果同时存在源⽂件和扩展模块,通常使⽤扩展模块,但从可⽤
的源代码编译模块性能最佳。如果不需要,使⽤ - no-prefer-source-code 来禁⽤关于此的
警告。默认关闭。
–follow-imports
递归到所有导⼊的模块。在 - standalone 模式下默认开启,否则关闭。
–follow-import-to=MODULE/PACKAGE
如果使⽤了这个模块,或者如果是⼀个包,跟踪到整个包。可以多次给出。默认为空。
–nofollow-import-to=MODULE/PACKAGE
即使使⽤了也不跟踪这个模块,或者如果是⼀个包,在任何情况下也不跟踪整个包,覆盖所有
其他选项。可以多次给出。默认为空。
–nofollow-imports
完全不递归到任何导⼊的模块,覆盖所有其他包含选项,不能⽤于独⽴模式。默认关闭。
–follow-stdlib
递归到标准库中导⼊的模块。这将极⼤地增加编译时间,⽬前也没有很好地测试过,有时也不
起作⽤。默认关闭。
–onefile-tempdir-spec=ONEFILE_TEMPDIR_SPEC
在 onefile 模式下解压缩到此⽂件夹。默认 ‘%TEMP%/onefile_%PID%_%TIME% ,即⽤
⼾临时⽬录,由于不是静态的,退出时会被删除。例如,使⽤类似’ CACHE_DIR%/%COMPANY%/%
PRODUCT%/%VERSION% 的字符串,这是⼀个很好的静态缓存路径,因此不会被删除。
–onefile-child-grace-time=GRACE_TIME_MS
当停⽌⼦进程时,例如由于 CTRL-C 或关闭等, Python 代码会收到⼀个 KeyboardInte
rrupt ,它可以处理,例如刷新数据。这是在硬关闭⼦进程前的宽限时间(单位毫秒)。默认值
为 。
–include-package-data=PACKAGE 来源地址:https://blog.csdn.net/qq_25262697/article/details/129302819
包含给定包名的数据⽂件。 DLL 和扩展模块不是数据⽂件,不会像这样包含。可以像下⾯
指⽰的那样使⽤通配符。默认情况下不包含包的数据⽂件,但包配置可以做到这⼀点。这只会包
含⾮ DLL 、⾮扩展模块的实际数据⽂件。冒号后可选地可以给出⼀个⽂件名模式,仅选择匹配
的⽂件。例⼦:
include-package-data=package_name (所有⽂件)
include-package-data=package_name=.txt (仅某种类型)
-include-package-data=package_name=some_filename.dat ( 具 体 ⽂ 件 ) 默 认 为
空。
–include-data-files=DESC
通过发⾏版中的⽂件名包含数据⽂件。有许多允许的形式。
使⽤ - include-data-files=/path/to/file/.txt=folder_name/some.txt 它会复
制⼀个单⼀⽂件,如果是多个会抱怨。
使⽤ - include-data-files=/path/to/files/.txt=folder_name/ 它会把所有匹配
的⽂件放⼊那个⽂件夹。
要进⾏递归复制,有⼀个带 个值的表单,即 - include-data-files=/path/to/scan=f
older_name=**/.txt 这会保留⽬录结构。默认为空。
–include-data-dir=DIRECTORY
从发⾏版中包含完整的⽬录中的数据⽂件。这是递归的。如果您想要⾮递归包含,请使⽤带
通配符的 - include-data-files 。⼀个例⼦是 - include-data-dir=/path/some_dir=d
ata/some_dir ,⽤于简单复制整个⽬录。所有⽂件都将被复制,如果您想排除⽂件,您需要事
先将其删除,或使⽤ - noinclude-data-files 选项将其删除。默认为空。
–noinclude-data-files=PATTERN
不要包含匹配给定的⽂件名模式的数据⽂件。这是针对⽬标⽂件名,⽽不是源路径。因此,
要忽略来⾃ package_name 的数据⽂件的⽂件模式,应匹配为 package_name*.txt' that will preserve directory structure. Default empty. --include-data-dir=DIRECTORY Include data files from complete directory in the distribution. This is recursive. Check '--include- data-files' with patterns if you want non-recursive inclusion. An example would be '--include-data- dir=/path/some_dir=data/some_dir' for plain copy, of the whole directory. All files are copied, if you want to exclude files you need to remove them beforehand, or use '--noinclude-data-files' option to remove them. Default empty. --noinclude-data-files=PATTERN Do not include data files matching the filename pattern given. This is against the target filename, not source paths. So to ignore a file pattern from package data for "package_name" should be matched as "package_name/*.txt". Or for the whole directory simply use "package_name". Default empty. --list-package-data=LIST_PACKAGE_DATA Output the data files found for a given package name. Default not done. DLL files: --noinclude-dlls=PATTERN Do not include DLL files matching the filename pattern given. This is against the target filename, not source paths. So ignore a DLL "someDLL" contained in the package "package_name" it should be matched as "package_name/someDLL.*". Default empty. --list-package-dlls=LIST_PACKAGE_DLLS Output the DLLs found for a given package name. Default not done. Control the warnings to be given by Nuitka: --warn-implicit-exceptions Enable warnings for implicit exceptions detected at compile time. --warn-unusual-code Enable warnings for unusual code detected at compile time. --assume-yes-for-downloads Allow Nuitka to download external code if necessary, e.g. dependency walker, ccache, and even gcc on Windows. To disable, redirect input from nul device, e.g. "WEBsite. The mnemonic is the part of the URL at the end, without the html suffix. Can be given multiple times and accepts shell pattern. Default empty. Immediate execution after compilation: --run Execute immediately the created binary (or import the compiled module). Defaults to off. --debugger Execute inside a debugger, e.g. "gdb" or "lldb" to automatically get a stack trace. Defaults to off. --execute-with-pythonpath When immediately executing the created binary or module using '--run', don't reset 'PYTHONPATH' environment. When all modules are successfully included, you ought to not need PYTHONPATH anymore, and definitely not for standalone mode. Compilation choices: --user-package-configuration-file=YAML_FILENAME User provided Yaml file with package configuration. You can include DLLs, remove bloat, add hidden dependencies. Check User Manual for a complete description of the format to use. Can be given multiple times. Defaults to empty. --full-compat Enforce absolute compatibility with CPython. Do not even allow minor deviations from CPython behavior, e.g. not having better tracebacks or exception messages which are not really incompatible, but only different or worse. This is intended for tests only and should *not* be used. --file-reference-choice=MODE Select what value "__file__" is going to be. With "runtime" (default for standalone binary mode and module mode), the created binaries and modules, use the location of themselves to deduct the value of "__file__". Included packages pretend to be in directories below that location. This allows you to include data files in deployments. If you merely seek acceleration, it's better for you to use the "original" value, where the source files location will be used. With "frozen" a notation "
--结束END--
本文标题: 【Python】Python项目打包发布(四)(基于Nuitka打包PySide6项目)
本文链接: https://www.lsjlt.com/news/424005.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0