Skip to main content

PyCharm 自动生成 requirements.txt

使用Python打开自己的工程,然后点击Tools,最后点击Sync Python Requirements

pip install -r requirements.txt

其他注意事项:

  • 确保 requirements.txt 文件位于项目的根目录。
  • 在提交代码到版本控制系统之前,更新 requirements.txt 文件。
  • 在使用 pip install -r requirements.txt 安装依赖项时,确保使用与项目环境匹配的 Python 版本。

打包执行以下命令

pyinstaller -n [your_exe_name] -F -w -i [your_ico] [your_main].py

会根据打包指定的py文件解析所有依赖自动打包所有依赖文件,并生成一个可执行文件。

--hidden-import 设置导入要动态加载的类 因为没被引用 所以不会导入需要手动设置

上述命令中的选项说明:

-n NAME, --name NAME: 可执行文件名称。

-F, --onefile: 将整个项目打包为一个单独的可执行文件。

-w, --windowed, --noconsole: 隐藏控制台窗口,将打包的应用程序显示为GUI应用程序。

打包报错排查

  • 无法将“pyinstaller”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

    解决方案: 安装 PyInstaller

    pip install PyInstaller

    pyinstaller --name=<your_exe_name> --onefile --windowed --add-data "<your_data_folder>;<your_data_folder>" <your_script_name>.py

  • win32ctypes.pywin32.pywintypes.error: (225, 'EndUpdateResourceW', '无法成功完成操作,因为文件包含病毒或潜在的垃圾软件。

    不使用-w --noconsole的命令打包隐藏命令行窗口时,是正常的,

    但是使用-w或者--noconsole就会报错win32ctypes.pywin32.pywintypes.error: (225, '', '无法成功完成操作,因为文件包含病毒或潜在的垃圾软件。')

    解决方案: 降级 PyInstaller, 安装6.2.0重新打包即可

    pip install pyinstaller==6.2.0

requirements

pip freeze > requirements.txt

pip install -r requirements.txt

打包

pyinstaller -n [your_exe_name] -F -w -i [your_ico] [your_main_].py

pyinstaller --onefile --add-data "[your_folder]/*.py;[your_folder]" [your_main_].py

pyinstaller --onefile --add-data "[your_folder];[your_folder]" [your_main_].py

pyinstaller --onefile --hidden-import=[your_module] [your_main_].py

--hidden-import--add-data 的区别

在使用 PyInstaller 打包 Python 应用程序时,--hidden-import--add-data 是两个用途不同的参数,具体区别如下:

--hidden-import

  • 用途:用于显式指定那些未在代码中直接引用但运行时需要的模块。
  • 场景:当代码中通过字符串导入模块(如 importlib.import_module())或动态导入某些模块时,PyInstaller 可能无法自动检测到这些依赖项,这时需要使用 --hidden-import 手动添加这些模块。
  • 示例--hidden-import=module_name

--add-data

  • 用途:用于将非 Python 文件(如图片、配置文件、资源文件等)打包进最终的可执行文件。
  • 场景:当项目依赖外部文件(如图标、JSON 配置文件、模板等)时,使用 --add-data 指定这些文件或目录,确保它们在运行时可被访问。
  • 格式--add-data "<source_path>;<destination_subdir>"(Windows 上使用分号 ; 分隔,Linux/macOS 使用冒号 :
  • 示例--add-data "resources;resources" 表示将本地的 resources 文件夹打包到可执行文件中的 resources 子目录中。

总结对比

参数 用途 示例用法
--hidden-import 添加运行时需要但未被自动检测的模块 --hidden-import=my_module
--add-data 添加非代码资源文件 --add-data "resources;resources"

: 添加项目资源,支持文件夹和文件,前面是资源路径,后面是输出路径,用分号进行分割。

-i <FILE.ico>, --icon <FILE.ico> : 指定icon

执行上述命令后,会在项目目录下生成一个.spec文件,这个文件会告诉PyInstaller如何将项目打包成exe文件。

Release files for wings-easy 0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wings-easy 0.7
File Size Uploaded
wings_easy-0.7.tar.gz 19.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wings-easy 0.7
File Interpreter ABI Platform
wings_easy-0.7-py3-none-any.whl Python 3 none any Details

Total release size: 40.4 kB

Release files / wings_easy-0.7.tar.gz

Download URL wings_easy-0.7.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
975196e30f1a96a564ba8d5ad8a5c8a3c3bdd252f5e07cc804daf7c08bd0ded8
BLAKE2b-256 checksum
How to use checksums
462863b4a898d02f9ebeddc6a950d271d74a507035853f48ec3661c50b802b23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / wings_easy-0.7-py3-none-any.whl

Download URL wings_easy-0.7-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e892fbdff224085d8acb57c110ad388b5059cefa6652541bc5a33a42f58daca
BLAKE2b-256 checksum
How to use checksums
3a42cd79e71d004e1294a86d859cb6e6aea70948f22e56594ebf369f6453b970
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.7 This release

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page