0

I want to store my configuration in db.ini file. I have seen this tutorial which explains the db.ini file should be saved at: /usr/bin/env and it can be accessed as below:

#!/usr/bin/env python3

import configparser

config = configparser.ConfigParser()
config.read('db.ini')

I am working on Windows machine, and not sure where should I copy my db.ini file?

I just don't want the config file to be checked-in to source control (git) as it contains sensitive information.

Note: My development environment is Windows and Production Environment is Linux. I need a configuration file to store my credentials.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • @Chris, if I put put the credentials in the project, would it not be included in source control? In .Net, I put my credential in machine.config which is not part of the project... I was wondering if there is a similar approach in Python? – Hooman Bahreini Jul 12 '20 at 23:17
  • 1
    `db.ini` isn't a standard filename, but you can always ignore files, e.g. by adding them to your project's [`.gitignore` file](https://git-scm.com/docs/gitignore). I suggest doing that here. – ChrisGPT was on strike Jul 12 '20 at 23:21
  • 1
    There are a few common conventions: (a) [commit a sample file like `credentials.ini.sample`, ignore `credentials.ini`, and instruct developers to create their own `credentials.ini` from the sample](https://stackoverflow.com/a/50256015/354577), (b) pull configuration from environment variables, possibly using [a `.env` file](https://pypi.org/project/python-dotenv/) / [Pipenv](https://pipenv.pypa.io/en/latest/advanced/#automatic-loading-of-env) in development, (c) using `git update index --skip-worktree` with tracked files (I don't recommend this one; it's not really a Git-supported workflow). – ChrisGPT was on strike Jul 12 '20 at 23:32
  • Thanks a lot. Based on this info, it sounds like using a `.env` file would be a better option? – Hooman Bahreini Jul 12 '20 at 23:36
  • 1
    There are a few factors... if your code will eventually run on a server somewhere, that's my preferred approach. Configure it via environment variables. But if it gets packaged up and run on users' computers that might not be as good a fit. – ChrisGPT was on strike Jul 12 '20 at 23:38

1 Answers1

2

I have seen this tutorial which explains the db.ini file should be saved at: /usr/bin/env

That's not what that line means.

That's the "shebang". It's a line that is literally saved in the contents of the script. It indicates that /usr/bin/env should be used to run the file if you run it directly. In other words, it makes running

path/to/your-script

equivalent to

/usr/bin/env python3 path/to/your-script

You can store the file wherever you want; it probably belongs somewhere in whatever project directory uses the database you're defining.

I just don't want the config file to be checked-in to source control (git) as it contains sensitive information

That's sensible.

I still suggest putting it somewhere in your project directory, but make sure you tell Git to ignore it.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257