Use pydantic when reading environment variables in Python

When implementing an application, handling environment variables is a little troublesome, isn't it?

When implementing in Python, you can use a library called pydantic to set default values, cast to int types, or set values from .env. You can easily implement processing such as reading. In this article, I will introduce those sample codes.

By the way, pydantic is not a library that only reads environment variables, but a library that can define classes using type annotations. For example, you can cast and validate json format data to classes. If you want to read all about other features, please read the following article.

Introduction to Pydantic

Read environment variables

Suppose the following environment variables are set:

#bash format
export REDIS_HOST_NAME=localhost
export REDIS_PORT=6379

If you use pydantic, you can read it with the following code.

from pydantic import BaseSettings

class Settings(BaseSettings):
    redis_host_name: str
    redis_port: int

settings = Settings()
 
print(settings.redis_host_name)
# => localhost
print(settings.redis_port)
# => 6379
print(type(settings.redis_port))
# => <class 'int'>

If the environment variable is not set, or if the typecast fails, a validation error will be issued.

pydantic.error_wrappers.ValidationError: 1 validation error for Settings
redis_port
  value is not a valid integer (type=type_error.integer)

Use dotenv

During development, you can use a file format called .env to simplify setting environment variables. This is an article on node.js, but I think this is easy to understand.

Use .env file instead of environment variables (dotenv)

First, you also need to install python-dotenv with pip install python-dotenv orpip install pydantic [dotenv].

Given the following .env file,

.env


REDIS_HOST_NAME=localhost
REDIS_PORT=6379

You can load it with code like this:

from pydantic import BaseSettings

class Settings(BaseSettings):
    redis_host_name: str
    redis_port: int

    class Config:
        env_file = '.env'

settings = Settings()

More detailed functions

With this, I think we have introduced useful functions related to environment variables that are often used in everyday development. If you want to know more, please read the corresponding item in the official document.

Settings management - pydantic

Recommended Posts

Use pydantic when reading environment variables in Python
Use os.getenv to get environment variables in Python
Handle environment variables in Python
How to access environment variables in Python
[Python] Use and and or when creating variables
To reference environment variables in Python in Blender
[Python] When you want to use all variables in another file
[Python] Get environment variables
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use Python in your environment from Win Automation
virtual environment in python
Use jupyter-lab installed in python virtual environment (venv)
Use Python in Anaconda environment with VS Code
[Question] What happens when I use% in python?
Get environment variables in Python, otherwise set default values
Use the CASA Toolkit in your own Python environment
Use communicate () when receiving output in a Python subprocess
Environment variables when using Tkinter
Use let expression in Python
Use Anaconda in pyenv environment
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
HTTP environment variables in Flask
Use Spyder in Python IDE
Attention when os.mkdir in Python
Error when building mac python environment
Use fabric as is in python (fabric3)
Landmines hidden in Python class variables
Behavior when listing in Python heapq
Install scrapy in python anaconda environment
How to use SQLite in Python
When reading C ++ structs in Cython
Use rospy with virtualenv in Python3
Using global variables in python functions
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Use django-debug-toolbar in VirtualBox / Vagrant environment
Reading and writing text in Python
Use gcc-4.2 when compiling Python (MacOS)
When using regular expressions in Python
When writing a program in Python
How to use Serverless Framework & Python environment variables and manage stages
Install Networkx in Python 3.7 environment for use in malware data science books
When specifying multiple keys in python sort
[Python3] Dynamically define global variables in functions
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
Use tensorflow in an environment without root
Method to build Python environment in Xcode 6
Use smbus with python3 under pyenv environment
How to dynamically define variables in Python
Using venv in Windows + Docker environment [Python]