Python script can't load env variables from Railway

AnonymousTRIAL

6 months ago

I have deployed a python script to Railway that read tweets and sends an email when certain requirements are met. To do this, I need some to load some env variables like the "TWITTER_BEARER_TOKEN", but when I try this I get the error: ERROR - Error fetching tweets: 401 Unauthorized.

Which means the script is not getting the variables. Here's a snippet of how I'm fetching the variables:

```import tweepy

import os

import smtplib

import pytesseract

from PIL import Image

import requests

from dotenv import load_dotenv

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

import time

import logging

# Only load .env file in development, not on Railway

if not os.getenv('RAILWAY_ENVIRONMENT'):

load_dotenv()

# Set up logging

logging.basicConfig(

level=logging.INFO,

format='%(asctime)s - %(levelname)s - %(message)s'

)

# Twitter API credentials (Bearer Token for API v2)

TWITTER_BEARER_TOKEN = os.getenv("TWITTER_BEARER_TOKEN")

# Gmail credentials

GMAIL_USER = os.getenv("GMAIL_USER")

GMAIL_PASSWORD = os.getenv("GMAIL_PASSWORD") # Gmail password stored in environment variable

# Safer environment variable handling with defaults

SUBSCRIBED_EMAILS = [email.strip() for email in os.getenv("SUBSCRIBED_EMAILS", "").split(',') if email.strip()]

ESTATE_NAMES = [name.strip() for name in os.getenv("ESTATE_NAMES", "").split(',') if name.strip()]```

1 Replies

6 months ago

Hello,

Please link to a service.