Configure a Python application
On app hosting, the platform expects a Python entry point and a start command. By default it installs requirements.txt, then runs python main.py. Your code must therefore start without arguments, read configuration from the environment and stay running: a script that exits after initialisation is treated as stopped.
Entry point
Put main.py at the project root, or point to the right path if your file lives in src/. The file must start the server or event loop at top level, or inside an if __name__ == "__main__": block.
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
body = b"ok"
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
port = int(os.environ.get("PORT", "8000"))
HTTPServer(("0.0.0.0", port), Handler).serve_forever()serve_forever() keeps the process alive. A print followed by a script exit causes an immediate stop, then a restart loop.
Dependencies and version
Declare packages in requirements.txt with precise versions. The platform offers Python 3.12, 3.11, 3.10 and 3.9: a wheel built for 3.12 does not work on 3.9, and vice versa. Check wheel availability before choosing an older version.
Do not bundle a virtual environment in the archive: it contains absolute paths from your machine. Let the platform install dependencies. Add PYTHONUNBUFFERED=1 to the variables if your print output does not appear immediately in the console.
Port and exposure
A web application reads PORT and listens on 0.0.0.0. A Discord bot opens no port: it connects to the gateway. In both cases, do not hard-code the port and do not assume localhost is reachable from outside.
If initialisation is slow (migrations, model loading), start listening quickly and move heavy work after startup, or the platform may consider the service unavailable.
Variables and files
Variables are configured in the panel and injected at startup. A change requires a restart. Files written by the application persist in the project folder depending on the plan, but they do not replace a backup: export important data.
Do not write into installed packages or the interpreter folder. Use a dedicated subfolder (data/, uploads/) with appropriate permissions.
Check
- The install log contains no dependency error.
- Startup prints the port or your initialisation message.
- A request to
/or/healthanswers. - The service stays up and memory settles.
- After renaming a file, the start command points to the right path.
If the service restarts endlessly, read the first exception: it almost always indicates a missing import, a wrong path or a missing variable.
Advertisement
Was this article helpful?
Read next
- Understanding requirements.txtPin versions, install with pip and reproduce an identical environment.
- Deploy a Flask applicationRun Flask on the provided port, without debug, with a health route.
- Node.js or Python for your project?Compare execution models and ecosystems to choose a runtime pragmatically.