Python caches compiled versions of your modules in __pycache__ folders. Usually that’s fine, but in a container image or a folder mounted read only from the host, I’d rather it didn’t try.
PYTHONDONTWRITEBYTECODE=1 python3 app.py
python3 -B app.py
# Dockerfile
ENV PYTHONDONTWRITEBYTECODE=1
The environment variable and the -B flag do the same thing: Python still compiles your code, it just keeps the result in memory and doesn’t write .pyc files to disk. Inside the program, sys.dont_write_bytecode is then True.
Where it helps: a Docker image where the app folder is mounted from the host and you don’t want root owned __pycache__ folders appearing in your checkout, a read only filesystem, or a shared network folder where several machines with different Python versions would otherwise each drop their own cache files.
The price is a slightly slower start, because imports are compiled again every run. For a small script you won’t notice. For a big app with lots of imports it adds up, and then it’s better to let the cache be written somewhere else. PYTHONPYCACHEPREFIX does that: point it at a folder like /tmp/pycache and Python keeps a mirror tree of cache files there instead of next to your source.
Further reading: PYTHONDONTWRITEBYTECODE, PYTHONPYCACHEPREFIX
Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more