admin管理员组

文章数量:1296258

When i'm trying to run the bot uploaded to home/%User_name%/ at PythonAnywhere it crashes with:

    File "/home/%User_name%/%Project_name%/./handlers/command_handlers.py",
line 103, in handle_start
    if str(user_id) in config["ADMINS"]:
    KeyError: 'ADMINS'

Looks like it can't find my config.json. I have config and data_user JSONs in my home/%User_name%/%Project_name%/ folder. It seems I need to do smth more, than just simply upload my project from PC.

I have this part of code, related to the path:

CONFIG_FILE = "%Project_name%/config.json"
def load_config():  
    if not os.path.exists(CONFIG_FILE):  
        return {}  
    with open(CONFIG_FILE, 'r', encoding='utf-8') as file:  
        return json.load(file)

It worked perfectly on my Win10 PC, where project folder lies right in the Visual Studio Code workspace.

What should I change to run the bot at PythonAnywhere?

I tried to change the path in my bot code, but it didn't work.

When i'm trying to run the bot uploaded to home/%User_name%/ at PythonAnywhere it crashes with:

    File "/home/%User_name%/%Project_name%/./handlers/command_handlers.py",
line 103, in handle_start
    if str(user_id) in config["ADMINS"]:
    KeyError: 'ADMINS'

Looks like it can't find my config.json. I have config and data_user JSONs in my home/%User_name%/%Project_name%/ folder. It seems I need to do smth more, than just simply upload my project from PC.

I have this part of code, related to the path:

CONFIG_FILE = "%Project_name%/config.json"
def load_config():  
    if not os.path.exists(CONFIG_FILE):  
        return {}  
    with open(CONFIG_FILE, 'r', encoding='utf-8') as file:  
        return json.load(file)

It worked perfectly on my Win10 PC, where project folder lies right in the Visual Studio Code workspace.

What should I change to run the bot at PythonAnywhere?

I tried to change the path in my bot code, but it didn't work.

Share asked Feb 11 at 21:46 PlemPlem 34 bronze badges 2
  • Are you using a virtual environment? – dev_light Commented Feb 12 at 3:35
  • I'm using simple Files upload and running the bot.py with console. – Plem Commented Feb 12 at 6:53
Add a comment  | 

1 Answer 1

Reset to default 0

Okay, I've tried a bit more and got success.
Both user_data and config files lies in %project_name% folder, where main bot.py file is.
I changed user_data path to simple "DATA_FILE: "user_data.json", and edited the code to:

CONFIG_FILE = "config.json"   # <- changed here

def load_config():
  if not CONFIG_FILE:         # <- and here
    return {}
  with open(CONFIG_FILE, 'r', encoding='utf-8') as file:
    return json.load(file)

本文标签: pythonHow to deploy TGbot to PythonAnywhereStack Overflow