Useful Python Modules

Easily manage settings with Pydantic

During development, configuration files are often needed to easily change the behavior of the system. For example, the switch of Debug mode, database related settings, etc. are usually placed in configuration files.

In addition to the built-in module configparser provided by Python to easily implement configuration files, it can also easily implement configuration files using classes, such as the following two files are examples of configuration

# settings.py
class Settings(object):
    DB_HOST = 'localhost'
    DB_PORT = 8888


settings = Settings()
# test.py
from settings import settings


print(settings.DB_HOST, settings.DB_PORT)

However, nowadays development projects also often use dotenv (e.g. python-dotenv) to make configuration easier.

In addition to developing the functionality of dotenv by yourself, you can actually choose to integrate class-style configuration files and dotenv easily with pydantic.

Posted on  Jul 1, 2020  in  Useful Python Modules , Python Programming - Advanced Level  by  Amo Chen  ‐ 5 min read

Python dotenv introduction and tutorial

Storing important data in environment variables is a common practice when developing, not only to avoid accidentally committing important data into the codebase, but also to use environment variables to store system or program settings. It is also often used in practice to separate development environment from production environment, for example:

if os. getenv('MODE') == 'development':
    # do development-related things
    pass
else:
    # do production-related things
    pass

But as the number of environment variables to be set increases, it may lead to a lot of environment variables to be filled every time you do development. If you have encountered such a situation, why not give python-dotenv a try!

Last updated on  Mar 13, 2023  in  Useful Python Modules  by  Amo Chen  ‐ 2 min read

Pyenv - A great tool for Python version management

Python has been evolving for nearly 10 years, so the development of Python projects also needs to consider the issue of version.

Especially now that Python 2 has retired, most Python projects now use Python 3 as the main version, but some of the old projects in companies still use Python 2 for development, so developers must switch between Python 2 and 3.

If you have trouble switching between Python versions, then pyenv will be your best friend!

Last updated on  Mar 13, 2023  in  Python Programming - Beginner Level , Useful Python Modules  by  Amo Chen  ‐ 3 min read