Search code examples
pythondiscord.pypyinstaller

Pyinstaller,how to solve "ModuleNotFoundError: No module named 'cogs'" error while trying to convert py file to.exe?


I've been trying to convert my main.py file to .exe so users don't have to install python

pyinstaller --onefile -w main.py

.After I create the .exe file,I try to run the exe and i get the above error.I've been using cogs for categorizing my commands.In the below photo,these modules are must be used in .exe version.How can i do that?Could someone help me?

The full error message:

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'

The photo of main.py and other files: main.py and other files


Solution

  • #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'