首页 > 解决方案 > Pyinstaller,如何在尝试将 py 文件转换为 .exe 时解决“ModuleNotFoundError: No module named 'cogs'”错误?

问题描述

我一直在尝试将我的main.py文件转换为 .exe,这样用户就不必安装 python

pyinstaller --onefile -w main.py

.创建.exe文件后,我尝试运行exe并收到上述错误。我一直在使用cogs对我的命令进行分类。在下面的照片中,这些模块必须在 .exe 版本中使用。我该怎么做?有人可以帮我吗?

完整的错误信息:

Traceback (most recent call last):

  File "main.py", line 212, in <module>

    client.load_extension(f"cogs.{filename[:-3]}")

  File "discord\ext\commands\bot.py", line 674, in load_extension

  File "importlib\util.py", line 94, in find_spec

ModuleNotFoundError: No module named 'cogs'

main.py等文件的照片: main.py等文件

标签: pythondiscord.pypyinstaller

解决方案


#Convert .py to .exe don't use python ver 3.9 will not work, recommend 3.8:

pip install pyinstaller

#to convert to a simple exe file the exe file will be in your dist folder
pyinstaller 'fileName.py'

#to convert to a onefile exe file the exe file will be in your dist folder
pyinstaller --onefile 'fileName.py'

#to convert to a onefile exe file and the python window will not appear
pyinstaller -w --onefile 'fileName.py'

推荐阅读