PythonAnywhere’s Free Version —  yourname.pythonanywhere.com domain and Building a Discord Bot

PythonAnywhere (https://www.pythonanywhere.com/) is a cloud-based platform where you can write, run, and host Python code directly in your browser. With its free plan, it’s an attractive option for anyone looking to explore web development or automation without having to worry about managing servers or complex setups. Whether you’re a beginner or an experienced programmer, PythonAnywhere offers a range of tools to help you deploy projects quickly.

The Free Version: What You Get and What to Expect

If you’re curious about the free plan, you’ll be glad to know it provides a solid foundation for small projects. With the free version, you get:

  • 512 MB of storage for your code and data.
  • Access to one web app under the yourname.pythonanywhere.com subdomain.
  • A limited amount of CPU time (100 seconds per day).
  • The ability to schedule tasks (once a day for free users).
  • Python and Bash consoles to test and run code directly in your browser.

This version is perfectly fine for deploying basic applications, building bots, and running scripts at scheduled times. However, because it’s free, there are some limits:

  • CPU time restrictions: If your app runs out of the free CPU allowance, you will need to wait for the next day to run more tasks. This limit can be a challenge if you’re working on something that requires more processing power, like web scraping or machine learning.
  • No external internet access for free users: You can only access whitelisted sites, which limits access to many third-party APIs unless they’re pre-approved by PythonAnywhere. The good news is that you can still use some major services like Google, Twitter, and Reddit.
  • Limited number of web apps: You can only host one web app at a time under the free plan. If you want to experiment with multiple apps, you’ll need to upgrade to a paid plan.

Despite these limits, you can achieve a lot with the free plan. For instance, let’s say you want to host your own website or a simple web service — you’ll be assigned a subdomain, such as yourname.pythonanywhere.com, where your app will be publicly accessible.

How to Set Up Your Web App

Once you create a PythonAnywhere account, you can set up a web app in just a few steps. Here’s how:

  1. Log in to your account and navigate to the “Web” tab.
  2. Click on “Add a new web app”.
  3. Choose FlaskDjango, or a simple Python script. Flask is a great option for beginners.
  4. Follow the prompts to configure your app.
  5. Once you’re done, you’ll receive a live link to your web app at yourname.pythonanywhere.com.

At this point, you can start building your web application and seeing your changes in real time. For example, if you want to make a simple page displaying some text, create a flask_app.py file, and add the following code:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from PythonAnywhere!'

if __name__ == '__main__':
    app.run()

Upload this file in PythonAnywhere’s file manager, and the platform will automatically handle the rest, giving you a live site you can share.

Example: Creating a Discord Bot That Collects Emails

Now, let’s dive into an interesting project — creating a simple Discord bot using PythonAnywhere that collects emails entered by users through direct messages.

Discord bots are a fun way to automate interactions, and PythonAnywhere’s free plan is perfect for this project. The goal here is to create a bot that listens for email addresses sent via DM and stores them in a file.

Step 1: Install the Required Packages

In PythonAnywhere’s console, you’ll need to install the discord.py library, which makes it easy to interact with Discord’s API. Use the Bash console to run:

pip install discord.py

Step 2: Writing the Bot Code

Next, create a new file called discord_bot.py. In this file, add the following code:

import discord

TOKEN = 'YOUR_DISCORD_BOT_TOKEN'

client = discord.Client()

@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

@client.event
async def on_message(message):
    if isinstance(message.channel, discord.DMChannel):  # Check if the message is a DM
        if '@' in message.content:  # Simple email check
            with open('emails.txt', 'a') as f:
                f.write(f'{message.author}: {message.content}\n')
            await message.channel.send('Thanks! Your email has been collected.')
        else:
            await message.channel.send('Please send a valid email address.')

client.run(TOKEN)

Make sure to replace YOUR_DISCORD_BOT_TOKEN with the actual token of your Discord bot, which you can get by creating an application on the Discord Developer Portal.

Step 3: Running the Bot

Now, you can run your bot. Open the PythonAnywhere console and execute:

python3 discord_bot.py

Your bot will start running and logging messages to the console. It will automatically listen for DMs, check if the message contains an email, and store any valid email addresses in emails.txt.

Step 4: Scheduling the Bot

To keep the bot running without manually starting it every day, you can use PythonAnywhere’s scheduling feature. In the “Tasks” tab, you can set up a scheduled task to run python3 discord_bot.py once a day. This will restart the bot, ensuring it’s live every day without you having to log in and start it manually.


Thank you for reading this article. We hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials/articles, please feel free to join and comment. Your feedback and suggestions are always welcome!

You can find the same tutorial on Medium.com.

Leave a Reply