I'm working on a Pycord bot project, and I'm using Nuitka to compile it. My goal is to compile the main script as a single file (--onefile) for distribution, but still be able to dynamically load command modules from a commands/ directory at runtime.
Project Structure (I must keep this layout):
Project/
├── script.py # compiled with --onefile
└── commands/
├── __init__.py # NOT compiled
└── ping.py # compiled with --module
Reason for this structure is I want to load command extensions dynamically like this:
import os
def load_all(bot):
cmds_dir = os.path.dirname(__file__) # Root directory for the commands.
files = os.listdir(cmds_dir) # List of files & folder of commands root directory.
for file in files:
name, ext = os.path.splitext(file)
if name != "__init__":
bot.load_extension(f"commands.{name}")
Each commands/*.py file contains:
from discord.ext import commands
def setup(bot):
@bot.command()
async def ping(ctx):
await ctx.respond("Pong!")
After building with Nuitka (script.py using --onefile, and ping.py with --module), running the executable gives:
┌──(.venv)─(...)-[~/Desktop/PyTesting]
└─$ ./script.bin
Traceback (most recent call last):
File "/tmp/onefile_278932_1750239107_362003/script.py", line 21, in <module>
File "/tmp/onefile_278932_1750239107_362003/commands/__init__.py", line 7, in load_all
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/onefile_278932_1750239107_362003/commands'
My goals:
script.pycompiled as a single executable.__init__.pyshould stay uncompiled (used for dynamic loading logic)- Only recompile individual command modules (like ping.py) when needed, instead of recompiling everything — to speed up development.
- This structure is used to only recompile command files if modified, if any fix to this issue with this feature it will be fine.
/tmp/onefile_278932_1750239107_362003/commandswhen you run program. Maybe it doesn't add this code to your filescript.binand it may needs to add foldercommandsmanually topyinstallerconfiguration.print()to check what you get withos.listdir(cmds_dir). And you could useprint()to debug rest of code.