Learning objectives
After reading this article you will be able to:
- Identify which kind of Python app you are hosting and what it needs
- Compare a VPS, a container platform, serverless and shared hosting
- Prepare the app for production with the right server, dependencies and version
Python runs almost anything: a Discord bot, a FastAPI service, a Django site, a script that collects data every hour, an AI model. That is why "where should I host Python?" has no single answer. A bot needs to stay up 24 hours a day, a scheduled job runs for two minutes a day and a model using torch needs a lot of RAM.
Start with the type of app. It drives the criteria that change the choice, the recommendation per scenario and what to prepare before you deploy.
What kind of Python app are you hosting?#
Almost every project falls into one of four groups:
- Long-running process: Discord and Telegram bots, queue workers, continuous scrapers. They stay up all the time and keep connections open.
- Web app: APIs and sites. They can be WSGI (Flask, classic Django), served by Gunicorn or Waitress, or ASGI (FastAPI, Starlette, async Django), served by Uvicorn. They receive HTTP requests and must listen on a port.
- Scheduled job: a script that runs every so often, such as a daily report or a database cleanup. It sits idle most of the time.
- AI and data: apps that load models with torch, transformers or pandas. What matters here is RAM and the size of the dependencies.
The type sets the first decision: whether the process must always be running or can wake up on demand.
Which criteria matter when choosing?#
- Always on or on demand. Bots and WebSocket connections need a continuous process. Short jobs can run on demand.
- Time limit per run. Serverless functions have a ceiling: on AWS Lambda, an invocation lasts at most 15 minutes.
- RAM. Each web worker is a separate Python process and adds memory. AI models can take several GB. See how to size RAM and CPU.
- Native dependencies. Libraries such as numpy, psycopg and torch ship compiled code. If the environment has no prebuilt package (wheel) for your Python version and system, installation tries to compile and may fail.
- Python version. Use a version that is still supported. The official schedule shows which versions are in that phase and when each one stops getting fixes.
- How much you want to run yourself. Operating system, security updates, firewall, reverse proxy and automatic restarts: someone has to handle them. It is either you or the platform you host on.
What are the options, with pros and cons?#
| Option | Good for | Pros | Cons |
|---|---|---|---|
| Home PC or Raspberry Pi | Tests and personal projects | No hosting bill | Goes down with power and internet; residential IP; exposes your network |
| VPS | People who want full control | Root access, any software | You handle the OS, security, proxy, systemd and updates |
| Container platform (application hosting) | Bots, APIs, sites, workers | Deploy via Git, CLI or ZIP; automatic restart; HTTPS ready | Less control over the system; limits per plan |
| Serverless (functions) | Short jobs and irregular traffic | Pay per run; no idle process | Time limit; cold starts; poor fit for long connections |
| Shared hosting | Simple existing sites | Cheap and familiar | Limited Python support; no long-running processes |
Two points usually settle it. First, serverless does not fit bots: Lambda caps a run at 15 minutes and also caps the size of a .zip package, which rules out large libraries such as torch unless you use a container image (the current sizes are on Lambda's limits page). Second, a VPS gives you freedom, but every hour spent configuring a server is an hour away from your code. The articles VPS vs cloud platform and serverless vs containers go deeper into those two comparisons.
Which option is best for each scenario?#
- Discord, Telegram or WhatsApp bot: an always-on process, on a container platform or a VPS. The bot reconnects on its own when the process restarts, as long as something restarts it.
- FastAPI or Flask API with steady traffic: an always-on container with a production server. Without cold starts, the first request of the day is as fast as the rest. See how to host FastAPI, Flask and Django.
- Django site with a database: a container or VPS, plus a separately hosted database. See the best database for Python.
- Script that runs once a day: serverless with a schedule, or an always-on process with an internal scheduler if you already have an app running.
- App using torch or transformers: a container or VPS with plenty of RAM. Check that the environment has prebuilt packages so nothing compiles during deploy.
- Learning and testing: your own computer. When the project needs to stay online, move it.
How do you prepare a Python app for production?#
Whatever option you pick, four habits prevent most problems.
1. Pin your dependencies. List everything in requirements.txt with versions, or use a lockfile (uv, Poetry). That way the server installs exactly what you tested:
# example versions: pin the ones you tested (check with pip freeze)
fastapi==0.143.0
uvicorn==0.54.02. Use a production server. The Flask docs are direct: do not use the development server in production, because it is not designed to be secure, stable or efficient. Django says the same about runserver. For FastAPI, run Uvicorn listening on all interfaces (0.0.0.0), not only on 127.0.0.1. A minimal main.py:
import os
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
def health():
return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", "8000"))
uvicorn.run(app, host="0.0.0.0", port=port)For several processes, the FastAPI docs use the --workers option:
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 2For Flask or Django, the equivalent is Gunicorn. The Gunicorn docs suggest starting with (2 × CPU cores) + 1 workers and adjusting under load. Remember that each worker uses its own RAM.
3. Read configuration from the environment. Tokens, passwords and database URLs belong in environment variables, never in the code. So does the port: each host tells you which one to use.
4. Write logs to standard output. Use print or the logging module pointed at stdout. Almost every host shows that output in its dashboard, and that is where you will diagnose errors.
Which errors show up when hosting Python?#
ModuleNotFoundError: a package missing fromrequirements.txt, or an install name that differs from the import name. See Cannot find module and ModuleNotFoundError.- The app starts but nobody can reach it: the server listens on
127.0.0.1or on a port other than the one the host expects. - Process killed without a clear error: out of memory, common with many workers or large models. Reduce workers or add RAM.
- Data that disappears: SQLite or files saved to the disk of an environment that is recreated on every deploy. Use a database or external storage.
- Compilation failing during deploy: the server's Python version has no wheel for some dependency. Match your local version to the server's.
How to do this on Square Cloud#
Among Square Cloud's several runtimes, such as Node.js, Python and Go, the Python one comes with prebuilt wheels for torch and transformers, so those libraries are not compiled during deploy. You deploy by uploading a ZIP in the dashboard, with the CLI, from GitHub (a push deploys, through the official GitHub Action or a webhook) or with the VS Code extension. The squarecloud.app configuration file is optional: the dashboard generates it on deploy (runtime, memory, main file), or you write it at the project root.
Apps run in always-on containers, with no cold starts or sleep mode, which works for bots and APIs alike. Automatic restart is on by default for dashboard deploys: it brings the app back when the process exits with an error. On start, the platform runs pip install -r requirements.txt, with your environment variables already set, and the app's disk persists across restarts and deploys.
Your plan's RAM is a shared pool: at least 256 MB per background app, such as a bot, and 512 MB per web app. Hobby starts at R$12.49 per month with 1 GB. Environment variables are set per app in the dashboard, API, CLI or VS Code, and a change takes effect on the next restart, without a rebuild.
A web app listens on 0.0.0.0, on the port in the PORT variable. Manual snapshots are available on every plan. See the details of the Python environment on the runtimes page.
Deploy
Push from GitHub, the CLI or a ZIP and go live in seconds
FAQs
Can I use flask run or manage.py runserver in production?
No. The Flask and Django docs both say the development server must not be used in production, because it was not built or tested for production security and performance. Use Gunicorn or Waitress for WSGI, or Uvicorn for ASGI.
Which Python version should I use in production?
A version that is still supported, ideally the newest one your dependencies already accept. The official Python release schedule shows which versions are supported and when each one reaches end of life. If a dependency has not published packages for the newest release yet, stay on the previous one.
Can I host a Python Discord bot on serverless?
It usually does not work. A Discord bot keeps a WebSocket connection to the Gateway open all the time, and serverless functions have a time limit per run, such as AWS Lambda's 15 minutes. Bots need an always-on process.
How much RAM does a Python app need?
A small Flask or FastAPI API usually runs in 256 to 512 MB. Each extra Gunicorn or Uvicorn worker is a new process and adds memory. Apps using torch or transformers can need several GB.
More in Deploy
See all 33 articles- What are environment variables?Environment variables are key-value pairs the operating system hands to a process at startup. Learn to read them in Node.js and Python and keep tokens out of code.
- What is a deployment?A deployment takes a version of your code to a server, prepares it and starts it. See the stages, the methods and how to roll back when it goes wrong.
- How much RAM and CPU does my app need?Size RAM and CPU by measuring your app's real peak and adding about 30% headroom. Learn how to measure in Node.js, Python and Java and what to do if short.
- Automatic deploys with GitHub ActionsAutomatic deploys with GitHub Actions ship your code to the server on every push. See the workflow step by step, secrets, tests before deploy and common errors.