Running discord.py on Railway, but print statements are not output as logs.

Although I should be doing stdout with print statements, for some reason they are not output in the log. For example, the following source code does output:
#!/usr/bin/env python3.6
import pymysql.cursors

con = pymysql.connect(
host = 'contap',
user = 'root',
password = '',
database = 'railway',
port = ,
cursorclass=pymysql.cursors.DictCursor
)

cursor = con.cursor()

cursor.execute("select * from server_configs;")
result = cursor.fetchall()
print(result)
print(result[0]['night_base_time'])
#!/usr/bin/env python3.6
import pymysql.cursors

con = pymysql.connect(
host = 'contap',
user = 'root',
password = '',
database = 'railway',
port = ,
cursorclass=pymysql.cursors.DictCursor
)

cursor = con.cursor()

cursor.execute("select * from server_configs;")
result = cursor.fetchall()
print(result)
print(result[0]['night_base_time'])
However, when I switch to code that includes discord.py, there is no output. Even if I pasted the above code at the very top, the result was the same. I can't provide all the code for discord.py, but I will provide a part of it. When run on a CentOS 8 environment, the print statements are displayed correctly.
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
for guild in client.guilds:
server_configs[guild.id] = default_config.copy()
postable_channel_found = False
for channel in guild.channels:
if isinstance(channel, discord.TextChannel):
perms = channel.permissions_for(guild.me)
if not perms.send_messages:
continue
if not postable_channel_found:
server_configs[guild.id]['channel_id'] = channel.id
server_configs[guild.id]['channel'] = channel
print(f"Default post channel for {guild.name} set to {channel.name} with ID {channel.id}.")
await channel.send(" has started. Please input your settings.") # Send message to channel
postable_channel_found = True
else:
break
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
for guild in client.guilds:
server_configs[guild.id] = default_config.copy()
postable_channel_found = False
for channel in guild.channels:
if isinstance(channel, discord.TextChannel):
perms = channel.permissions_for(guild.me)
if not perms.send_messages:
continue
if not postable_channel_found:
server_configs[guild.id]['channel_id'] = channel.id
server_configs[guild.id]['channel'] = channel
print(f"Default post channel for {guild.name} set to {channel.name} with ID {channel.id}.")
await channel.send(" has started. Please input your settings.") # Send message to channel
postable_channel_found = True
else:
break
Is there a problem where print statements are not output when using discord.py? Or do I need to set something up? I would appreciate it if you could let me know. Thank you in advance.
Solution:
but yeah Adam is right, python print statements are buggy in docker environments, there's 3 things you can do 1. what Adam said, flush=True 2. set a service variable PYTHONUNBUFFERED = 1 (this is what is done by nixpacks) 3. run python with python -u instead...
Jump to solution
9 Replies
Percy
Percy16mo ago
Project ID: f83f8d1f-6778-472e-9271-de7e9f261bb6
IrisPumila
IrisPumila16mo ago
f83f8d1f-6778-472e-9271-de7e9f261bb6
Adam
Adam16mo ago
Python’s print statements can be buggy, can you try adding flush=True to your statements?
Brody
Brody16mo ago
are you building with a dockerfile?
IrisPumila
IrisPumila16mo ago
yes, dockerfile is here FROM python:3.6 WORKDIR /bot COPY requirements.txt /bot/ RUN pip install -r requirements.txt COPY . /bot CMD python main.py
Brody
Brody16mo ago
doesn't look like it's doing anything nixpacks wouldn't do by default, try removing your dockerfile and let railway build it with nixpacks
Solution
Brody
Brody16mo ago
but yeah Adam is right, python print statements are buggy in docker environments, there's 3 things you can do 1. what Adam said, flush=True 2. set a service variable PYTHONUNBUFFERED = 1 (this is what is done by nixpacks) 3. run python with python -u instead
IrisPumila
IrisPumila16mo ago
As Adam said, I was able to output with flush=True. However, it's a hassle to write this for every print statement. The method you suggested, setting the environment variable to PYTHONUNBUFFERED = 1, also worked. Thank you. I will consider this issue resolved now.
Brody
Brody16mo ago
glad we could help 🙂
Want results from more Discord servers?
Add your server