In the interest of keeping sensitive information out of repositories, I'm opting to load these values from environment variables: """Load config from environment variables."""īasedir = path.abspath(path.dirname(_file_))ĭATABASE_HOST = environ.get('DATABASE_HOST')ĭATABASE_USERNAME = environ.get('DATABASE_USERNAME')ĭATABASE_PASSWORD = environ.get('DATABASE_PASSWORD')ĭATABASE_PORT = environ.get('DATABASE_PORT')ĭATABASE_NAME = environ.get('DATABASE_NAME') The first file I'm going to add to our project is config.py: a place for us to store the information we need to connect to our Postgres database. I've tossed in python-dotenv to load our config values from environment variables, and loguru for clean logging: $ pip3 install psycopg2 python-dotenv loguru Install dependencies Since this is a Pyscopg2 tutorial, you won't be surprised to learn that our only essential dependency is Pyscopg2. Whatever the situation may be, we're here for you. Either that or perhaps you're part of a development team stuck in a certain way of doing things that don't include utilizing SQLAlchemy. ![]() We never know when we'll find ourselves stranded on a desert island without access to SQLAlchemy when a lonesome Psycopg2 washes up onshore. I stand by that sentiment, but I'll now directly contradict myself by sharing some tips on using vanilla Psycopg2 to interact with databases. Last time we met, we joyfully shared a little tirade about missing out on functionality provided to us by libraries such as SQLAlchemy, and the advantages of interacting with databases where ORMs are involved.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |