Apologies in advance for how messy and meandering this question (and my code) may end up becoming. I have attempted to exhaust every single possible solution, without much luck, before deciding to post my problem here.
I am working on a Discord bot written in Discord.py, and having it hosted on Heroku. Everything is properly set-up on Heroku (i.e. requirements.txt, Procfile, etc). I have been getting errors from the Heroku log regarding a ModuleNotFoundError
. This is weird because the code works perfectly fine on my desktop, laptop, and even RPi. Below is the log in question:
2020-08-13T16:27:38.629564+00:00 heroku[worker.1]: Starting process with command `python3 ./bot/exalted-sage.py`
2020-08-13T16:27:39.205170+00:00 heroku[worker.1]: State changed from starting to up
2020-08-13T16:27:42.106185+00:00 app[worker.1]: Traceback (most recent call last):
2020-08-13T16:27:42.106211+00:00 app[worker.1]: File "./bot/exalted-sage.py", line 20, in <module>
2020-08-13T16:27:42.106423+00:00 app[worker.1]: import dispatch
2020-08-13T16:27:42.106476+00:00 app[worker.1]: ModuleNotFoundError: No module named 'dispatch'
2020-08-13T16:27:42.222494+00:00 heroku[worker.1]: Process exited with status 1
2020-08-13T16:27:42.281076+00:00 heroku[worker.1]: State changed from up to crashed
So, I suspect it has to do with the way I'm doing things within my bot program. Here is the directory breakdown that I have. I'm only including that which I feel would be necessary in finding a solution to my problem:
bot/
cogs/
daily_alert.py
dispatch.py
data/
dispatcher.json
.env
exalted-sage.py
settings.py
Procfile
requirements.txt
runtime.txt
The bot file is exalted-sage.py
. The way how I've been importing each Cog object is very weird, and it has been working for me so far, just not on Heroku. In exalted-sage.py
, I have a init_cogs()
function that takes a list of the cogs that I would like to load to the bot. This list is obtained from the dispatcher.json
file stored within the data
subdirectory. From there, I make use of the importlib
module to import the cogs I want to load to the bot:
def init_cogs(bot, cog_list):
"""Add all the cogs in the given list of available cogs"""
for cog in cog_list:
importlib.import_module(cog)
bot.add_cog(dispatch.dispatcher[cog]())
The dispatch
object in question in the code above comes from an import statement I have near the top of the file where I've done this:
sys.path.insert(1, settings.COGS_PATH)
import dispatch
Anyone have any ideas or suggestions on what it could be? More importantly, is there a better way of doing anything that I have been doing so far? Any help would be greatly appreciated!
Here is the GitHub repo if anyone is interested at taking a better look at my code.
Solutions/Guides I have looked at to better understand my problem
- https://gist.github.com/MRobertEvers/55a989b4883ea8d7715d2e156627f034
- Not able to import module from other directory in Python 3.7.1
- Import module from subfolder
Libraries I have attempted to use as an alternative