0

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.py compiled as a single executable.
  • __init__.py should 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.
2
  • maybe first check if you have folder /tmp/onefile_278932_1750239107_362003/commands when you run program. Maybe it doesn't add this code to your file script.bin and it may needs to add folder commands manually to pyinstaller configuration. Commented Jun 18 at 12:48
  • in code you could use print() to check what you get with os.listdir(cmds_dir). And you could use print() to debug rest of code. Commented Jun 18 at 12:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.