Factoring connection details into a package

You will want to avoid having your password and connection details strewn over several notebooks. If you or your colleages are connecting to the same database in a project, you can write a helper module that abstracts the connection details. Thus, the connection details are centralized in a single location. Start by creating a directory (let’s call it database) in the project workspace, and populate it with a setup.py containing:

# /project/database/setup.py

from setuptools import setup

setup(name='database', version='0.1', py_modules=['database'])

Then, create another file called database.py (the name name needs to match the name of the py_modules entry above, plus the .py extension):

# /project/database/database.py
import psycopg2

def connect():
    connection = psycopg2.connect(
        host='customers.cpllpj7t12adb.mydomain.com',  # host on which the database is running
        database='database_name',  # name of the database to connect to
        user='username',  # username to connect with
        password='password'  # password associated with your username
    return connection

A user can then install the database package in a server by running:

$ cd /project/database
$ pip install -e .

They can then use the connect function anywhere within their server:

import pandas
from database import connect

with connect() as conn:
    df = pandas.read_sql('SELECT * FROM customers LIMIT 4')



The above example uses psycopg2 to connect to a PostgreSQL database, but the same strategy can be used to centralize connection details for other database types.