LoginSignup
4
5

More than 5 years have passed since last update.

テスト時と本番環境で処理を分けたい

Last updated at Posted at 2014-11-11

テスト時とかにどうしても処理分けたかった。sys.argvを使って実行時引数のファイル名から判断しようかな。

import sys

if 'test_loader.py' in sys.argv:
    db = load_database('test')
else:
    db = load_database('production')

実行時

python test_loader.py

でも、もし test_loaderを他のスクリプトから呼びだす必要がある場合、この方法はだめ。。

※ 2014/11/12 追記
コメントで頂いた環境変数を設定する場合は os.environ を直接操作したほうが良いとのこと。
15.1. os — 雑多なオペレーティングシステムインタフェース Python 2.7ja1 documentation

test_loader.py:

import os
if not os.getenv('DB'):
    os.environ['DB'] = 'test'
4
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5