Ani*_*tta 6 python environment-variables getenv python-3.x
我正在尝试运行下面的简单代码片段
port = int(os.getenv('PORT'))
print("Starting app on port %d" % port)
Run Code Online (Sandbox Code Playgroud)
我可以理解 PORT 是 s 字符串,但我需要转换为 int。为什么我收到错误
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Run Code Online (Sandbox Code Playgroud)
您没有名为 的环境变量PORT。
os.getenv('PORT')-> 返回None-> 当您尝试将其转换为 int 时抛出异常
在运行脚本之前,请通过以下方式在终端中创建环境变量:
export PORT=1234
Run Code Online (Sandbox Code Playgroud)
或者,您可以提供默认端口,以防它未在您的计算机上定义为环境变量:
DEFAULT_PORT = 1234
port = int(os.getenv('PORT',DEFAULT_PORT))
print("Starting app on port %d" % port)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9873 次 |
| 最近记录: |