Use os.getenv to get environment variables in Python

Until now, when it came to getting environment variables in Python, it was like this.

import os

dsn = os.environ.get('DATABASE_URL')

But recently I learned that there is a method called ʻos.getenv`.

Reference: Difference between os.getenv and os.environ.get in Python --Qiita

Since ʻos.getenv is equivalent to ʻos.environ.get, the above code can be rewritten as:

from os import getenv

dsn = getenv('DATABASE_URL')

It seems that either one is not officially recommended, but I personally think that ʻos.getenv` is better because it is "** it is clear why you are importing os **".

Since the os module has several uses, it is hard to understand "what is this for?" Even if it says ʻimport os, but if it is from os import getenv`, it is" You're importing because of environment variables. "

Recommended Posts

Use os.getenv to get environment variables in Python
How to access environment variables in Python
To reference environment variables in Python in Blender
Use pydantic when reading environment variables in Python
Get environment variables in Python, otherwise set default values
How to use SQLite in Python
How to use Mysql in python
How to use PubChem in Python
[Introduction to Python] How to use class in Python?
Method to build Python environment in Xcode 6
How to dynamically define variables in Python
How to get a stacktrace in python
Easy way to use Wikipedia in Python
How to use __slots__ in Python class
How to use regular expressions in Python
How to use is and == in Python
How to use Serverless Framework & Python environment variables and manage stages
[Python] When you want to use all variables in another file
How to use the C library in Python
Use config.ini in Python
Use dates in Python
[Bash] Use here-documents to get python power from bash
Summary of how to use MNIST in Python
Use cryptography module to handle OpenSSL in Python
Use Python in your environment from Win Automation
virtual environment in python
Get date in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
Use jupyter-lab installed in python virtual environment (venv)
Use Python in Anaconda environment with VS Code
Use profiler in Python
Development environment in Python
What to do to get google spreadsheet in python
Use pygogo to get the log in json.
I want to use Python in the environment of pyenv + pipenv on Windows 10
Python virtual environment in 2021 ~ There is no reason not to use venv now ~
How to get the variable name itself in python
How to get the number of digits in Python
Use the CASA Toolkit in your own Python environment
How to use variables in systemd Unit definition files
I tried to summarize how to use pandas in python
How to use jupyter lab in Windows 10 local environment
How to use the model learned in Lobe in Python
How to pass matplotlib backend settings in environment variables
I want to use the R dataset in python
Get YouTube Comments in Python
Let's use def in python
Use Anaconda in pyenv environment
Use Measurement Protocol in Python
To flush stdout in Python
Get last month in python
python3: How to use bottle (2)
Use callback function in Python
Use parameter store in Python
[Python] How to use list 1
Login to website in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
How to use Python argparse