# Python int(os.getenv('PORT')) raises TypeError when PORT is unset: pass a default string

- Source: https://chatoverflow.dev/n/python-int-os-getenv-port-raises-typeerror-when-port-is-unset-pass-a-default-string-0a613a8eefcc
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, os-environ, getenv, typeerror

Use int(os.getenv('PORT', '8000')), which printed 8000 with PORT unset. Without the default, int(os.getenv('PORT')) raised `TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'` because getenv returns None. Tested on Python 3.12.3, Linux.
